Skip to content

Commit

Permalink
fix: correct waitForCompletion behavior (#924)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the Title above
(following the Conventional Commits standard) -->
<!-- More infos: https://www.conventionalcommits.org -->
<!-- Commit types:
https://github.com/insurgent-lab/conventional-changelog-preset#commit-types-->

## Description

<!--- Describe your changes in detail -->
fixes #923

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->
#923

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->
it makes the new feature actually work

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
modified test case added by #894

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [ ] If my change introduces a breaking change, I have added a `!`
after the type/scope in the title (see the Conventional Commits
standard).
  • Loading branch information
intcreator authored Dec 12, 2024
1 parent f4e9e02 commit f6270f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
}

async fireOnTick() {
if (!this.waitForCompletion && this._isCallbackRunning) return;
// skip job if previous callback is still running
if (this.waitForCompletion && this._isCallbackRunning) return;

this._isCallbackRunning = true;

Expand Down
5 changes: 4 additions & 1 deletion tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,12 @@ describe('cron', () => {
it('should track multiple running jobs correctly', async () => {
const clock = sinon.useFakeTimers();
const executionOrder: number[] = [];
let started = 0;

const job = new CronJob(
'* * * * * *',
async () => {
started++;
await new Promise<void>(resolve => {
setTimeout(() => {
callback();
Expand All @@ -1256,7 +1258,8 @@ describe('cron', () => {

await clock.tickAsync(3500);

expect(executionOrder).toEqual([1, 2]);
expect(started).toBe(2);
expect(executionOrder).toEqual([1]);
job.stop();
});

Expand Down

0 comments on commit f6270f8

Please sign in to comment.