Skip to content

Commit

Permalink
fix: move restartTimeout to class options
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Jul 25, 2022
1 parent 73057ac commit dd5703e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface ThreadedClassConfig {
threadId?: string
/** If the process crashes or freezes it's automatically restarted. (ThreadedClassManager will emit the "restarted" event upon restart) */
autoRestart?: boolean
/** If the process needs to restart, how long to wait for it to initalize, before failing. (default is 1000ms) */
restartTimeout?: number
/** Set to true to disable multi-threading, this might be useful when you want to disable multi-threading but keep the interface unchanged. */
disableMultithreading?: boolean
/** Set path to worker, used in browser */
Expand Down
16 changes: 5 additions & 11 deletions src/parent-process/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
import {
InitProps,
DEFAULT_CHILD_FREEZE_TIME,
DEFAULT_RESTART_TIMEOUT,
encodeArguments,
CallbackFunction,
Message,
Expand Down Expand Up @@ -47,14 +48,6 @@ export class ThreadedClassManagerClass {
return this._internal.handleExit
}

/** How quickly to time out when restarting the threads. Default is 1000ms. */
public set restartTimeout (v: number) {
this._internal.restartTimeout = v
}
public get restartTimeout (): number {
return this._internal.restartTimeout
}

/** Destroy a proxy class */
public destroy (proxy: ThreadedClass<any>): Promise<void> {
return this._internal.killProxy(proxy)
Expand Down Expand Up @@ -166,7 +159,6 @@ export class ThreadedClassManagerClassInternal extends EventEmitter {

/** Set to true if you want to handle the exiting of child process yourselt */
public handleExit = RegisterExitHandlers.AUTO
public restartTimeout = 1000
private isInitialized: boolean = false
private _threadId: number = 0
private _instanceId: number = 0
Expand Down Expand Up @@ -425,6 +417,8 @@ export class ThreadedClassManagerClassInternal extends EventEmitter {
await this.killChild(child, true)
}

const restartTimeout = child.config.restartTimeout ?? DEFAULT_RESTART_TIMEOUT

if (!child.alive) {
// clear old process:
child.process.removeAllListeners()
Expand All @@ -450,9 +444,9 @@ export class ThreadedClassManagerClassInternal extends EventEmitter {
}
this.on('initialized', onInit)
setTimeout(() => {
reject(`Timeout when trying to restart after ${this.restartTimeout}`)
reject(`Timeout when trying to restart after ${restartTimeout}`)
this.removeListener('initialized', onInit)
}, this.restartTimeout)
}, restartTimeout)
})
const promises: Array<Promise<void>> = []

Expand Down
1 change: 1 addition & 0 deletions src/shared/sharedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ThreadedClassConfig } from '../api'
// This file contains definitions for the API between the child and parent process.

export const DEFAULT_CHILD_FREEZE_TIME = 1000 // how long to wait before considering a child to be unresponsive
export const DEFAULT_RESTART_TIMEOUT = 1000 // how long to wait for the child to come back after restart

export type InitProps = Array<InitProp>
export enum InitPropType {
Expand Down

0 comments on commit dd5703e

Please sign in to comment.