| 1 | <?php |
||
| 20 | class Deferred_tasks { |
||
| 21 | use |
||
| 22 | CRUD, |
||
| 23 | Singleton; |
||
| 24 | protected $data_model = [ |
||
| 25 | 'id' => 'int', |
||
| 26 | 'begin' => 'int:0', |
||
| 27 | 'started' => 'int:0', |
||
| 28 | 'started_hash' => 'text', |
||
| 29 | 'expected' => 'int:1', |
||
| 30 | 'priority' => 'int:0..2', |
||
| 31 | 'module' => 'text', |
||
| 32 | 'data' => 'json' |
||
| 33 | ]; |
||
| 34 | protected $table = '[prefix]deferred_tasks_tasks'; |
||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $max_number_of_workers; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $security_key; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $core_url; |
||
| 47 | |||
| 48 | protected function construct () { |
||
| 49 | $Config = Config::instance(); |
||
| 50 | $module_data = $Config->module('Deferred_tasks'); |
||
| 51 | $this->max_number_of_workers = $module_data->max_number_of_workers; |
||
| 52 | $this->security_key = $module_data->security_key; |
||
| 53 | $this->core_url = $Config->core_url(); |
||
| 54 | } |
||
| 55 | /** |
||
| 56 | * Returns database index |
||
| 57 | * |
||
| 58 | * @return int |
||
| 59 | */ |
||
| 60 | protected function cdb () { |
||
| 63 | /** |
||
| 64 | * Add new task |
||
| 65 | * |
||
| 66 | * @param string $module Module, to what task belongs |
||
| 67 | * @param mixed $data Any data, needed for task execution. Can be array, string, number... |
||
| 68 | * @param int $expected Max time in seconds, during which task is expected to be finished |
||
| 69 | * @param int $begin Unix timestamp, task will not be executed until this time |
||
| 70 | * @param int $priority Priority 0..2, higher number - higher priority |
||
| 71 | * |
||
| 72 | * @return false|int Id of created task or `false` on failure |
||
|
1 ignored issue
–
show
|
|||
| 73 | */ |
||
| 74 | function add ($module, $data, $expected, $begin = 0, $priority = 1) { |
||
| 77 | /** |
||
| 78 | * Get task |
||
| 79 | * |
||
| 80 | * @param int $id |
||
| 81 | * |
||
| 82 | * @return false|mixed |
||
| 83 | */ |
||
| 84 | protected function get ($id) { |
||
| 87 | /** |
||
| 88 | * Delete task |
||
| 89 | * |
||
| 90 | * @param int $id |
||
| 91 | * |
||
| 92 | * @return false|mixed |
||
| 93 | */ |
||
| 94 | function del ($id) { |
||
| 97 | /** |
||
| 98 | * Run specified task |
||
| 99 | * |
||
| 100 | * @param int $task |
||
| 101 | * |
||
| 102 | * @throws ExitException |
||
| 103 | */ |
||
| 104 | function run_task ($task) { |
||
| 118 | /** |
||
| 119 | * Run worker |
||
| 120 | */ |
||
| 121 | function run_tasks () { |
||
| 138 | /** |
||
| 139 | * Update time of task start |
||
| 140 | * |
||
| 141 | * @param int $id |
||
| 142 | * |
||
| 143 | * @return bool `false` if another worker occupied this task in the meantime |
||
| 144 | */ |
||
| 145 | protected function started ($id) { |
||
| 154 | /** |
||
| 155 | * Get number of runned workers |
||
| 156 | * |
||
| 157 | * @return int |
||
|
1 ignored issue
–
show
|
|||
| 158 | */ |
||
| 159 | protected function tasks_running () { |
||
| 169 | /** |
||
| 170 | * Get id of next task that should be executed |
||
| 171 | * |
||
| 172 | * @return false|int |
||
| 173 | */ |
||
| 174 | protected function next_task () { |
||
| 192 | } |
||
| 193 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.