Skip to content

Commit

Permalink
fix: use different appraoch to find definition file
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed May 11, 2022
1 parent 83a5240 commit 9d4c60e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
"debug": "~4.3.4",
"human-interval": "~2.0.1",
"luxon": "^2.3.1",
"mongodb": "^4.3.1",
"get-function-location": "^2.0.0"
"mongodb": "^4.3.1"
},
"devDependencies": {
"eslint": "^8.11.0",
Expand Down
20 changes: 6 additions & 14 deletions src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as date from 'date.js';
import * as debug from 'debug';
import { ObjectId } from 'mongodb';
import { fork } from 'child_process';
import * as getFunctionLocation from 'get-function-location';
import type { Agenda } from './index';
import type { DefinitionProcessor } from './types/JobDefinition';
import { IJobParameters, datefields, TJobDatefield } from './types/JobParameters';
Expand All @@ -17,8 +16,6 @@ const log = debug('agenda:job');
export class Job<DATA = unknown | void> {
readonly attrs: IJobParameters<DATA>;

static functionLocationCache: { [key: string]: string } = {};

/** this flag is set to true, if a job got canceled (e.g. due to a timeout or other exception),
* you can use it for long running tasks to periodically check if canceled is true,
* also touch will check if and throws that the job got canceled
Expand Down Expand Up @@ -362,16 +359,7 @@ export class Job<DATA = unknown | void> {
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] ||
(await getFunctionLocation(this.agenda.definitions[this.attrs.name].fn)).source.replace(
/^file:\/\//,
''
);

if (!Job.functionLocationCache[this.attrs.name]) {
Job.functionLocationCache[this.attrs.name] = location;
}
// console.log('location', location);
let controller: AbortController | undefined;
let signal: AbortSignal | undefined;
Expand All @@ -387,7 +375,11 @@ export class Job<DATA = unknown | void> {

const child = fork(
forkHelper.path,
[this.attrs.name, this.attrs._id!.toString(), location],
[
this.attrs.name,
this.attrs._id!.toString(),
this.agenda.definitions[this.attrs.name].filePath || ''
],
{
...forkHelper.options,
signal
Expand All @@ -402,7 +394,7 @@ export class Job<DATA = unknown | void> {
forkHelper,
this.attrs.name,
this.attrs._id,
location
this.agenda.definitions[this.attrs.name].filePath
);
const error = new Error(`child process exited with code: ${code}`);
console.warn(error.message);
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { JobDbRepository } from './JobDbRepository';
import { JobPriority, parsePriority } from './utils/priority';
import { JobProcessor } from './JobProcessor';
import { calculateProcessEvery } from './utils/processEvery';
import {getCallerFilePath} from "./utils/stack";

const log = debug('agenda');

Expand Down Expand Up @@ -337,8 +338,12 @@ export class Agenda extends EventEmitter {
if (this.definitions[name]) {
log('overwriting already defined agenda job', name);
}
this.definitions[name] = {

const filePath = getCallerFilePath();

this.definitions[name] = {
fn: processor,
filePath,
concurrency: options?.concurrency || this.attrs.defaultConcurrency,
lockLimit: options?.lockLimit || this.attrs.defaultLockLimit,
priority: parsePriority(options?.priority),
Expand Down
1 change: 1 addition & 0 deletions src/types/JobDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IJobDefinition<DATA = unknown> {
/** how many jobs of this kind can run in parallel/simultanously per Agenda instance */
concurrency?: number;

filePath: string | undefined;
fn: DefinitionProcessor<DATA, void | ((error?: Error) => void)>;
}

Expand Down
21 changes: 21 additions & 0 deletions src/utils/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function getCallerFilePath(position = 2): string | undefined {
if (position >= Error.stackTraceLimit) {
throw new TypeError(
`getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: \`${position}\` and Error.stackTraceLimit was: \`${Error.stackTraceLimit}\``
);
}

const oldPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
// eslint-disable-next-line unicorn/error-message
const { stack } = new Error();
Error.prepareStackTrace = oldPrepareStackTrace;

if (stack !== null && typeof stack === 'object') {
// stack[0] holds this file
// stack[1] holds where this function was called
// stack[2] holds the file we're interested in
return stack[position] ? (stack[position] as any).getFileName() : undefined;
}
return undefined;
}

0 comments on commit 9d4c60e

Please sign in to comment.