11#!/usr/bin/env node
22
33global . config = { }
4+ Error . stackTraceLimit = Infinity
5+
46for ( const f in require ( 'ramda' ) )
57 global [ f ] = require ( 'ramda' ) [ f ]
8+
69const { readFile, writeFile} = require ( 'fs' ) . promises
710const dns2 = require ( 'dns2' ) ;
811const { promisify} = require ( 'util' )
@@ -11,36 +14,101 @@ const http = require('http')
1114const daemonizeProcess = require ( 'daemonize-process' )
1215const yaml = require ( 'yaml' )
1316const { program } = require ( 'commander' )
17+ const execa = require ( 'execa' )
18+
19+ const running = { }
20+
21+ setInterval ( ( ) => {
22+ console . log ( { running : Object . keys ( running ) } )
23+ for ( const name in running ) {
24+ // console.log(name, running[name])
25+ if ( running [ name ] . exitCode != null ) {
26+ // console.log(name)
27+ delete running [ name ]
28+ }
29+ }
30+ } , 300 )
31+
32+ const run = ( command , name , opts ) => {
33+
34+ console . log ( `${ name } run ${ command } ` )
35+ // pp({run: command, name})
36+
37+ const r = execa ( 'bash' , [ '-c' , command ] , opts )
38+ if ( name ) {
39+ // console.log(running[name].killed, running[name].closed)
40+ console . log ( running [ name ] && running [ name ] . exitCode )
41+ if ( running [ name ] && running [ name ] . exitCode == null ) {
42+ console . log ( 'alraeedy runnint ' + name )
43+ return running [ name ]
44+ }
45+ running [ name ] = r
46+ }
47+ // r.stdout.pipe(process.stdout)
48+ // r.stderr.pipe(process.stderr)
49+ return r
50+ }
51+
52+ const healthcheck = ( c , name ) => {
53+
54+ console . log ( `${ name } health ${ c . healthcheck } ` )
55+ // if (!c.healthcheck ) return Promise.reject()
56+ const r = running [ name ]
57+ if ( r && r . exitCode == null ) return r
58+
59+ if ( r ) {
60+ delete running [ name ]
61+ }
62+
63+ const healthcheck = running [ `healthcheck ${ name } ` ]
64+ if ( healthcheck ) return healthcheck
65+
66+ if ( c . healthcheck )
67+ return run ( c . healthcheck , `healthcheck ${ name } ` , { cwd : c . folder || '~' } )
68+
69+ return Promise . reject ( { } )
70+ }
71+
72+
73+ const onetimeServer = ( title ) => {
74+ // daemonizeProcess()
75+ let message = ''
76+ let closed = false
77+ process . stdin . on ( "data" , data => {
78+ message += data . toString ( )
79+ process . stdout . write ( message )
80+ } )
81+ process . stdin . on ( 'close' , ( ) => closed = true )
1482
15- const onetimeServer = ( { message, title} ) => {
1683 try {
1784 const server = http . createServer ( function ( req , res ) {
1885 res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
1986 res . write ( `callmemaybe: ${ title } \n\n${ message } ` )
2087 res . end ( )
21- setTimeout ( ( ) => {
22- console . log ( 'one time server down' )
23- server . close ( )
24- } , 100 )
88+ if ( closed ) setTimeout ( ( ) => {
89+ console . log ( 'one time server down' )
90+ process . exit ( )
91+ } , 300 )
92+
2593 } ) . listen ( { port : 80 , host : '0.0.0.0' } , ( ) => console . log ( 'one time server up' ) )
2694 } catch ( e ) {
2795 console . error ( e )
2896 }
2997}
3098
3199program
32- . option ( '--test ' )
100+ . option ( '--server ' )
33101 . parse ( )
34102const options = program . opts ( ) ;
35103
36- if ( options . test ) {
37- onetimeServer ( { message : `Hello, is it me you're looking for?` , title : 'test' } )
38- // daemonizeProcess();
39- return
104+ if ( options . server ) {
105+ return onetimeServer ( '' )
40106}
41107
42108const pp = x =>
43- process . stdout . write ( yaml . stringify ( x || { } ) )
109+ x
110+ // console.log(x)
111+ // process.stdout.write(yaml.stringify(x || {}))
44112
45113const pe = x =>
46114 process . stderr . write ( yaml . stringify ( x || { } ) )
@@ -49,7 +117,10 @@ dns2.pp = (x, comment='') => console.log(comment + join('',values(mapObjIndexed(
49117
50118require ( './config' ) . then ( ( ) => {
51119
52- let running = [ ]
120+
121+ // setInterval(() => {
122+ // pp({running})
123+ // }, 1000)
53124
54125const server = dns2 . createServer ( {
55126 udp : true ,
@@ -59,7 +130,7 @@ const server = dns2.createServer({
59130 const c = config [ question . name ]
60131
61132 if ( c ) {
62- pp ( { matched : c , running} )
133+ pp ( { matched : c , running : keys ( running ) } )
63134 response . answers . push ( {
64135 name : question . name ,
65136 type : dns2 . Packet . TYPE . A ,
@@ -68,36 +139,18 @@ const server = dns2.createServer({
68139 address : c . ip || '127.0.0.1'
69140 } ) ;
70141
71- if ( includes ( question . name , running ) ) return send ( response )
72- running . push ( question . name )
73- setTimeout ( ( ) => {
74- running = without ( question . name , running )
75- pp ( { running} )
76- } , 1000 ) //should be healthcheck start interval
77-
78- await ( c . healthcheck ? exec ( c . healthcheck , { cwd : c . folder || '~' , stdio : 'inherit' } ) : Promise . reject ( { } ) )
79- . then ( ( { stdout, stderr} ) => {
80- running . push ( question . name )
81- send ( response )
82- pp ( { healthcheck : 'ok' , stdout, stderr} )
142+ await healthcheck ( c , question . name )
143+ . catch ( async x => {
144+ await run ( c . start , question . name , { cwd : c . folder } )
145+ . catch ( ( { stderr, stdout} ) => {
146+ console . log ( 'failed to start' )
147+ run ( `/home/vganzin/work/callmemaybe/callmemaybe.js --server` , 'error-server' , { input : stderr + stdout } )
148+ . catch ( pp )
149+ } )
83150 } )
84- . catch ( async ( { stdout, stderr} ) => {
85- pp ( { healthcheck : 'fail' , stdout, stderr} )
86- running = without ( question . name , running )
87- if ( c . start ) {
88- pp ( { starting : c . start } )
89-
90- return await exec ( c . start , { cwd : c . folder , stdio : 'inherit' } )
91- . then ( pp )
92- . catch ( ( { stderr} ) => {
93- pp ( { stderr} )
94- onetimeServer ( { message : stderr , title : question . name + ' ' + c . start + ' error' } )
95- } ) . then ( ( ) => {
96- send ( response )
97- } )
98- }
99151
100- } )
152+ send ( response )
153+ return
101154 }
102155
103156 if ( blocklist . has ( question . name ) ) {
@@ -130,8 +183,8 @@ const server = dns2.createServer({
130183} )
131184
132185// server.on('request', (request, response, rinfo) => {
133- // console.log(request.header.id, request.questions[0]);
134- // });
186+ // console.log(request.header.id, request.questions[0])
187+ // })
135188
136189. on ( 'requestError' , ( error ) => {
137190 console . log ( 'Client sent an invalid request' , error )
0 commit comments