question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schedule only firing on app start

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
toymachiner62commented, Apr 1, 2019

3 reasons:

  1. B/c my code can’t be modular if i follow your suggestion.

The functions that I’m putting on a schedule with node-schedule I also want to execute via a REST endpoint

  1. 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 logic

  2. I plan to schedule many jobs (say 100). I don’t want all those functions in a single file that’s 5K lines long.

1reaction
MathieuAAcommented, Apr 2, 2019

@toymachiner62 the issue is that the code from your scheduler is wrong, it should be:

const schedule = require('node-schedule')

function scheduler () {
  schedule.scheduleJob('*/1 * * * *', doStuff) // <-- here
}

function doStuff () {
  console.log('in doStuff. The time is ', new Date())
}

module.exports = scheduler

You were passing undefined in your example instead of a function.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found