Completed
Push — master ( ae6c2c...0a100a )
by Wachter
01:52
created

TaskRunner::runTasks()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 35
cts 35
cp 1
rs 9.0303
c 0
b 0
f 0
cc 3
eloc 30
nc 7
nop 0
crap 3
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\Runner;
13
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Task\Event\Events;
16
use Task\Event\TaskExecutionEvent;
17
use Task\Handler\TaskHandlerFactoryInterface;
18
use Task\Storage\TaskExecutionRepositoryInterface;
19
use Task\TaskStatus;
20
21
/**
22
 * Executes scheduled tasks.
23
 */
24
class TaskRunner implements TaskRunnerInterface
25
{
26
    /**
27
     * @var TaskExecutionRepositoryInterface
28
     */
29
    private $taskExecutionRepository;
30
31
    /**
32
     * @var TaskHandlerFactoryInterface
33
     */
34
    private $taskHandlerFactory;
35
36
    /**
37
     * @var EventDispatcherInterface
38
     */
39
    private $eventDispatcher;
40
41
    /**
42
     * @param TaskExecutionRepositoryInterface $executionRepository
43
     * @param TaskHandlerFactoryInterface $taskHandlerFactory
44
     * @param EventDispatcherInterface $eventDispatcher
45
     */
46 2
    public function __construct(
47
        TaskExecutionRepositoryInterface $executionRepository,
48
        TaskHandlerFactoryInterface $taskHandlerFactory,
49
        EventDispatcherInterface $eventDispatcher
50
    ) {
51 2
        $this->taskExecutionRepository = $executionRepository;
52 2
        $this->taskHandlerFactory = $taskHandlerFactory;
53 2
        $this->eventDispatcher = $eventDispatcher;
54 2
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2
    public function runTasks()
60
    {
61 2
        $executions = $this->taskExecutionRepository->findScheduled();
62
63 2
        foreach ($executions as $execution) {
64 2
            $handler = $this->taskHandlerFactory->create($execution->getHandlerClass());
65
66 2
            $start = microtime(true);
67 2
            $execution->setStartTime(new \DateTime());
68 2
            $execution->setStatus(TaskStatus::RUNNING);
69 2
            $this->taskExecutionRepository->save($execution);
70
71
            try {
72 2
                $this->eventDispatcher->dispatch(
73 2
                    Events::TASK_BEFORE,
74 2
                    new TaskExecutionEvent($execution->getTask(), $execution)
75 2
                );
76
77 2
                $result = $handler->handle($execution->getWorkload());
78
79 2
                $execution->setStatus(TaskStatus::COMPLETED);
80 2
                $execution->setResult($result);
81
82 2
                $this->eventDispatcher->dispatch(
83 2
                    Events::TASK_PASSED,
84 2
                    new TaskExecutionEvent($execution->getTask(), $execution)
85 2
                );
86 2
            } catch (\Exception $ex) {
87 1
                $execution->setException($ex->__toString());
88 1
                $execution->setStatus(TaskStatus::FAILED);
89
90 1
                $this->eventDispatcher->dispatch(
91 1
                    Events::TASK_FAILED,
92 1
                    new TaskExecutionEvent($execution->getTask(), $execution)
93 1
                );
94
            }
95
96 2
            $execution->setEndTime(new \DateTime());
97 2
            $execution->setDuration(microtime(true) - $start);
98
99 2
            $this->eventDispatcher->dispatch(
100 2
                Events::TASK_FINISHED,
101 2
                new TaskExecutionEvent($execution->getTask(), $execution)
102 2
            );
103 2
            $this->taskExecutionRepository->save($execution);
104 2
        }
105 2
    }
106
}
107