How To Create a Job Module

A job module is a background job that LiteCart pushes frequently to run.

Example

In the following example, we control the frequency of how often the job will be processed. See how the settings are defined and then used.

Please note: For the job to be considered as successfully processed, LiteCart is expecting an output. Return null or false to skip the process.

includes/modules/jobs/job_myjob.inc.php:


<?php

  class job_myjob {
    public $id = __CLASS__;
    public $name = 'My Background Job';
    public $description = '';
    public $author = 'ACME Inc.';
    public $version = '1.0';
    public $website = 'https://www.acme.com';
    public $priority = 0;

    public function process($force, $last_run) {

      if (empty($force)) {

      // Abort if the module is disabled
        if (empty($this->settings['status'])) return;

      // Abort if outside the window of working hours
        if (!empty($this->settings['working_hours'])) {
          list($from_time, $to_time) = explode('-', $this->settings['working_hours']);
          if (time() < strtotime("Today $from_time") || time() > strtotime("Today $to_time")) return;
        }

      // Abort if the frequency for running this job is not met
        switch ($this->settings['frequency']) {

            case '5 min':
              if (strtotime($last_run) >= mktime(date('H'), floor(date('i')/5)*5, 0, 0)) return;
              break;

            case '10 min':
              if (strtotime($last_run) >= mktime(date('H'), floor(date('i')/10)*10, 0, 0)) return;
              break;

            case '15 min':
              if (strtotime($last_run) >= mktime(date('H'), floor(date('i')/15)*15, 0, 0)) return;
              break;

            case '30 min':
              if (strtotime($last_run) >= mktime(date('H'), floor(date('i')/30)*30, 0, 0)) return;
              break;

            case 'Hourly':
              if (strtotime($last_run) >= mktime(date('H'), 0, 0)) return;
              break;

            case '2 Hours':
              if (strtotime($last_run) >= mktime(floor(date('H')/2)*2, 0, 0)) return;
              break;

            case '3 Hours':
              if (strtotime($last_run) >= mktime(floor(date('H')/3)*3, 0, 0)) return;
              break;

            case '6 Hours':
              if (strtotime($last_run) >= mktime(floor(date('H')/6)*6, 0, 0)) return;
              break;

            case '12 Hours':
              if (strtotime($last_run) >= mktime(floor(date('H')/12)*12, 0, 0)) return;
              break;

            case 'Daily':
              if (strtotime($last_run) >= mktime(0, 0, 0)) return;
              break;

            case 'Weekly':
              if (strtotime($last_run) >= strtotime('This week 00:00:00')) return;
              break;

            case 'Monthly':
              if (strtotime($last_run) >= mktime(0, 0, 0, null, 1)) return;
              break;

            case 'Quarterly':
              if (strtotime($last_run) >= mktime(0, 0, 0, ((ceil(date('n')/3)-1)*3)+1)) return;
              break;

            case 'Half-Yearly':
              if (strtotime($last_run) >= mktime(0, 0, 0, ((ceil(date('n')/6)-1)*6)+1)) return;

            case 'Yearly':
              if (strtotime($last_run) >= mktime(0, 0, 0, 1, 1)) return;
              break;

            default:
              trigger_error('Unknown job frequency interval ('. $this->settings['frequency'] .')', E_USER_WARNING);
              break;
        }
      }

    // Do desired stuff
    // ...

      echo 'Output something for the log...' . PHP_EOL;
    }

    public function settings() {
      return [
        [
          'key' => 'status',
          'default_value' => '1',
          'title' => language::translate(__CLASS__.':title_status', 'Status'),
          'description' => language::translate(__CLASS__.':description_status', 'Enables or disables the module.'),
          'function' => 'toggle("e/d")',
        ],
        [
          'key' => 'frequency',
          'default_value' => 'Daily',
          'title' => language::translate(__CLASS__.':title_frequency', 'Frequency'),
          'description' => language::translate(__CLASS__.':description_frequency', 'How often this job should be processed.'),
          'function' => 'radio("Immediately","5 min","10 min","15 min","30 min","Hourly","2 Hours","3 Hours","6 Hours","12 Hours","Daily","Weekly","Monthly","Quarterly","Half-Yearly","Yearly")',
        ],
        [
          'key' => 'working_hours',
          'default_value' => '07:00-21:00',
          'title' => language::translate(__CLASS__.':title_working_hours', 'Working Hours'),
          'description' => language::translate(__CLASS__.':description_working_hours', 'During what hours of the day the job should operate. Example: 07:00-21:00'),
          'function' => 'text()',
        ],
        [
          'key' => 'priority',
          'default_value' => '0',
          'title' => language::translate(__CLASS__.':title_priority', 'Priority'),
          'description' => language::translate(__CLASS__.':description_priority', 'Process this module in the given priority order.'),
          'function' => 'number()',
        ],
      ];
    }

    // Run some code when the module is installed
    public function install() {
    }

    // Run some code when the module is uninstalled
    public function uninstall() {
    }
  }

Revisions

Top Editors
Recently Edited Articles