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

Job scheduled with setTime throws error instead of running #739

Closed
lpgera opened this issue Oct 21, 2023 · 2 comments · Fixed by #740
Closed

Job scheduled with setTime throws error instead of running #739

lpgera opened this issue Oct 21, 2023 · 2 comments · Fixed by #740
Labels
released type:bug Bug reports and bug fixes

Comments

@lpgera
Copy link

lpgera commented Oct 21, 2023

Description

When I reschedule a job to a specific date using the job.setTime(new CronTime(startDate)) function, the job throws an error at the scheduled time instead of executing.

Expected Behavior

I expect the job to run at startDate.

Actual Behavior

I'm getting the following error:

node_modules/cron/dist/time.js:93
                throw new Error('WARNING: Date in past. Will never be fired.');
                ^

Error: WARNING: Date in past. Will never be fired.

Possible Fix

No response

Steps to Reproduce

// create job with a default schedule
const job = new CronJob(
  '0 0 20 * * *',
  () => console.log('onTick')
)

// re-schedule for 5 seconds from now
const startDate = new Date(Date.now() + 5000)
job.setTime(new CronTime(startDate))
job.start()

console.log('current date:', new Date())
console.log('start date:', startDate)

Context

This used to be working properly in earlier versions of the library.

Your Environment

  • cron version: 3.1.3
  • NodeJS version: 20.8.1
  • Operating System and version: macOS 14.0, Debian 10.13
@sheerlox sheerlox added the type:bug Bug reports and bug fixes label Oct 21, 2023
@sheerlox sheerlox self-assigned this Oct 21, 2023
@sheerlox
Copy link
Collaborator

thanks for reporting on this issue @lpgera!

after a bit of debugging, turns out this bug is due to not updating the internal runOnce property if setTime() is passed an actual date:
image

so the callback is actually called once, but right after this it tries to execute again, causing this error.

I'm preparing a PR with the fix!

@ncb000gt
Copy link
Member

🎉 This issue has been resolved in version 3.1.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released type:bug Bug reports and bug fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants