User Tools

Site Tools


how_to_create_a_job_module

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 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 Fancy Background Job';
    public $description = '';
    public $author = 'ACME Inc.';
    public $version = '1.0';
    public $website = 'http://www.acme.com';
    public $priority = 0;
 
    public function process($force, $last_run) {
 
      if (empty($force)) {
 
        if (empty($this->settings['status'])) return;
 
        switch ($this->settings['frequency']) {
 
          case 'Hourly':
            if (date('Ymdh', strtotime($last_run)) == date('Ymdh')) return;
            break;
 
          case '2 Hours':
            if (strtotime($last_run) > strtotime('-2 hours')) return;
            break;
 
          case '3 Hours':
            if (strtotime($last_run) > strtotime('-3 hours')) return;
            break;
 
          case '6 Hours':
            if (strtotime($last_run) > strtotime('-6 hours')) return;
            break;
 
          case '12 Hours':
            if (strtotime($last_run) > strtotime('-12 hours')) return;
            break;
 
          case 'Daily':
            //if (strtotime($last_run) > strtotime('-1 day')) return;
            if (date('Ymd', strtotime($last_run)) == date('Ymd')) return;
            break;
 
          case 'Weekly':
            //if (strtotime($last_run) > strtotime('-1 week')) return;
            if (date('W', strtotime($last_run)) == date('W')) return;
            break;
 
          case 'Monthly':
            //if (strtotime($last_run) > strtotime('-1 month')) return;
            if (date('Ym', strtotime($last_run)) == date('Ym')) return;
            break;
 
          case 'Yearly':
            //if (strtotime($last_run) > strtotime('-1 year')) return;
            if (date('Y', strtotime($last_run)) == date('Y')) return;
            break;
        }
      }
 
      echo 'Doing something great...' . PHP_EOL;
 
      // Do something ... 
    }
 
    public function settings() {
 
      return array(
        array(
          '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")',
        ),
        array(
          '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("Hourly","2 Hours","3 Hours","6 Hours","12 Hours","Daily","Weekly","Monthly","Yearly")',
        ),
        array(
          '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' => 'int()',
        ),
      );
    }
 
    public function install() {
    }
 
    public function uninstall() {
    }
  }
how_to_create_a_job_module.txt · Last modified: 2020/11/29 20:21 by A User Not Logged in