Skip to content

Instantly share code, notes, and snippets.

@gimdongwoo
Last active July 12, 2020 14:48
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save gimdongwoo/326133fc6a471740a5ee73a3565e1b42 to your computer and use it in GitHub Desktop.
Save gimdongwoo/326133fc6a471740a5ee73a3565e1b42 to your computer and use it in GitHub Desktop.
Background Job in Parse Server (node/cron)
// src/CloudCode.js
"use strict"
var Cron = require('./Cron');
var Job = require('./Job');
class CloudCode {
constructor(Parse, timezone){
this.Parse = Parse;
this.cron = new Cron(timezone);
this.job = new Job(this.Parse);
}
putJob(name, req){
this.job.put(name, req);
return this;
}
addCron(name, timeFormat){
this.cron.addJob(timeFormat, this.job.get(name));
return this;
}
start(){
this.cron.on();
}
stop(){
this.cron.off();
}
}
module.exports = CloudCode;
// src/Cron.js
"use strict"
class Cron {
constructor(timezone){
this.cronJob = require('cron').CronJob;
this.jobs = [];
this.timezone = timezone;
}
addJob(timeFormat, func){
var job = new this.cronJob(timeFormat, func, null, false, this.timezone);
this.jobs.push(job);
return this;
}
on(){
for(let job of this.jobs){
job.start();
}
console.log("cron on");
}
off(){
for(let job of this.jobs){
job.stop();
}
console.log("cron off");
}
static getTimeFormat(min, hour, day, month, weekday){
return [min, hour, day, month, weekday].join(" ");
}
}
module.exports = Cron;
// end of index.js in https://github.com/ParsePlatform/parse-server-example
// thanks for shoe116 ( https://github.com/ParsePlatform/parse-server/pull/398 )
// ............... //
// [ CronJob ]
// Seconds: 0-59
// Minutes: 0-59
// Hours: 0-23
// Day of Month: 1-31
// Months: 0-11
// Day of Week: 0-6
if (process.env.NODE_ENV == "production") {
var Parse = require('parse/node').Parse;
Parse.initialize(process.env.APP_ID || 'myAppId', null, process.env.MASTER_KEY || '');
Parse.serverURL = process.env.SERVER_URL || 'http://localhost:1337/parse';
var CloudCode = require('./src/CloudCode');
var cloud = new CloudCode(Parse, 'Asia/Seoul');
cloud.putJob("backgroundJob", null); // parse cloud function name & param
cloud.addCron("backgroundJob", "0 */15 * * * *"); // per 15 minutes
cloud.start();
}
// src/Job.js
"use strict"
class Job {
constructor(Parse){
this.Parse = Parse;
this.jobs = {};
}
put(name, req){
var res = {
success: (message) => {console.log(message)},
error: (message) => {console.error(message)}
}
this.jobs[name] = (function () { this.Parse.Cloud.run(name, req, res); }).bind(this);
return this;
}
get(name){
return this.jobs[name];
}
}
module.exports = Job;
<!-- need add dependencies -->
{
"dependencies": {
"cron": "1.1.0"
}
}
@Kinzi
Copy link

Kinzi commented Jun 4, 2016

Nice! Thanks! ;-)

@miracle7
Copy link

This worked for me too, awesome! :D
Do you know if there are any limitations of doing it like this?

I see examples such as this one that run separate clock and worker dynos, and from reading documentation apparently this is required for horizontal scalability.

That example almost worked for me, but the worker dyno crashed after start with no errors, so didn't know how to debug further so am using this instead now :)

@Code-Crash
Copy link

Thanks!! It Works for me too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment