Completed
Push — 2.0 ( ee0168...76e968 )
by Marco
11:26
created

Runner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 15
dl 0
loc 164
rs 9.1666
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 2
C run() 0 85 7
A openWorklog() 0 22 1
A closeWorklog() 0 15 1
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Extender\Components\Database;
4
use \Comodojo\Extender\Task\Table as TasksTable;
5
use \Comodojo\Extender\Events\TaskEvent;
6
use \Comodojo\Extender\Events\TaskStatusEvent;
7
use \Comodojo\Foundation\Base\Configuration;
8
use \Comodojo\Foundation\Events\Manager as EventsManager;
9
use \Comodojo\Daemon\Traits\LoggerTrait;
10
use \Comodojo\Daemon\Traits\EventsTrait;
11
use \Comodojo\Extender\Traits\ConfigurationTrait;
12
use \Comodojo\Extender\Traits\TasksTableTrait;
13
use \Comodojo\Extender\Orm\Entities\Worklog;
14
use \Comodojo\Extender\Utils\StopWatch;
15
use \Psr\Log\LoggerInterface;
16
use \Doctrine\ORM\EntityManager;
17
use \Comodojo\Exception\TaskException;
18
use \Exception;
19
20
/**
21
* @package     Comodojo Extender
22
* @author      Marco Giovinazzi <[email protected]>
23
* @license     MIT
24
*
25
* LICENSE:
26
*
27
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
* THE SOFTWARE.
34
 */
35
36
class Runner {
37
38
    use LoggerTrait;
39
    use ConfigurationTrait;
40
    use EventsTrait;
41
    use TasksTableTrait;
42
43
    protected $em;
44
45
    protected $worklog;
46
47
    protected $stopwatch;
48
49
    public function __construct(
50
        Configuration $configuration,
51
        LoggerInterface $logger,
52
        TasksTable $table,
53
        EventsManager $events,
54
        EntityManager $em = null
55
    ) {
56
57
        // init components
58
        $this->setConfiguration($configuration);
59
        $this->setLogger($logger);
60
        $this->setEvents($events);
61
        $this->setTasksTable($table);
62
63
        // create StopWatch
64
        $this->stopwatch = new StopWatch();
65
66
        // init database
67
        $this->em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em;
68
69
        // init worklog manager
70
        $this->worklog = new Worklog();
71
72
    }
73
74
    public function run(
75
        $name,
76
        $task,
77
        $jid = null,
78
        TaskParameters $parameters = null
79
    ) {
80
81
        if ( is_null($parameters) ) $parameters = new TaskParameters();
82
83
        try {
84
85
            $this->stopwatch->start();
86
87
            $this->logger->info("Starting new task $task ($name)");
88
89
            $thetask = $this->table->get($task)->getInstance($name, $parameters);
90
91
            $this->events->emit( new TaskEvent('start', $thetask) );
92
93
            $pid = $thetask->getPid();
94
95
            $this->openWorklog(
96
                $pid,
97
                $name,
98
                $jid,
99
                $task,
100
                $parameters,
101
                $this->stopwatch->getStartTime()
102
            );
103
104
            $this->events->emit( new TaskStatusEvent('start', $thetask) );
105
106
            try {
107
108
                $result = $thetask->run();
109
110
                $status = Worklog::STATUS_FINISHED;
111
112
            } catch (TaskException $te) {
113
114
                $status = Worklog::STATUS_ABORTED;
115
116
                $result = $te->getMessage();
117
118
            } catch (Exception $e) {
119
120
                $status = Worklog::STATUS_ERROR;
121
122
                $result = $e->getMessage();
123
124
            }
125
126
            $this->events->emit( new TaskStatusEvent($status ? 'success' : 'error', $thetask) );
127
128
            $this->events->emit( new TaskStatusEvent('stop', $thetask) );
129
130
            $this->events->emit( new TaskEvent('stop', $thetask) );
131
132
            $this->stopwatch->stop();
133
134
            $this->closeWorklog($status, $result, $this->stopwatch->getStopTime());
135
136
            $drift = $this->stopwatch->getDrift()->format('%s');
137
138
            $this->logger->notice("Task $name ($task) with pid $pid ends in ".($status ? 'success' : 'error')." in $drift secs");
139
140
        } catch (Exception $e) {
141
142
            throw $e;
143
144
        }
145
146
        return new Result(
147
            array (
148
                $pid,
149
                $name,
150
                $status,
151
                $this->stopwatch->getStartTime(),
152
                $this->stopwatch->getStopTime(),
153
                $result,
154
                $this->worklog->getId()
155
            )
156
        );
157
158
    }
159
160
    protected function openWorklog(
161
        $pid,
162
        $name,
163
        $jid,
164
        $task,
165
        $parameters,
166
        $start
167
    ) {
168
169
        $this->worklog
170
            ->setPid($pid)
171
            ->setName($name)
172
            ->setStatus(Worklog::STATUS_RUNNING)
173
            ->setJid($jid)
174
            ->setTask($task)
175
            ->setParameters($parameters)
176
            ->setStartTime($start);
177
178
        $this->em->persist($this->worklog);
179
        $this->em->flush();
180
181
    }
182
183
    protected function closeWorklog(
184
        $status,
185
        $result,
186
        $end
187
    ) {
188
189
        $this->worklog
190
            ->setStatus($status)
191
            ->setResult($result)
192
            ->setEndTime($end);
193
194
        $this->em->persist($this->worklog);
195
        $this->em->flush();
196
197
    }
198
199
}
200