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