thamtech /
yii2-scheduler
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @copyright Copyright(c) 2016 Webtools Ltd |
||
| 4 | * @copyright Copyright(c) 2018 Thamtech, LLC |
||
| 5 | * @link https://github.com/thamtech/yii2-scheduler |
||
| 6 | * @license https://opensource.org/licenses/MIT |
||
| 7 | **/ |
||
| 8 | |||
| 9 | namespace thamtech\scheduler; |
||
| 10 | |||
| 11 | use thamtech\scheduler\models\SchedulerTask; |
||
| 12 | use yii\base\Component; |
||
| 13 | use Cron\CronExpression; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class Task |
||
| 17 | * @package thamtech\scheduler |
||
| 18 | */ |
||
| 19 | abstract class Task extends Component |
||
| 20 | { |
||
| 21 | const EVENT_BEFORE_RUN = 'TaskBeforeRun'; |
||
| 22 | const EVENT_AFTER_RUN = 'TaskAfterRun'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Module the scheduler module. |
||
| 26 | */ |
||
| 27 | public $scheduler; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Exception|null Exception raised during run (if any) |
||
| 31 | */ |
||
| 32 | public $exception; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var String Task description |
||
| 36 | */ |
||
| 37 | public $description; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var String The cron expression that determines how often this task |
||
| 41 | * should run. |
||
| 42 | */ |
||
| 43 | public $schedule; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool Active flag allows you to set the task to inactive (meaning it |
||
| 47 | * will not run) |
||
| 48 | */ |
||
| 49 | public $active = true; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int How many seconds after due date to wait until the task becomes |
||
| 53 | * overdue and is re-run. This should be set to at least 2x the amount |
||
| 54 | * of time the task takes to run as the task will be restarted. |
||
| 55 | */ |
||
| 56 | public $overdueThreshold = 3600; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var null|SchedulerTask |
||
| 60 | */ |
||
| 61 | private $_model; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string the task name |
||
| 65 | */ |
||
| 66 | private $_displayName; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \thamtech\scheduler\models\SchedulerLog |
||
| 70 | */ |
||
| 71 | private $_log; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The main method that gets invoked whenever a task is ran, any errors that occur |
||
| 75 | * inside this method will be captured by the TaskRunner and logged against the task. |
||
| 76 | * |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | abstract public function run(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \thamtech\scheduler\models\SchedulerLog $log |
||
| 83 | */ |
||
| 84 | public function setLog($log) |
||
| 85 | { |
||
| 86 | $this->_log = $log; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return SchedulerLog |
||
|
0 ignored issues
–
show
|
|||
| 91 | */ |
||
| 92 | public function getLog() |
||
| 93 | { |
||
| 94 | return $this->_log; |
||
|
0 ignored issues
–
show
|
|||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string|\DateTime $currentTime |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 4 | public function getNextRunDate($currentTime = 'now') |
|
| 103 | { |
||
| 104 | 4 | // Let's continue to use the deprecated CronExpression::factory() method |
|
| 105 | 4 | // as long as we continue to support dragonmantank/cron-expression |
|
| 106 | 4 | // versions before v3.0.2 to avoid compatibility issues. |
|
| 107 | // |
||
| 108 | // Once we require v3.0.2 or higher version of cron-expression, then |
||
| 109 | // we can remove the scrutinizer ignore-deprecated tag and replace |
||
| 110 | // |
||
| 111 | // `CronExpression::factory($this->schedule)` |
||
| 112 | // |
||
| 113 | // with |
||
| 114 | 1 | // |
|
| 115 | // `(new CronExpression($this->schedule))` |
||
| 116 | 1 | // |
|
| 117 | 1 | return /** @scrutinizer ignore-deprecated */ CronExpression::factory($this->schedule) |
|
| 118 | ->getNextRunDate($currentTime) |
||
| 119 | ->format('Y-m-d H:i:s'); |
||
| 120 | } |
||
| 121 | |||
| 122 | 2 | /** |
|
| 123 | * Sets the task display name. |
||
| 124 | 2 | * |
|
| 125 | * @param string $name the task name |
||
| 126 | */ |
||
| 127 | public function setDisplayName($name) |
||
| 128 | { |
||
| 129 | $this->_displayName = $name; |
||
| 130 | 2 | } |
|
| 131 | |||
| 132 | 2 | /** |
|
| 133 | 2 | * @return string |
|
| 134 | */ |
||
| 135 | public function getDisplayName() |
||
| 136 | { |
||
| 137 | return $this->_displayName ?: get_class($this); |
||
| 138 | 1 | } |
|
| 139 | |||
| 140 | 1 | /** |
|
| 141 | * @param SchedulerTask $model |
||
| 142 | */ |
||
| 143 | public function setModel($model) |
||
| 144 | { |
||
| 145 | $this->_model = $model; |
||
| 146 | 1 | } |
|
| 147 | |||
| 148 | 1 | /** |
|
| 149 | 1 | * @return SchedulerTask |
|
| 150 | */ |
||
| 151 | public function getModel() |
||
| 152 | { |
||
| 153 | return $this->_model; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $str |
||
| 158 | */ |
||
| 159 | public function writeLine($str) |
||
| 160 | { |
||
| 161 | echo $str.PHP_EOL; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Mark the task as started |
||
| 166 | */ |
||
| 167 | public function start() |
||
| 168 | { |
||
| 169 | $model = $this->getModel(); |
||
| 170 | $model->started_at = date('Y-m-d H:i:s'); |
||
| 171 | $model->save(false);} |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Mark the task as stopped. |
||
| 175 | */ |
||
| 176 | public function stop() |
||
| 177 | { |
||
| 178 | $model = $this->getModel(); |
||
| 179 | $model->last_run = $model->started_at; |
||
| 180 | $model->next_run = $this->getNextRunDate(); |
||
| 181 | $model->started_at = null; |
||
| 182 | $model->save(false); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param bool $forceRun |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function shouldRun($forceRun = false) |
||
| 191 | { |
||
| 192 | $model = $this->getModel(); |
||
| 193 | $isDue = in_array($model->status_id, [SchedulerTask::STATUS_DUE, SchedulerTask::STATUS_OVERDUE, SchedulerTask::STATUS_ERROR]); |
||
| 194 | $isRunning = $model->status_id == SchedulerTask::STATUS_RUNNING; |
||
| 195 | $overdue = false; |
||
| 196 | |||
| 197 | if((strtotime($model->started_at) + $this->overdueThreshold) <= strtotime("now")) { |
||
| 198 | $overdue = true; |
||
| 199 | } |
||
| 200 | |||
| 201 | return ($this->active && ((!$isRunning && ($isDue || $forceRun)) || ($isRunning && $overdue))); |
||
| 202 | } |
||
| 203 | |||
| 204 | } |
||
| 205 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths