1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\TaskBundle\Task; |
4
|
|
|
|
5
|
|
|
use Glooby\TaskBundle\Entity\QueuedTask; |
6
|
|
|
use Glooby\TaskBundle\Manager\TaskManager; |
7
|
|
|
use Glooby\TaskBundle\Model\QueuedTaskInterface; |
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Emil Kilhage |
14
|
|
|
*/ |
15
|
|
|
class TaskRunner |
16
|
|
|
{ |
17
|
|
|
use ContainerAwareTrait; |
18
|
|
|
use LoggerAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var OutputInterface |
22
|
|
|
*/ |
23
|
|
|
protected $output; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param OutputInterface $output |
27
|
|
|
*/ |
28
|
|
|
public function setOutput(OutputInterface $output) |
29
|
|
|
{ |
30
|
|
|
$this->output = $output; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TaskManager |
35
|
|
|
*/ |
36
|
|
|
private $taskManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param TaskManager $taskManager |
40
|
|
|
*/ |
41
|
|
|
public function setTaskManager(TaskManager $taskManager) |
42
|
|
|
{ |
43
|
|
|
$this->taskManager = $taskManager; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param QueuedTask $run |
48
|
|
|
* @return array |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function run(QueuedTask $run) |
52
|
|
|
{ |
53
|
|
|
$task = $this->container->get($run->getName()); |
54
|
|
|
|
55
|
|
|
if (!($task instanceof TaskInterface)) { |
56
|
|
|
throw new \InvalidArgumentException($run->getName().' does not implement TaskInterface'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->taskManager->start($run); |
60
|
|
|
|
61
|
|
|
return $this->execute($task, $run->getParams(), $run); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @param array $params |
67
|
|
|
* @return array |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
|
public function runTask($name, array $params = []) |
71
|
|
|
{ |
72
|
|
|
$task = $this->container->get($name); |
73
|
|
|
|
74
|
|
|
if (!($task instanceof TaskInterface)) { |
75
|
|
|
throw new \InvalidArgumentException($name.' does not implement TaskInterface'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$run = $this->taskManager->run($name, $params); |
79
|
|
|
|
80
|
|
|
return $this->execute($task, $params, $run); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param TaskInterface $task |
85
|
|
|
* @param array $params |
86
|
|
|
* @param QueuedTaskInterface $run |
87
|
|
|
* @return mixed |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
protected function execute(TaskInterface $task, array $params, QueuedTaskInterface $run) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
if (count($params) > 0) { |
94
|
|
|
$response = $task->run($params); |
95
|
|
|
} else { |
96
|
|
|
$response = $task->run(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->taskManager->success($run, $response); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$this->logger->error("$e"); |
102
|
|
|
$this->taskManager->failure($run, "$e"); |
103
|
|
|
throw $e; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $response; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|