Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2293 from botpress/ya-mt
fix(core): added multi-thread support for heavy tasks
- Loading branch information
Showing
11 changed files
with
138 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sdk from 'botpress/sdk' | ||
import cluster from 'cluster' | ||
import yn from 'yn' | ||
|
||
const debug = DEBUG('cluster') | ||
|
||
const msgHandlers: { [messageType: string]: (message: any, worker: cluster.Worker) => void } = {} | ||
|
||
/** | ||
* The master process handles training and rebooting the server. | ||
* The worker process runs the actual server | ||
* | ||
* Exit code 0: Success (not respawn workers) | ||
* Exit code 1: Error (will try to respawn workers) | ||
*/ | ||
export const registerMsgHandler = (messageType: string, handler: (message: any, worker: cluster.Worker) => void) => { | ||
msgHandlers[messageType] = handler | ||
} | ||
|
||
export const setupMasterNode = (logger: sdk.Logger) => { | ||
registerMsgHandler('reboot_server', (message, worker) => { | ||
logger.warn(`Restarting server...`) | ||
worker.disconnect() | ||
worker.kill() | ||
}) | ||
|
||
cluster.on('exit', (worker, code, signal) => { | ||
debug(`Process exiting %o`, { workerId: worker.id, code, signal }) | ||
if (code === 0) { | ||
process.exit(0) | ||
} | ||
|
||
if (!yn(process.core_env.BP_DISABLE_AUTO_RESTART)) { | ||
cluster.fork() | ||
} | ||
}) | ||
|
||
cluster.on('message', (worker: cluster.Worker, message: any) => { | ||
const handler = msgHandlers[message.type] | ||
if (!handler) { | ||
return logger.error(`No handler configured for ${message.type}`) | ||
} | ||
|
||
try { | ||
handler(message, worker) | ||
} catch (err) { | ||
logger.attachError(err).error(`Error while processing worker message ${message.type}`) | ||
} | ||
}) | ||
|
||
cluster.fork() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters