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.

Basic Example

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=false) {
 
      if (empty($this->settings['status'])) return;
 
      // Do something ... 
    }
 
    public function settings() {
 
      return array(
        array(
          'key' => 'status',
          'default_value' => '1',
          'title' => 'Status',
          'description' => 'Enables or disables the module.'
          'function' => 'toggle("e/d")',
        ),
        array(
          'key' => 'priority',
          'default_value' => '0',
          'title' => 'Priority',
          'description' => 'Process this module in the given priority order.',
          'function' => 'int()',
        ),
      );
    }
 
    public function install() {}
 
    public function uninstall() {}
  }
 
?>

Basic Example (With Run Control)

If you want to control how often the job can be executeed we need to keep track of time of each excecution. See how we create the settings key “job_myjob:last_run” and abort an execution if recently already executed.

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=false) {
 
      if (empty($this->settings['status'])) return;
 
      if (empty($force)) {
        switch ($this->settings['update_frequency']) {
          case 'Daily':
            if (strtotime(settings::get(__CLASS__.':last_run')) > strtotime('-1 day')) return;
            break;
          case 'Weekly':
            if (strtotime(settings::get(__CLASS__.':last_run')) > strtotime('-1 week')) return;
            break;
          case 'Monthly':
            if (strtotime(settings::get(__CLASS__.':last_run')) > strtotime('-1 month')) return;
            break;
        }
      }
 
    // Check in for work
      database::query(
        "update ". DB_TABLE_SETTINGS ."
        set value = '". date('Y-m-d H:i:s') . "'
        where `key` = '". __CLASS__ .":last_run'
        limit 1;"
      );
 
      // 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' => 'update_frequency',
          'default_value' => 'Daily',
          'title' => language::translate(__CLASS__.':title_update_frequency', 'Update Frequency'),
          'description' => language::translate(__CLASS__.':description_update_frequency', 'How often the currency values should be updated.'),
          'function' => 'radio("Daily","Weekly","Monthly")',
        ),
        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() {
      database::query(
        "insert into ". DB_TABLE_SETTINGS ."
        (title, description, `key`, value, date_created, date_updated)
        values ('My Fancy Job Last Run', 'Time when job was last run.', '".__CLASS__.":last_run', '', '". date('Y-m-d H:i:s') ."', '". date('Y-m-d H:i:s') ."');"
      );
    }
 
    public function uninstall() {
      database::query(
        "delete from ". DB_TABLE_SETTINGS ."
        where `key` = '".__CLASS__.":last_run'
        limit 1;"
      );
    }
  }
 
?>
how_to_create_a_job_module.txt · Last modified: 2016/02/13 00:38 by 78.72.180.122