Skip to content

Commit

Permalink
revert: Revert "fix: run once when actual date is given to setTime (k…
Browse files Browse the repository at this point in the history
…elektiv#740)"

This reverts commit ee54dd5.
  • Loading branch information
sheerlox committed Oct 29, 2023
1 parent 31c1ff3 commit f5cec42
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
9 changes: 6 additions & 3 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
running = false;
unrefTimeout = false;
lastExecution: Date | null = null;
runOnce = false;
context: CronContext<C>;
onComplete?: WithOnComplete<OC> extends true
? CronOnCompleteCallback
Expand Down Expand Up @@ -83,6 +84,10 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
) as WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined;
}

if (this.cronTime.realDate) {
this.runOnce = true;
}

this.addCallback(this._fnWrap(onTick));

if (runOnInit) {
Expand Down Expand Up @@ -176,9 +181,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
}
const wasRunning = this.running;
this.stop();

this.cronTime = time;

if (wasRunning) this.start();
}

Expand Down Expand Up @@ -255,7 +258,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
this.running = false;

// start before calling back so the callbacks have the ability to stop the cron job
if (!this.cronTime.realDate) {
if (!this.runOnce) {
this.start();
}

Expand Down
4 changes: 2 additions & 2 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ export class CronTime {
return true;
}

/**
/*
* Parse the cron syntax into something useful for selecting the next execution time.
*
* Algorithm:
Expand Down Expand Up @@ -733,7 +733,7 @@ export class CronTime {
}
}

/**
/*
* Parse individual field from the cron syntax provided.
*
* Algorithm:
Expand Down
23 changes: 0 additions & 23 deletions tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,29 +1080,6 @@ describe('cron', () => {
job.stop();
expect(callback).toHaveBeenCalledTimes(1);
});

it('should create recurring job, setTime with actual date, start and run once (#739)', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new CronJob('0 0 20 * * *', callback);

const startDate = new Date(Date.now() + 5000);
job.setTime(new CronTime(startDate));

job.start();

clock.tick(5000);

expect(callback).toHaveBeenCalledTimes(1);

clock.tick(60000);

clock.restore();

expect(callback).toHaveBeenCalledTimes(1);
expect(job.running).toBe(false);
});
});

describe('nextDate(s)', () => {
Expand Down

0 comments on commit f5cec42

Please sign in to comment.