Schedule only firing on app start
See original GitHub issueI have 2 files, index.js
and scheduler.js
.
index.js
const scheduler = require('./scheduler')
console.log('starting')
scheduler()
scheduler.js
const schedule = require('node-schedule')
function scheduler () {
schedule.scheduleJob('*/1 * * * *', doStuff())
}
function doStuff () {
console.log('in doStuff. The time is ', new Date())
}
module.exports = scheduler
According to https://crontab.guru/#*/1_*_*_*_*
my cron syntax should fire every minute, but it is only firing on app start and then errors the next minute when it tries to fire.
npm start
> schedule@1.0.0 start /Users/tomcaflisch/Sites/schedule
> node index.js
starting
in doStuff. My dogs name is foo. The time is 2019-03-27T23:39:16.218Z
/Users/tomcaflisch/Sites/schedule/node_modules/node-schedule/lib/schedule.js:175
this.job.execute(fireDate);
^
TypeError: Cannot read property 'execute' of undefined
at Job.invoke (/Users/tomcaflisch/Sites/schedule/node_modules/node-schedule/lib/schedule.js:175:14)
at /Users/tomcaflisch/Sites/schedule/node_modules/node-schedule/lib/schedule.js:552:11
at Timeout._onTimeout (/Users/tomcaflisch/Sites/schedule/node_modules/node-schedule/lib/schedule.js:510:7)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! schedule@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the schedule@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/tomcaflisch/.npm/_logs/2019-03-27T23_40_00_012Z-debug.log
Seems that if i don’t pass in a parameter and call schedule.scheduleJob('*/1 * * * *', doStuff)
instead of schedule.scheduleJob('*/1 * * * *', doStuff())
it works just fine.
Is there any way to pass in a function in which I pass in params as the callback in the scheduleJob
function?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
How can I execute something just once per application start?
You can clear memory in Task Manager, so all applications will be closed and then relaunch your application to make sure that your...
Read more >Schedule Zaps to run at specific intervals - Zapier Help
Typically, Zaps only run when a trigger event occurs in an app. With schedule triggers, you can choose to run your Zap every...
Read more >Schedule alarms - Android Developers
They let you fire Intents at set times and/or intervals. ... Caution: When your app schedules an exact alarm using this method, the...
Read more >How to Auto Boot up Any APP on your Firestick? - YouTube
... Also Check out my Website https://www.cwtek.co.uk Shows you how to auto start an App on your Fire TV Stick. Show less Show...
Read more >Ready For Wildfire App
Use the CAL FIRE app to create a personalized wildfire readiness plan and ... You'll start by taking a short survey that identifies...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
3 reasons:
The functions that I’m putting on a schedule with node-schedule I also want to execute via a REST endpoint
I want to be able to write unit tests for my functions. By embedding them inside
schedule.scheduleJob
It makes it much harder to unit test just the logicI plan to schedule many jobs (say 100). I don’t want all those functions in a single file that’s 5K lines long.
@toymachiner62 the issue is that the code from your scheduler is wrong, it should be:
You were passing
undefined
in your example instead of a function.