Skip to content

Instantly share code, notes, and snippets.

@hmarcelodn
Last active July 7, 2022 16:32
Show Gist options
  • Save hmarcelodn/24e1fbf2cb08a307f95c1b8da852595d to your computer and use it in GitHub Desktop.
Save hmarcelodn/24e1fbf2cb08a307f95c1b8da852595d to your computer and use it in GitHub Desktop.
index.ts
/* eslint-disable no-unused-vars */
import 'dotenv/config';
import throng from 'throng';
import { APP_LOGGER } from './utils/logger';
enum ProcessType {
WEB = 'web',
DOCUMENT_DRAFT_PROCESSOR_WORKER = 'document-draft-processor-worker',
}
(async () => {
const processType = process.env.PROCESS_TYPE;
const master = async () => {
APP_LOGGER.info(`🦭 Started master process for ${processType}`);
};
const worker = async (workerId: number, disconnect: any) => {
APP_LOGGER.info(`🦭 Started worker ${workerId} process for ${processType}`);
if (process.env.PROCESS_TYPE === ProcessType.WEB) {
require('./web/server');
} else if (process.env.PROCESS_TYPE === ProcessType.DOCUMENT_DRAFT_PROCESSOR_WORKER) {
require('./workers/document-draft-processor-worker/worker');
} else {
throw new Error(`${processType} is an unsupported process type.`);
}
const shutdown = async () => {
APP_LOGGER.info('Gracefully stopping process.');
disconnect();
process.exit();
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
};
throng({
master,
worker,
count: Number(process.env.WEB_CONCURRENCY) || 1,
lifetime: Infinity,
signals: ['SIGTERM', 'SIGINT'],
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment