Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Scheduler

You can create one-time or recurring scheduled events to invoke serverless apps. At the specified time, the App Framework is notified which then invokes the relevant serverless method in the app.

Registration:

To register a scheduled event and the corresponding callback:

  • From your app’s root directory, navigate to the manifest.json file.
  • Include the events attribute, specifying the scheduled event and the corresponding callback methods as follows:
"events": {
    "onScheduledEvent": {
        "handler": "onScheduledEvent"
    }
}
  • Navigate to the server.js file.
exports = {
    onScheduledEvent: function(payload) {
        console.log("Logging arguments from onScheduledEvent: " + JSON.stringify(payload));
        if (payload.data.account_id === "3") {
            //app logic
        }
    }
};

Sample payload:

{
  "account_id" : "1234",
  "eventType"  : "onScheduledEvent",
  "data" : {
    "key":"value"
  },
  "iparams" : {
      "Param1" : "Piadgahkdfa734egrdafdsfsdf",
    }
}

Schedules:

You can use the following methods to create one time, recurring, update, fetch, delete, pause and resume schedules.

All of these methids are implemented in server.js

One Time Schedule:

  • To create a one-time schedule
try {
    await $Schedule.create({
      name: "sample",
      data: "hello world",
      schedule_at: "2023-06-10T07:00:00.860Z"
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Recurring Schedule:

  • To create a Recurring schedule
try {
    await $Schedule.create({
      name: "sample",
      data: "hello world",
      cronExpression: "0 8 * * *",
      type: 'CRON'
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Fetch a Schedule:

  • To fetch a schedule
try {
    await $Schedule.fetch({
      name: "sample"
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Update a Schedule:

  • To update a schedule
 try {
    await $Schedule.update({
      name: 'sample',
      data: "new data",
      schedule_at: "2023-07-10T07:00:00.860Z"
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Delete a Schedule:

  • To delete a schedule
try {
    await $Schedule.delete({
      name: 'sample'
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Pause a Schedule:

  • To pause a schedule
try {
    await $Schedule.pause({
      name: 'sample'
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Resume a Schedule:

  • To resume a schedule
 try {
    await $Schedule.resume({
      name: 'sample'
    });
  } catch (error) {
    console.log(`Error occured`);
    throw error;
  }

Ensure that the onScheduledEvent.json file, which contains the sample payload to test scheduled events, is available at <app’s root directory>/server/test_data.