@@ -30,22 +30,12 @@ import { Readable, Writable } from "stream";
3030
3131import { HostClient as HostApiClient } from "@scramjet/api-client" ;
3232import { ClientUtilsCustomAgent } from "@scramjet/client-utils" ;
33- import { ManagerClient } from "@scramjet/manager-api-client" ;
3433import { RunnerConnectInfo } from "@scramjet/types/src/runner-connect" ;
3534import { writeFileSync } from "fs" ;
3635import { mapToInputDataStream , readInputStreamHeaders } from "./input-stream" ;
3736import { MessageUtils } from "./message-utils" ;
3837import { RunnerAppContext , RunnerProxy } from "./runner-app-context" ;
3938
40- process . once ( "beforeExit" , ( code ) => {
41- const filepath = `/tmp/runner-${ process . pid . toString ( ) } ` ;
42-
43- writeFileSync ( filepath , code . toString ( ) ) ;
44-
45- // eslint-disable-next-line no-console
46- console . log ( "Runner exit" ) ;
47- } ) ;
48-
4939// async function flushStream(source: Readable | undefined, target: Writable) {
5040// if (!source) return;
5141
@@ -74,26 +64,10 @@ export function isSynchronousStreamable(obj: SynchronousStreamable<any> | Primit
7464
7565const overrideMap : Map < Writable , OverrideConfig > = new Map ( ) ;
7666
77- function revertStandardStream ( oldStream : Writable ) {
78- if ( overrideMap . has ( oldStream ) ) {
79- const { write, drainCb, errorCb } = overrideMap . get ( oldStream ) as OverrideConfig ;
80-
81- // @ts -ignore - this is ok, we're doing this on purpose!
82- delete oldStream . write ;
83-
84- // if prototypic write is there, then no change needed
85- if ( oldStream . write !== write )
86- oldStream . write = write ;
87-
88- oldStream . off ( "drain" , drainCb ) ;
89- oldStream . off ( "error" , errorCb ) ;
90- overrideMap . delete ( oldStream ) ;
91- }
92- }
93-
9467function overrideStandardStream ( oldStream : Writable , newStream : Writable ) {
9568 if ( overrideMap . has ( oldStream ) ) {
9669 //throw new Error("Attempt to override stream more than once");
70+ // eslint-disable-next-line no-use-before-define
9771 revertStandardStream ( oldStream ) ;
9872 }
9973
@@ -115,6 +89,23 @@ function overrideStandardStream(oldStream: Writable, newStream: Writable) {
11589 overrideMap . set ( oldStream , { write, drainCb, errorCb } ) ;
11690}
11791
92+ function revertStandardStream ( oldStream : Writable ) {
93+ if ( overrideMap . has ( oldStream ) ) {
94+ const { write, drainCb, errorCb } = overrideMap . get ( oldStream ) as OverrideConfig ;
95+
96+ // @ts -ignore - this is ok, we're doing this on purpose!
97+ delete oldStream . write ;
98+
99+ // if prototypic write is there, then no change needed
100+ if ( oldStream . write !== write )
101+ oldStream . write = write ;
102+
103+ oldStream . off ( "drain" , drainCb ) ;
104+ oldStream . off ( "error" , errorCb ) ;
105+ overrideMap . delete ( oldStream ) ;
106+ }
107+ }
108+
118109/**
119110 * Runtime environment for sequence code.
120111 * Communicates with Host with data transferred to/from Sequence, health info,
@@ -165,11 +156,17 @@ export class Runner<X extends AppConfig> implements IComponent {
165156 throw e ;
166157 } ) ;
167158
168- this . outputDataStream = new DataStream ( { highWaterMark : 0 } ) . catch ( ( e : any ) => {
159+ this . outputDataStream = new DataStream ( ) . catch ( ( e : any ) => {
169160 this . logger . error ( "Error during input data stream" , e ) ;
170161
171162 throw e ;
172163 } ) ;
164+
165+ process . on ( "beforeExit" , ( code ) => {
166+ const filepath = `/tmp/runner-${ process . pid . toString ( ) } ` ;
167+
168+ writeFileSync ( filepath , code . toString ( ) ) ;
169+ } ) ;
173170 }
174171
175172 get context ( ) : RunnerAppContext < X , any > {
@@ -261,17 +258,17 @@ export class Runner<X extends AppConfig> implements IComponent {
261258 [ RunnerMessageCode . MONITORING , { healthy } ] , this . hostClient . monitorStream
262259 ) ;
263260
264- this . monitoringMessageReplyTimeout = setTimeout ( async ( ) => {
265- await this . handleDisconnect ( ) ;
261+ this . monitoringMessageReplyTimeout = setTimeout ( ( ) => {
262+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
263+ this . handleDisconnect ( ) ;
266264 } , 500 ) ;
267265 }
268266
269267 async handleDisconnect ( ) {
270268 this . logger . info ( "Reinitializing...." ) ;
271269
272- this . premain ( ) . catch ( ( e ) => {
273- this . logger . error ( "Premain error" , e ) ;
274- } ) ;
270+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
271+ this . premain ( ) ;
275272 }
276273
277274 async handleKillRequest ( ) : Promise < void > {
@@ -330,13 +327,9 @@ export class Runner<X extends AppConfig> implements IComponent {
330327 try {
331328 await this . hostClient . init ( this . instanceId ) ;
332329 } catch ( e ) {
333- this . logger . error ( "hostClient init error" , e ) ;
334-
335330 await defer ( 2000 ) ;
336-
337- this . premain ( ) . catch ( ( err : any ) => {
338- this . logger . error ( "Premain error" , err ) ;
339- } ) ;
331+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
332+ this . premain ( ) ;
340333 }
341334
342335 this . redirectOutputs ( ) ;
@@ -480,9 +473,6 @@ export class Runner<X extends AppConfig> implements IComponent {
480473 const hostClientUtils = new ClientUtilsCustomAgent ( "http://scramjet-host/api/v1" , this . hostClient . getAgent ( ) ) ;
481474 const hostApiClient = new HostApiClient ( "http://scramjet-host/api/v1" , hostClientUtils ) ;
482475
483- const managerClientUtils = new ClientUtilsCustomAgent ( "http://scramjet-host/api/v1/cpm/api/v1" , this . hostClient . getAgent ( ) ) ;
484- const managerApiClient = new ManagerClient ( "http://scramjet-host/api/v1/cpm/api/v1" , managerClientUtils ) ;
485-
486476 const runner : RunnerProxy = {
487477 keepAliveIssued : ( ) => this . keepAliveIssued ( ) ,
488478 sendStop : ( err ?: Error ) => {
@@ -493,13 +483,12 @@ export class Runner<X extends AppConfig> implements IComponent {
493483 } ;
494484
495485 this . _context = new RunnerAppContext (
486+ this . instanceId ,
496487 config ,
497488 this . hostClient . monitorStream ,
498489 this . emitter ,
499490 runner ,
500- hostApiClient as HostClient ,
501- managerApiClient as ManagerClient ,
502- this . instanceId
491+ hostApiClient as HostClient
503492 ) ;
504493 this . _context . logger . pipe ( this . logger ) ;
505494
@@ -612,7 +601,7 @@ export class Runner<X extends AppConfig> implements IComponent {
612601 if ( intermediate instanceof Readable ) {
613602 stream = intermediate ;
614603 } else if ( intermediate !== undefined && isSynchronousStreamable ( intermediate ) ) {
615- stream = Object . assign ( DataStream . from ( intermediate as Readable , { highWaterMark : 0 } ) , {
604+ stream = Object . assign ( DataStream . from ( intermediate as Readable ) , {
616605 topic : intermediate . topic ,
617606 contentType : intermediate . contentType
618607 } ) ;
0 commit comments