Selfhosted Parse with cron jobs

Hey guys,

is there an easy approach to activate scheduled jobs in parse server?

There are some packages on npm, but none of this is working.
Thanks!

Easiest way is defining your job with Parse.Cloud.job() and schedule the jobs using your OS crontab. You can setup crontab with the periodicity you want and make it to send a request like this:

curl -X POST -H 'X-Parse-Application-Id: appId' -H 'X-Parse-Master-Key: masterKey' https://my-parse-server.com/parse/jobs/myJob
1 Like

I use this package:

https://www.npmjs.com/package/parse-server-jobs-scheduler

Install the package, add a few lines in the Parse Cloud code main file.

Configure the execution of jobs by Parse Dashboard

I used this already and somehow didn’t work. Even the npm description says it’s better to use normal crons.

I have been using it on a project for over a year.
I had no problems.
But I have a simple instance and not a cluster (which is what the documentation says, in this case cron is better.)

Hi there,

Just want to share, i also use the Parse server scheduler and so far until now i had no problems, i use it as on single instance, and it build as separate parse server from the main Parse Server, the main Parse server call the job function to the Parse server scheduler and the sceduler will run the job.

I’ve solved this problem with the following npm packages:

… and this helper function (TypeScript)

const schedule = require('node-schedule');
const axios = require('axios').default;
const logger = require('parse-server').logger;

export function scheduleCloudJob(jobname:string, cfg:string) {
    logger.info(`schedule job ${jobname} with config ${cfg}`);
    schedule.scheduleJob(cfg, function () {
        axios.create({
            baseURL: process.env.PARSE_SERVER_URL + '/jobs/' + jobname,
            timeout: 1000,
            headers: {'X-Parse-Application-Id':process.env.PARSE_SERVER_APPLICATION_ID,'X-Parse-Master-Key':process.env.PARSE_SERVER_MASTER_KEY}
        })
        .post()
        .then(function (res : any) {
            logger.info(`${jobname} triggered by scheduler`);
        })
        .catch((error : any) => {
            logger.error(`error during triggering ${jobname} by scheduler`);
            if (error.response) {
                logger.error(`${error.response.data.error};${error.response.status}`);
            } else if (error.request) {
                logger.error(error.request);
            } else { 
                logger.error(error.message);
            }
        });
    });
}

Usage:

// register cloud job myJob for running every minute
scheduleCloudJob('myJob', '0 * * * * *');

This way I don’t have to mess around with OS’s crontab which seems laborious to me to keep in sync over different Stages and operating systems.

FYI: I’ve just implemented and testet this solution. It seems to work fine but there are no long term experiences in productive environment.

in case others come across this problem. by far the most pain free way to implement cloud job scheduling is to use this package:
https://www.npmjs.com/package/@johnbraum/parse-server-scheduler

install using

yarn add @johnbraum/parse-server-scheduler

then add this code to your main cloud (usually cloud/index.js) file after you have declared/imported Parse

require('@johnbraum/parse-server-scheduler')(Parse)

this is a fork of the parse-server-scheduler and the main difference is the use of require rather than import for modules.

Hello

I use POSTMAN to call the job but it seems do not go through (ie. it does not call the job - it always return empty response). If I try to write a function and call the function, it works.

I’ve checked the X-Parse-Application-Id and X-Parse-Master-Key, they are correct and sent along in the request.

Do you know why?

Thanks

Can you show us which url etc you are using?

the URL is: http://localhost:1337/server/jobs/job_verifyIngameBalance

I have define this in the cloud functions

Parse.Cloud.job(‘job_verifyIngameBalance’, async(request: any)=> {…})

Did you configure the /server part by yourself? Normally it is /parse

Everything else looks right. Do you have also a dashboard running? Can you check there, if any jobs did run?

Yes, the server is configured by me. I’ve tested with normal functions call.

I haven’t setup dashboard. I’m trying to call the cronjob using Server Cron

I also use cron jobs and I am doing following:

curl -X POST -H ‘X-Parse-Application-Id: myApp’ -H ‘X-Parse-Master-Key: XXxxXX’ https://parse.server.de/parse/jobs/syncUser

I’ve just done this and it works like a bomb :slight_smile:

1 Like