Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a recurring timer instead of an interval for processJobs. #4

Open
wants to merge 1 commit into
base: Admithub-v4.3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/agenda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Agenda extends EventEmitter {
_mdb!: MongoDb;
_collection!: Collection;
_nextScanAt: any;
_processInterval: any;
_processTimer: any;

cancel!: typeof cancel;
close!: typeof close;
Expand Down
6 changes: 3 additions & 3 deletions lib/agenda/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const debug = createDebugger("agenda:start");
* @returns resolves if db set beforehand, returns undefined otherwise
*/
export const start = async function (this: Agenda): Promise<void | unknown> {
if (this._processInterval) {
if (this._processTimer) {
debug("Agenda.start was already called, ignoring");
return this._ready;
}
Expand All @@ -22,8 +22,8 @@ export const start = async function (this: Agenda): Promise<void | unknown> {
"Agenda.start called, creating interval to call processJobs every [%dms]",
this._processEvery
);
this._processInterval = setInterval(
processJobs.bind(this),
this._processTimer = setTimer(
processJobsOnTimer.bind(this),
this._processEvery
);
process.nextTick(processJobs.bind(this));
Expand Down
4 changes: 2 additions & 2 deletions lib/agenda/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const stop = async function (this: Agenda): Promise<void> {
};

debug("Agenda.stop called, clearing interval for processJobs()");
clearInterval(this._processInterval);
this._processInterval = undefined;
clearTimeout(this._processTimeout);
this._processTimeout = undefined;
return _unlockJobs();
};
38 changes: 35 additions & 3 deletions lib/utils/process-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ import { Agenda } from "../agenda";

const debug = createDebugger("agenda:internal:processJobs");

/**
* Timer to run process methods for jobs
*
* Use this rather than setInterval to ensure that at most one processJobs is running at a time.
*/

export const processJobsOnTimer = async function (
this: Agenda,
): any {
// NOTE: There is the potential for a bit of drift if this job runs late.
// But it's internal to the process, so likely it's reasonable. We could
// store a time on `this` and add 5 seconds every time, but this is simpler
// and less invasive.
// TODO: Is this right in typescript?
const lastRun = new Date();

// Actually check for jobs to run
processJobs().bind(this)

// Compute the remaining time for the next run, subtracting out the time that passed since we started.
// Unless we already missed it; then schedule it right now.
const delayToNextRun = this._processEvery - ((new Date()) - lastRun);
if (delayToNextRun < 0)
delayToNextRun = 0;

// And schedule ourselves to run again at the appropriate time.
this._processTimer = setTimer(
processJobsOnTimer.bind(this),
this._processEvery
);
}

/**
* Process methods for jobs
* @param {Job} extraJob job to run immediately
Expand All @@ -20,8 +52,8 @@ export const processJobs = async function (
);
// Make sure an interval has actually been set
// Prevents race condition with 'Agenda.stop' and already scheduled run
if (!this._processInterval) {
debug("no _processInterval set when calling processJobs, returning");
if (!this._processTimer) {
debug("no _processTimer set when calling processJobs, returning");
return;
}

Expand Down Expand Up @@ -283,7 +315,7 @@ export const processJobs = async function (
* @returns {undefined}
*/
function runOrRetry() {
if (self._processInterval) {
if (self._processTimer) {
// @todo: We should check if job exists
const job = jobQueue.pop()!;
const jobDefinition = definitions[job.attrs.name];
Expand Down