A job module is a background job that LiteCart pushes frequently to run.
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.
<?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 module 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 '5 min': case 'Quarter-Hourly': //if (strtotime($last_run) > strtotime('-5 minutes')) return; if (date('Ymdh', strtotime($last_run)) == date('Ymdh') && floor(date('i', strtotime($last_run))/12) == floor(date('i')/12)) return; break; case '10 min': case 'Quarter-Hourly': //if (strtotime($last_run) > strtotime('-10 minutes')) return; if (date('Ymdh', strtotime($last_run)) == date('Ymdh') && floor(date('i', strtotime($last_run))/6) == floor(date('i')/6)) return; break; case '15 min': case 'Quarter-Hourly': //if (strtotime($last_run) > strtotime('-15 minutes')) return; if (date('Ymdh', strtotime($last_run)) == date('Ymdh') && floor(date('i', strtotime($last_run))/4) == floor(date('i')/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') && floor(date('i', strtotime($last_run))/2) == floor(date('i')/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') && floor(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') && floor(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') && floor(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') && floor(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) == ceil(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) == ceil(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; } } // Do a bunch of stuff // ... echo 'Output something to be seen in 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("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() { } }