TaskRunner   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 14.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 152
ccs 10
cts 68
cp 0.1471
rs 10
c 2
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTask() 0 3 1
A setLog() 0 3 1
A getLog() 0 3 1
A setTask() 0 3 1
A initLog() 0 9 1
A handleError() 0 19 2
A runTask() 0 35 4
A log() 0 10 2
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 Yii;
12
use thamtech\scheduler\events\TaskEvent;
13
use thamtech\scheduler\models\SchedulerLog;
14
use thamtech\scheduler\models\SchedulerTask;
15
16
/**
17
 * Class TaskRunner
18
 *
19
 * @property \thamtech\scheduler\Task $task
20
 */
21
class TaskRunner extends \yii\base\Component
22
{
23
24
    /**
25
     * @var bool Indicates whether an error occured during the executing of the task.
26
     */
27
    public $error;
28
29
    /**
30
     * @var \thamtech\scheduler\Task The task that will be executed.
31
     */
32
    private $_task;
33
34
    /**
35
     * @var \thamtech\scheduler\models\SchedulerLog
36
     */
37
    private $_log;
38
39
    /**
40
     * @var bool
41
     */
42
    private $running = false;
43
44
    /**
45
     * @param Task $task
46
     */
47 1
    public function setTask(Task $task)
48
    {
49 1
        $this->_task = $task;
50 1
    }
51
52
    /**
53
     * @return Task
54
     */
55 1
    public function getTask()
56
    {
57 1
        return $this->_task;
58
    }
59
60
    /**
61
     * @param \thamtech\scheduler\models\SchedulerLog $log
62
     */
63 1
    public function setLog($log)
64
    {
65 1
        $this->_log = $log;
66 1
    }
67
68
    /**
69
     * @return SchedulerLog
70
     */
71 1
    public function getLog()
72
    {
73 1
        return $this->_log;
74
    }
75
76
    /**
77
     * @param bool $forceRun
78
     */
79
    public function runTask($forceRun = false)
80
    {
81
        $task = $this->getTask();
82
83
        if ($task->shouldRun($forceRun)) {
84
            $event = new TaskEvent([
85
                'task' => $task,
86
                'taskRunner' => $this,
87
                'success' => true,
88
            ]);
89
            $this->trigger(Task::EVENT_BEFORE_RUN, $event);
90
            if (!$event->cancel) {
91
                $this->initLog();
92
                $task->setLog($this->getLog());
93
                $task->start();
94
                ob_start();
95
                try {
96
                    $this->running = true;
97
                    $task->run();
98
                    $this->running = false;
99
                    $output = ob_get_contents();
100
                    ob_end_clean();
101
                    $this->log($output);
102
                    $task->stop();
103
                } catch (\Exception $e) {
104
                    $this->running = false;
105
                    $task->exception = $e;
106
                    $event->exception = $e;
107
                    $event->success = false;
108
                    $this->handleError($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
109
                }
110
                $this->trigger(Task::EVENT_AFTER_RUN, $event);
111
            }
112
        }
113
        $task->getModel()->save();
114
    }
115
116
    /**
117
     * @param $code
118
     *
119
     * @param $message
120
     *
121
     * @param $file
122
     *
123
     * @param $lineNumber
124
     */
125
    public function handleError($code, $message, $file, $lineNumber)
126
    {
127
        echo sprintf('ERROR: %s %s', $code, PHP_EOL);
128
        echo sprintf('ERROR FILE: %s %s', $file, PHP_EOL);
129
        echo sprintf('ERROR LINE: %s %s', $lineNumber, PHP_EOL);
130
        echo sprintf('ERROR MESSAGE: %s %s', $message, PHP_EOL);
131
132
        // if the failed task was mid transaction, rollback so we can save.
133
        if (null !== ($tx = \Yii::$app->db->getTransaction())) {
134
            $tx->rollBack();
135
        }
136
137
        $output = ob_get_contents();
138
        ob_end_clean();
139
140
        $this->error = true;
141
        $this->log($output);
142
        $this->getTask()->getModel()->status_id = SchedulerTask::STATUS_ERROR;
143
        $this->getTask()->stop();
144
    }
145
146
    /**
147
     * @param string $output
148
     */
149
    public function log($output)
150
    {
151
        $model = $this->getTask()->getModel();
152
        $log = $this->getLog();
153
154
        $log->started_at = $model->started_at;
155
        $log->ended_at = date('Y-m-d H:i:s');
156
        $log->error = $this->error ? 1 : 0;
157
        $log->output = $output;
158
        $log->save(false);
159
    }
160
161
    /**
162
     * Initialize the log record.
163
     */
164
    protected function initLog()
165
    {
166
        $model = $this->getTask()->getModel();
167
        $log = $this->getLog();
168
169
        $log->scheduler_task_id = $model->id;
170
        $log->started_at = date('Y-m-d H:i:s');
171
        $log->save(false);
172
        $log->refresh();
173
    }
174
}
175