Skip to content

Commit

Permalink
refactor: allow passing in fork mode options
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed May 10, 2022
1 parent b61b0e5 commit 3853263
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ export class Job<DATA = unknown | void> {
*/
async save(): Promise<Job> {
if (this.agenda.forkedWorker) {
console.warn('calling save() on a Job during a forkedWorker has no effect!');
const warning = new Error('calling save() on a Job during a forkedWorker has no effect!');
console.warn(warning.message, warning.stack);
return this as Job;
}
// ensure db connection is ready
Expand Down Expand Up @@ -359,7 +360,10 @@ export class Job<DATA = unknown | void> {
this.agenda.emit(`start:${this.attrs.name}`, this);
log('[%s:%s] starting job', this.attrs.name, this.attrs._id);

if (this.attrs.fork && this.agenda.forkHelper) {
if (this.attrs.fork) {
if (!this.agenda.forkHelper) {
throw new Error('no forkHelper specified, you need to set a path to a helper script');
}
const { forkHelper } = this.agenda;
const location =
Job.functionLocationCache[this.attrs.name] ||
Expand All @@ -375,9 +379,14 @@ export class Job<DATA = unknown | void> {

await new Promise<void>((resolve, reject) => {
let stillRunning = true;
const child = fork(forkHelper, [this.attrs.name, this.attrs._id!.toString(), location], {
signal
});
const child = fork(
forkHelper.path,
[this.attrs.name, this.attrs._id!.toString(), location],
{
signal,
...forkHelper.options
}
);

child.on('close', code => {
console.log(`child process exited with code ${code}`);
Expand Down
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as debug from 'debug';

import type { Db, Filter, MongoClientOptions, Sort } from 'mongodb';
import { SortDirection } from 'mongodb';
import { ForkOptions } from 'child_process';
import type { IJobDefinition } from './types/JobDefinition';
import type { IAgendaConfig } from './types/AgendaConfig';
import type { IDatabaseOptions, IDbConfig, IMongoOptions } from './types/DbOptions';
Expand All @@ -24,7 +25,7 @@ const DefaultOptions = {
lockLimit: 0,
defaultLockLifetime: 10 * 60 * 1000,
sort: { nextRunAt: 1, priority: -1 } as const,
forkHelper: 'dist/childWorker.js'
forkHelper: { path: 'dist/childWorker.js' }
};

/**
Expand All @@ -35,7 +36,10 @@ export class Agenda extends EventEmitter {

public readonly forkedWorker?: boolean;

public readonly forkHelper?: string;
public readonly forkHelper?: {
path: string;
options?: ForkOptions;
};

db: JobDbRepository;

Expand All @@ -51,8 +55,9 @@ export class Agenda extends EventEmitter {
on(event: 'ready', listener: () => void): this;
on(event: 'error', listener: (error: Error) => void): this;
on(event: string, listener: (...args) => void): this {
if (this.forkedWorker) {
console.warn('calling on() during a forkedWorker has no effect!');
if (this.forkedWorker && event !== 'ready') {
const warning = new Error('calling on() during a forkedWorker has no effect!');
console.warn(warning.message, warning.stack);
return this;
}
return super.on(event, listener);
Expand Down Expand Up @@ -101,7 +106,10 @@ export class Agenda extends EventEmitter {
defaultLockLifetime?: number;
// eslint-disable-next-line @typescript-eslint/ban-types
} & (IDatabaseOptions | IMongoOptions | {}) &
IDbConfig & { forkHelper?: string; forkedWorker?: boolean } = DefaultOptions,
IDbConfig & {
forkHelper?: { path: string; options?: ForkOptions };
forkedWorker?: boolean;
} = DefaultOptions,
cb?: (error?: Error) => void
) {
super();
Expand Down

0 comments on commit 3853263

Please sign in to comment.