Purpose: A website may need to perform several scheduled tasks, such as cleaning up the cache every hour, generating a sitemap Once a day, and backing up the database once a week ... If all is written in crontab, it may not be easy to manage and user configuration, use this class to easily add scheduled tasks, view the log of the execution Plan task, and manage the execution time of each scheduled task in the background. Installation: 1 database Add Table:
- CREATE TABLE ' Cron_schedule ' (
- ' schedule_id ' int (ten) unsigned not NULL auto_increment,
- ' Job_code ' varchar (255) Not NULL default ' 0 ',
- ' Status ' enum (' Pending ', ' running ', ' success ', ' missed ', ' error ') not NULL default ' pending ',
- ' Messages ' text,
- ' Created_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
- ' Scheduled_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
- ' Executed_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
- ' Finished_at ' datetime not NULL default ' 0000-00-00 00:00:00 ',
- PRIMARY KEY (' schedule_id '),
- KEY ' Task_name ' (' Job_code '),
- KEY ' Scheduled_at ' (' scheduled_at ', ' status ')
- ) Engine=myisam DEFAULT Charset=utf8;
2/application/config/config.php Add:
- /*
- |--------------------------------------------------------------------------
- | Cron Schedule Variables
- |--------------------------------------------------------------------------
- |
- | ' Enable_cron ' = whether to turn on cron schedule
- | ' Cron_schedule_table_name ' = table that holds cron schedule
- | ' Cron_schedule_generate_every ' = time interval to generate cron schedule
- | ' cron_schedule_ahead_for ' = cron Schedule each time it is produced
- | ' Cron_schedule_lifetime ' = the expiration date of cron schedule
- | ' Cron_history_cleanup_every ' = time interval to clear cron schedule
- | ' Cron_history_success_lifetime ' = save time for running a successful cron schedule
- | ' Cron_history_failure_lifetime ' = save time for cron schedule that failed to run
- |
- */
- $config [' enable_cron '] = TRUE;
- $config [' cron_schedule_table_name '] = ' cron_schedule ';
- $config [' cron_schedule_generate_every '] = 15;
- $config [' cron_schedule_ahead_for '] = 20;
- $config [' cron_schedule_lifetime '] = 15;
- $config [' cron_history_cleanup_every '] = 10;
- $config [' cron_history_success_lifetime '] = 60;
- $config [' cron_history_failure_lifetime '] = 600;
3/application/libraries/cron_schedule.php See annex 4 new/application/config/cron_schedules.php add Scheduled tasks to this file, such as:
- <?php
- $cron _schedule[' clear_log ') = Array (
- ' Schedule ' = Array (
- ' Config_path ' + ', '//cron expression is used to get an expression in the configuration file or database that is empty when directly specified
- ' cron_expr ' = ' */5 * * * * '//directly specify cron expression to get an expression in the configuration file or database is empty
- ),
- ' Run ' = = Array (
- ' FilePath ' = ' cron ',//directory where the file is located relative to AppPath
- ' filename ' = ' myclass.php ',//filename
- ' Class ' = ' MyClass ',//class name if only simple function can be empty
- ' function ' = ' clear_log ',//functions to execute
- ' params ' = = Array ()//parameters to be passed
- )
- );
- $cron _schedule[' clear_log ') = ...
- $cron _schedule[' create_sitemap ') = ...
5 adding the corresponding classes and functions/application/cron/myclass.php
- <?php
- Class MyClass
- {
- function Clear_log ($params = Array ())
- {
- Clean log
- }
- //.....
- }
6 adding/application/controllers/cron.php
- <?php
- Class Cron extends Ci_controller
- {
- Public Function Index ()
- {
- $this->load->library (' cron_schedule ');
- $this->cron_schedule->dispatch ();
- }
- }
7 Adding a system cron
- #crontab-E
- Add scheduled tasks as needed, such as:
- */5 * * * * php/var/www/ci210/index.php cron Index
- #service Cron Restart
Linux crontab timed task invoke CI framework PHP code