Background jobs allow you to schedule parse server cloud code functions to be executed at periodic intervals. There is no limit on the number of times your background jobs execute.
Background jobs execute in the same container as your app but on a different thread. Background jobs share CPU and Memory resources with your app. You can scale background jobs to multiple workers. Where multiple app containers are involved, the scheduler can also be instructed on the total number of your app containers to use for running a background job.
The below steps provide a guide to configure background jobs on NodeChef
Create a file with name jobs.js. This file lives in the cloud folder and follows all the above rules of cloud code on the Parse Server
Deploy your cloud code as is using deploy --ccode. NodeChef Parse Server will detect the jobs.js file and load the code.
To schedule jobs, log into your Account. Under the Task Manager, find your app listed. Click on App Actions and select background jobs.
Schedule the job by completing the form in the background jobs dialog box.
Sample jobs.js
var cloud = require( './cloud.js' );
Parse.Cloud.job('myjob', function(request, status) {
// [request] only has the params property if set from the dashboard.
// [status] implements the following methods. success(), error() and message()
status.message('in progress');
status.success('done');
} );
status.success() or status.error() must be called in your code for the background process to exit. Not calling this function will result in a process leak which will eventually crash the app container.
After status.success() or status.error() is called, the background process running the job will exit immediately.
You can explicitly kill long running jobs from the dashboard. Navigate to App actions > Background Jobs > Jobs. Click on the name of the job and use the "try kill job" button.
Scheduling Guide
1
Run a job function now
Select the function to execute and use the 'Run Now' button to schedule the job for immediate execution.
2
Run a job once every day starting tomorrow
For this use case, consider the job runs every day at 10am and the job has to be scheduled to start executing starting tomorrow. If the job was to start executing today and the time of the day is past 10am, the job will run immediately when scheduled.
In the First Run row, select tomorrow's date and then select 10:00 from the 'At' dropdown field
In the Then after, run every row, enter 1 in the text input, select day(s) and select 10:00
3
Run every day repeating every 4 hours
In the First Run row, select 00:00 from the 'At' dropdown
In the Then after, run every row, enter 1 in the text input, select day(s) and select 00:00
In the On the day the job runs, repeat row, select All day and then enter 4 in the every textbox and select hour(s) from the dropdown field.
4
Run once every week, and then repeat the job 3 times every 3 hours on the day the job runs
For this use case, consider we want the job to run every of the week at 10am. If running on a specific day of the week is not a requirement you can simply select today's date under the First Run dropdown.
In the First Run row, select today's date and then select 10:00 from the 'At' dropdown
In the Then after, run every row, enter 1 in the text input, select week(s) and then select 10:00 from the At dropdown
In the On the day the job runs, repeat row, select Thrice, enter 3 in the every text input and then select hour(s)