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)) {
 
      // Abort if the modules is disabled
        if (empty($this->settings['status'])) return;
 
      // Abort if outside the window of working hours
        if (!empty($this->settings['working_hours'])) {
          $is_working_hours = false;
          foreach (preg_split('#\s*,\s*#', $this->settings['working_hours'], -1, PREG_SPLIT_NO_EMPTY) as $working_hours)
          list($from_time, $to_time) = preg_split('#\s*-\s*#', $working_hours, -1, PREG_SPLIT_NO_EMPTY);
          if (time() > strtotime("Today $from_time") && time() < strtotime("Today $to_time")) {
            $is_working_hours = true;
            break;
          }
          if (!$is_working_hours) return;
        }
 
      // Abort if the frequency for running the job is not met
        switch ($this->settings['frequency']) {
 
          case '15 min':
          case 'Quarter-Hourly':
            //if (strtotime($last_run) > strtotime('-15 minutes')) return;
            if (date('Ymdh', strtotime($last_run)) == date('Ymdh') && ceil(date('m', strtotime($last_run))/4) == floor(date('m'))/4) return;
            break;
 
          case '30 min':
          case 'Half-Hourly':
            //if (strtotime($last_run) > strtotime('-30 minutes')) return;
            if (date('Ymdh', strtotime($last_run)) == date('Ymdh') && ceil(date('m', strtotime($last_run))/2) == floor(date('m'))/2) return;
            break;
 
          case 'Hourly':
            //if (strtotime($last_run) > strtotime('-1 hours')) return;
            if (date('Ymdh', strtotime($last_run)) == date('Ymdh')) return;
            break;
 
          case '2 Hours':
            //if (strtotime($last_run) > strtotime('-2 hours')) return;
            if (date('Ymd', strtotime($last_run)) == date('Ymd') && ceil(date('h', strtotime($last_run))/2) == floor(date('h'))/2) return;
            break;
 
          case '3 Hours':
            //if (strtotime($last_run) > strtotime('-3 hours')) return;
            if (date('Ymd', strtotime($last_run)) == date('Ymd') && ceil(date('h', strtotime($last_run))/3) == floor(date('h'))/3) return;
            break;
 
          case '6 Hours':
            //if (strtotime($last_run) > strtotime('-6 hours')) return;
            if (date('Ymd', strtotime($last_run)) == date('Ymd') && ceil(date('h', strtotime($last_run))/6) == floor(date('h'))/6) return;
            break;
 
          case '12 Hours':
            //if (strtotime($last_run) > strtotime('-12 hours')) return;
            if (date('Ymd', strtotime($last_run)) == date('Ymd') && ceil(date('h', strtotime($last_run))/12) == floor(date('h'))/12) return;
            break;
 
          case 'Daily':
            //if (strtotime($last_run) > strtotime('-24 hours')) 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 'Quarterly':
            //if (strtotime($last_run) > strtotime('-3 months')) return;
            if (date('Y', strtotime($last_run)) == date('Y') && ceil(date('m', strtotime($last_run))/3) == floor(date('m'))/3) return;
            break;
 
          case 'Half-Yearly':
            //if (strtotime($last_run) > strtotime('-6 months')) return;
            if (date('Y', strtotime($last_run)) == date('Y') && ceil(date('m', strtotime($last_run))/6) == floor(date('m'))/6) 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 [
        [
          '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("Quarter-Hourly","Half-Hourly","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 would operate. Example: 07:00-12:00,13: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' => 'int()',
        ],
      ];
    }
 
    public function install() {
    }
 
    public function uninstall() {
    }
  }
how_to_create_a_job_module.txt · Last modified: 2021/10/10 19:17 by tim