Skip to content

Latest commit

 

History

History
119 lines (77 loc) · 2.51 KB

File metadata and controls

119 lines (77 loc) · 2.51 KB
title slug description sidebar_position
Custom Jobs
./custom-jobs
This guide will teach you how to add custom jobs to your self-hosted Moralis Server
3

{% hint style="info" %}

Overview

This guide will teach you how to add custom jobs to your self-hosted Moralis server.

{% endhint %}

{% hint style="warning" %}

Important

The completion of the Parse Dashboard and Custom Cloud Code guides is required to set up custom jobs.

{% endhint %}

Installation

Install the required package:

npm install parse-server-jobs-scheduler

Configuration

Inside your src/cloud/ folder, create a new file called jobs.ts and add the following code:

declare const Parse: any;

import Scheduler from "parse-server-jobs-scheduler";
const scheduler = new Scheduler();

// Recreates all crons when the server is launched
scheduler.recreateScheduleForAllJobs();

// Recreates schedule when a job schedule has changed
Parse.Cloud.afterSave("_JobSchedule", async (request: any) => {
  scheduler.recreateSchedule(request.object.id);
});

// Destroy schedule for removed job
Parse.Cloud.afterDelete("_JobSchedule", async (request: any) => {
  scheduler.destroySchedule(request.object.id);
});

Import the new file inside src/cloud/main.ts by adding:

//import jobs.ts file
import "./jobs";

Creating a job

Create a job inside src/cloud/cloud.ts:

declare const Parse: any;

Parse.Cloud.define("Hello", () => {
  return `Hello! Cloud functions are cool!`;
});

Parse.Cloud.define("SayMyName", (request: any) => {
  return `Hello ${request.params.name}! Cloud functions are cool!`;
});

// Creating a new job
Parse.Cloud.job("newJob", () => {
  console.log("The code inside newJob has beed executed");
  return "test";
});

Build and run

Build the project:

npm run build

And then run it locally:

npm run start

Schedule a job

Head over to http://localhost:1337/dashboard/ and choose Jobs --> Schedule a job:

Write a description and choose newJob on the Cloud job dropdown:

Configure when you want the job to run and choose Schedule:

{% hint style="success" %}

If Start immediately was selected your job will start right away and repeat based on the selected time interval.

{% endhint %}