-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow running/sandboxing message handlers using worker threads
- Loading branch information
Showing
20 changed files
with
426 additions
and
217 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/lib/consumer/message-handler/consume-message-worker-thread.ts
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,138 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { | ||
isMainThread, | ||
parentPort, | ||
workerData, // type-coverage:ignore-line | ||
MessagePort, | ||
} from 'worker_threads'; | ||
import { | ||
IMessageTransferable, | ||
TConsumerMessageHandlerFn, | ||
} from '../../../../types'; | ||
|
||
export enum EWorkerThreadMessageCodeExit { | ||
WORKER_DATA_REQUIRED = 100, | ||
INVALID_HANDLER_TYPE, | ||
HANDLER_IMPORT_ERROR, | ||
UNCAUGHT_EXCEPTION, | ||
TERMINATED, | ||
} | ||
|
||
export enum EWorkerThreadMessageCodeConsume { | ||
OK = 200, | ||
MESSAGE_PROCESSING_ERROR, | ||
MESSAGE_PROCESSING_CAUGHT_ERROR, | ||
} | ||
|
||
export type TWorkerThreadMessageCode = | ||
| EWorkerThreadMessageCodeExit | ||
| EWorkerThreadMessageCodeConsume; | ||
|
||
export type TWorkerThreadError = { | ||
name: string; | ||
message: string; | ||
}; | ||
|
||
export type TWorkerThreadMessage = { | ||
code: TWorkerThreadMessageCode; | ||
error: TWorkerThreadError | null; | ||
}; | ||
|
||
function getHandlerFn( | ||
filename: string, | ||
cb: (fn: TConsumerMessageHandlerFn) => void, | ||
) { | ||
import(filename) | ||
.then( | ||
( | ||
importedModule: | ||
| { default?: TConsumerMessageHandlerFn } | ||
| TConsumerMessageHandlerFn, | ||
) => { | ||
const fn = | ||
typeof importedModule !== 'function' && importedModule.default | ||
? importedModule.default | ||
: importedModule; | ||
if (typeof fn !== 'function') { | ||
exit(EWorkerThreadMessageCodeExit.INVALID_HANDLER_TYPE); | ||
} else cb(fn); | ||
}, | ||
) | ||
.catch((err: unknown) => { | ||
console.error(err); | ||
exit(EWorkerThreadMessageCodeExit.HANDLER_IMPORT_ERROR); | ||
}); | ||
} | ||
|
||
function formatMessage( | ||
code: TWorkerThreadMessageCode, | ||
err?: unknown, | ||
): TWorkerThreadMessage { | ||
const error = | ||
err && err instanceof Error | ||
? { name: err.name, message: err.message } | ||
: null; | ||
return { | ||
code, | ||
error, | ||
}; | ||
} | ||
|
||
function postMessage( | ||
messagePort: MessagePort, | ||
code: TWorkerThreadMessageCode, | ||
err?: unknown, | ||
) { | ||
const msg = formatMessage(code, err); | ||
messagePort.postMessage(msg); | ||
} | ||
|
||
function exit(code: TWorkerThreadMessageCode, err?: unknown) { | ||
parentPort && postMessage(parentPort, code, err); | ||
process.exit(code); | ||
} | ||
|
||
if (!isMainThread && parentPort) { | ||
const messagePort: MessagePort = parentPort; | ||
|
||
// type-coverage:ignore-next-line | ||
if (!workerData) { | ||
exit(EWorkerThreadMessageCodeExit.WORKER_DATA_REQUIRED); | ||
} | ||
|
||
getHandlerFn(workerData, (handlerFn) => { | ||
messagePort.on('message', (msg: IMessageTransferable) => { | ||
try { | ||
handlerFn(msg, (err) => { | ||
if (err) { | ||
postMessage( | ||
messagePort, | ||
EWorkerThreadMessageCodeConsume.MESSAGE_PROCESSING_ERROR, | ||
err, | ||
); | ||
} else { | ||
postMessage(messagePort, EWorkerThreadMessageCodeConsume.OK); | ||
} | ||
}); | ||
} catch (err: unknown) { | ||
postMessage( | ||
messagePort, | ||
EWorkerThreadMessageCodeConsume.MESSAGE_PROCESSING_CAUGHT_ERROR, | ||
err, | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
process.on('uncaughtException', (err) => { | ||
exit(EWorkerThreadMessageCodeExit.UNCAUGHT_EXCEPTION, err); | ||
}); | ||
} |
91 changes: 91 additions & 0 deletions
91
src/lib/consumer/message-handler/consume-message-worker.ts
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,91 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import path from 'path'; | ||
import { Worker } from 'worker_threads'; | ||
import { ICallback } from 'redis-smq-common'; | ||
import { IMessageParams } from '../../../../types'; | ||
import { | ||
EWorkerThreadMessageCodeExit, | ||
EWorkerThreadMessageCodeConsume, | ||
TWorkerThreadMessage, | ||
} from './consume-message-worker-thread'; | ||
import { ConsumerMessageHandlerWorkerError } from './errors'; | ||
|
||
export class ConsumeMessageWorker { | ||
protected messageHandlerFilename; | ||
protected messageHandlerThread: Worker | null = null; | ||
|
||
constructor(messageHandlerFilename: string) { | ||
this.messageHandlerFilename = messageHandlerFilename; | ||
} | ||
|
||
protected getMessageHandlerThread(): Worker { | ||
if (!this.messageHandlerThread) { | ||
this.messageHandlerThread = new Worker( | ||
path.resolve(__dirname, './consume-message-worker-thread.js'), | ||
{ | ||
workerData: this.messageHandlerFilename, | ||
}, | ||
); | ||
this.messageHandlerThread.on('messageerror', (err) => { | ||
console.error(err); | ||
}); | ||
this.messageHandlerThread.on('error', (err) => { | ||
console.error(err); | ||
}); | ||
this.messageHandlerThread.on('exit', () => { | ||
this.messageHandlerThread = null; | ||
}); | ||
} | ||
return this.messageHandlerThread; | ||
} | ||
|
||
consume(message: IMessageParams, cb: ICallback<void>): void { | ||
const worker = this.getMessageHandlerThread(); | ||
|
||
const cleanUp = () => { | ||
worker | ||
.removeListener('message', onMessage) | ||
.removeListener('exit', onExit); | ||
}; | ||
|
||
const onMessage = (msg: TWorkerThreadMessage) => { | ||
cleanUp(); | ||
if (msg.code !== EWorkerThreadMessageCodeConsume.OK) { | ||
console.error(`ConsumerMessageHandlerWorkerError`, msg); | ||
cb(new ConsumerMessageHandlerWorkerError(msg)); | ||
} else cb(); | ||
}; | ||
|
||
const onExit = () => { | ||
cleanUp(); | ||
const msg = { | ||
code: EWorkerThreadMessageCodeExit.TERMINATED, | ||
error: null, | ||
}; | ||
console.error('ConsumerMessageHandlerWorkerError', msg); | ||
cb(new ConsumerMessageHandlerWorkerError(msg)); | ||
}; | ||
|
||
worker.once('message', onMessage); | ||
worker.once('exit', onExit); | ||
worker.postMessage(message); | ||
} | ||
|
||
quit(cb: ICallback<void>) { | ||
const callback = () => { | ||
this.messageHandlerThread = null; | ||
cb(); | ||
}; | ||
if (this.messageHandlerThread) { | ||
this.messageHandlerThread.terminate().then(callback).catch(callback); | ||
} else cb(); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/lib/consumer/message-handler/errors/consumer-message-handler-worker.error.ts
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,22 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { TWorkerThreadMessage } from '../consume-message-worker-thread'; | ||
import { ConsumerMessageHandlerError } from './consumer-message-handler.error'; | ||
|
||
export class ConsumerMessageHandlerWorkerError extends ConsumerMessageHandlerError { | ||
constructor(msg: TWorkerThreadMessage) { | ||
const { code, error } = msg; | ||
super( | ||
`Error code: ${code}.${ | ||
error ? `Message: ${error.name}: ${error.message}` : '' | ||
}`, | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/lib/consumer/message-handler/errors/consumer-message-handler.error.ts
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,16 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
import { ConsumerError } from '../../errors'; | ||
|
||
export class ConsumerMessageHandlerError extends ConsumerError { | ||
constructor(msg?: string) { | ||
super(msg); | ||
} | ||
} |
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,11 @@ | ||
/* | ||
* Copyright (c) | ||
* Weyoss <weyoss@protonmail.com> | ||
* https://github.com/weyoss | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file | ||
* in the root directory of this source tree. | ||
*/ | ||
|
||
export { ConsumerMessageHandlerError } from './consumer-message-handler.error'; | ||
export { ConsumerMessageHandlerWorkerError } from './consumer-message-handler-worker.error'; |
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
Oops, something went wrong.