-
-
Save rcdelacruz/ae552a19b70ee806b408f71c56457dbe to your computer and use it in GitHub Desktop.
Background Job in Parse Server (node/cron)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- need add dependencies --> | |
{ | |
"dependencies": { | |
"cron": "1.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment