Utilities for scheduled jobs

Utilities for schedueled jobs are designed to automate the regular execution of some actions at certain intervals.

Each utility must be specified in deploy.json of the application in the globals.jobs object, for example:

{
  "globals": {
    "jobs": {
      "ticketClose": {
        "description": "Ночной перевод билетов в статус \"проверен\"",
          "launch": {
            "timeout": 3600000,
            "hour": 24
          },
          "worker": "ticketCloser",
          "di": {
            "ticketCloser": {
              "executable": "applications/khv-ticket-discount/lib/overnightTicketClose",
              "options": {
                "dataRepo": "ion://dataRepo",
                "log": "ion://sysLog",
                "workflows": "ion://workflows"
...

di should contain a field with a name equal to worker value - this is the job that will be launched.

Here the script applications/khv-ticket-discount/lib/overnightTicketClose.js is launched on the schedule. launch can be an object containing the following fields:

month, week, day, dayOfYear, weekday, hour, min, minute, sec, second - set the interval for job executions;

check - interval for checking the execution condition, in milliseconds, by default - 1000.

For example, if check is equal to 5000, and sec is 2, the job will be implemented only every 10 seconds, when the interval between checks coincides with the interval of execution

If the job execution interval is not specified, then it will be executed when the application is launched and at each check interval.

timeout - the time in milliseconds after which the running job is interrupted by timeout;

launch can also be an integer - the interval of the job in milliseconds, while the job will also be performed immediately when the application starts. The timeout will be set equal to the execution interval.

In the options field, any variables and their values, which will become available in the script through the fields of the object passed as an argument to the main function of the module, can be specified.

The script is compiled in the module format, for example:

"use strict";
const Logger = require("core/interfaces/Logger");

module.exports = function (options) {
  return options.dataRepo
    .getList(
      "ticket@khv-ticket-discount",
      "ticketYear@khv-ticket-discount"
    )
    .then((tickets) => {
      let p = Promise.resolve();
      tickets.forEach((ticket) => {
        p = p
          .then(() => options.dataRepo.editItem(ticket.getClassName(), ticket.getItemId(), {"state": "close"}))
          .then(item => (item.name === "ticketYear" ?
            options.workflows.pushToState(item, "ticketYear@khv-ticket-discount", "close") :
            options.workflows.pushToState(item, "ticket@khv-ticket-discount", "close")))
          .catch((err) => {
            if(options.log instanceof Logger) {
              options.log.error(err);
            } else {
              console.error(err);
            }
          });
      });
      return p;
    });
};