Skip to content

Commit ee54dd5

Browse files
authored
fix: run once when actual date is given to setTime (#740)
1 parent 6d94742 commit ee54dd5

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/job.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
1616
running = false;
1717
unrefTimeout = false;
1818
lastExecution: Date | null = null;
19-
runOnce = false;
2019
context: CronContext<C>;
2120
onComplete?: WithOnComplete<OC> extends true
2221
? CronOnCompleteCallback
@@ -84,10 +83,6 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
8483
) as WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined;
8584
}
8685

87-
if (this.cronTime.realDate) {
88-
this.runOnce = true;
89-
}
90-
9186
this.addCallback(this._fnWrap(onTick));
9287

9388
if (runOnInit) {
@@ -181,7 +176,9 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
181176
}
182177
const wasRunning = this.running;
183178
this.stop();
179+
184180
this.cronTime = time;
181+
185182
if (wasRunning) this.start();
186183
}
187184

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

260257
// start before calling back so the callbacks have the ability to stop the cron job
261-
if (!this.runOnce) {
258+
if (!this.cronTime.realDate) {
262259
this.start();
263260
}
264261

src/time.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ export class CronTime {
684684
return true;
685685
}
686686

687-
/*
687+
/**
688688
* Parse the cron syntax into something useful for selecting the next execution time.
689689
*
690690
* Algorithm:
@@ -733,7 +733,7 @@ export class CronTime {
733733
}
734734
}
735735

736-
/*
736+
/**
737737
* Parse individual field from the cron syntax provided.
738738
*
739739
* Algorithm:

tests/cron.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,29 @@ describe('cron', () => {
10801080
job.stop();
10811081
expect(callback).toHaveBeenCalledTimes(1);
10821082
});
1083+
1084+
it('should create recurring job, setTime with actual date, start and run once (#739)', () => {
1085+
const callback = jest.fn();
1086+
const clock = sinon.useFakeTimers();
1087+
1088+
const job = new CronJob('0 0 20 * * *', callback);
1089+
1090+
const startDate = new Date(Date.now() + 5000);
1091+
job.setTime(new CronTime(startDate));
1092+
1093+
job.start();
1094+
1095+
clock.tick(5000);
1096+
1097+
expect(callback).toHaveBeenCalledTimes(1);
1098+
1099+
clock.tick(60000);
1100+
1101+
clock.restore();
1102+
1103+
expect(callback).toHaveBeenCalledTimes(1);
1104+
expect(job.running).toBe(false);
1105+
});
10831106
});
10841107

10851108
describe('nextDate(s)', () => {

0 commit comments

Comments
 (0)