Steve Jobs
The Simple Jobs Queue That Just Works
Run scheduled tasks with Steve Jobs, the simple jobs queue made just for Meteor. With tight MongoDB integration and fiber-independent async timing functions, this package is quick, reliable, and effortless to use.
• Jobs run on one server at a time • Jobs run predictably and consecutively • Jobs, their history, and returned data are stored in MongoDB • Failed jobs are retried on server restart • No third-party dependencies • Fully async/await compatible for Meteor 3.0
The new 5.0 introduces async/await support to work seamlessly with Meteor 3.0+.! It can run hundreds of jobs in seconds with minimal CPU impact, making it a reasonable choice for many applications. To get started, check out the documentation and the quick start below.
Special thanks to (haryadel)[https://github.com/harryadel] and (StorytellerCZ)[https://github.com/StorytellerCZ] for the migration to Meteor 3.0
Quick Start
First, install the package, and import if necessary:
meteor add msavin:sjobs
1import { Jobs } from 'meteor/msavin:sjobs'
Then, write your background jobs like you would write your methods:
1await Jobs.register({ 2 "sendReminder": async function (to, message) { 3 const instance = this; 4 5 try { 6 const call = await HTTP.callAsync('PUT', "http://www.magic.com/email/send", { 7 data: { 8 to: to, 9 message: message, 10 subject: "You've Got Mail!" 11 } 12 }); 13 14 // Check if response is not 200 15 if (call.statusCode !== 200) { 16 await instance.reschedule({ 17 in: { 18 minutes: 5 19 } 20 }); 21 } else { 22 // Return response data if successful 23 return call.data; 24 } 25 26 } catch (e) { 27 // Handle error (e.g., network issue) 28 await instance.reschedule({ 29 in: { 30 minutes: 5 31 } 32 }); 33 console.error("Failed to send email. Rescheduling job:", e); 34 } 35 } 36});
Finally, schedule a background job like you would call a method:
1await Jobs.run("sendReminder", "jony@apple.com", "The future is here!");
One more thing: the function above will schedule the job to run on the moment that the function was called, however, you can delay it by passing in a special configuration object at the end:
1await Jobs.run("sendReminder", "jony@apple.com", "The future is here!", { 2 in: { 3 days: 3, 4 }, 5 on: { 6 hour: 9, 7 minute: 42 8 }, 9 priority: 9999999999 10});
The configuration object supports date
, in
, on
, priority
, singular
, unique
, and data
, all of which are completely optional. For more information, see the Jobs.run
documentation.
Repeating Jobs
Compared to a CRON Job, the Steve Jobs package gives you much more control over how and when the job runs. To get started, you just need to create a job that replicates itself.
1await Jobs.register({ 2 "syncData": async function () { 3 const instance = this; 4 const call = await HTTP.putAsync("http://www.magic.com/syncData"); 5 6 if (call.statusCode === 200) { 7 await instance.replicate({ 8 in: { 9 hours: 1 10 } 11 }); 12 13 // to save storage, you can remove the document 14 await instance.remove(); 15 } else { 16 await instance.reschedule({ 17 in: { 18 minutes: 5 19 } 20 }); 21 } 22 } 23});
Then, you need to "kickstart" the queue by creating the first job to run. By using the singular flag, you can ensure that Meteor will only create the job if there is no pending or failed instance of it.
1Meteor.startup(async function () { 2 await Jobs.run("syncData", { 3 singular: true 4 }) 5})
Documentation
Jobs.register
and Jobs.run
are all you need to get started, but that's only the beginning of what the package can do. To explore the rest of the functionality, jump into the documentation:
- Jobs.configure
- Jobs.register
- Jobs.run
- Jobs.execute
- Jobs.reschedule
- Jobs.replicate
- Jobs.start
- Jobs.stop
- Jobs.get
- Jobs.cancel
- Jobs.clear
- Jobs.remove
- Jobs.collection
Steve Jobs is an MIT-licensed project, brought to you by Meteor Candy.