feat(cli): add start-worker command

This commit is contained in:
Ali BARIN
2022-09-07 18:35:20 +02:00
parent 72e3a69bd9
commit ec0d0203ae
5 changed files with 54 additions and 2 deletions

View File

@@ -1,2 +1,2 @@
import './config/orm';
import './workers/processor';
export { worker } from './workers/processor';

View File

@@ -4,7 +4,7 @@ import redisConfig from '../config/redis';
import Flow from '../models/flow';
import logger from '../helpers/logger';
const worker = new Worker(
export const worker = new Worker(
'processor',
async (job) => {
const flow = await Flow.query().findById(job.data.flowId).throwIfNotFound();

1
packages/backend/worker.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
export * from './dist/src/worker';

View File

@@ -0,0 +1,2 @@
/* eslint-disable */
module.exports = require('./dist/src/worker.js');

View File

@@ -0,0 +1,49 @@
import { readFileSync } from 'fs';
import { Command, Flags } from '@oclif/core';
import * as dotenv from 'dotenv';
export default class StartWorker extends Command {
static description = 'Run automatisch worker';
static flags = {
env: Flags.string({
multiple: true,
char: 'e',
}),
'env-file': Flags.string(),
}
async prepareEnvVars(): Promise<void> {
const { flags } = await this.parse(StartWorker);
if (flags['env-file']) {
const envFile = readFileSync(flags['env-file'], 'utf8');
const envConfig = dotenv.parse(envFile);
for (const key in envConfig) {
const value = envConfig[key];
process.env[key] = value;
}
}
if (flags.env) {
for (const env of flags.env) {
const [key, value] = env.split('=');
process.env[key] = value;
}
}
// must serve until more customization is introduced
delete process.env.SERVE_WEB_APP_SEPARATELY;
}
async runWorker(): Promise<void> {
await import('@automatisch/backend/worker');
}
async run(): Promise<void> {
await this.prepareEnvVars();
await this.runWorker();
}
}