|
1
|
|
|
<?php namespace Comodojo\Extender\Workers; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Daemon\Worker\AbstractWorker; |
|
4
|
|
|
use \Comodojo\Foundation\Logging\LoggerTrait; |
|
5
|
|
|
use \Comodojo\Foundation\Events\EventsTrait; |
|
6
|
|
|
use \Comodojo\Extender\Queue\Manager as QueueManager; |
|
7
|
|
|
use \Comodojo\Extender\Task\Locker; |
|
8
|
|
|
use \Comodojo\Extender\Task\Manager as TaskManager; |
|
9
|
|
|
use \Comodojo\Extender\Traits\TasksTableTrait; |
|
10
|
|
|
use \Comodojo\Foundation\Base\ConfigurationTrait; |
|
11
|
|
|
use \Comodojo\Extender\Traits\WorkerTrait; |
|
12
|
|
|
|
|
13
|
|
|
class QueueWorker extends AbstractWorker { |
|
14
|
|
|
|
|
15
|
|
|
use ConfigurationTrait; |
|
16
|
|
|
use LoggerTrait; |
|
17
|
|
|
use EventsTrait; |
|
18
|
|
|
use TasksTableTrait; |
|
19
|
|
|
use WorkerTrait; |
|
20
|
|
|
|
|
21
|
|
|
protected $locker; |
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
public function spinup() { |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
$configuration = $this->getConfiguration(); |
|
26
|
|
|
|
|
27
|
|
|
$base_path = $configuration->get('base-path'); |
|
28
|
|
|
$lock_path = $configuration->get('run-path'); |
|
29
|
|
|
$lock_file = "$base_path/$lock_path/queue.worker.lock"; |
|
30
|
|
|
|
|
31
|
|
|
$this->locker = new Locker($lock_file); |
|
32
|
|
|
$this->locker->lock([]); |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function loop() { |
|
37
|
|
|
|
|
38
|
|
|
$task_manager = new TaskManager( |
|
39
|
|
|
$this->locker, |
|
40
|
|
|
$this->getConfiguration(), |
|
41
|
|
|
$this->getLogger(), |
|
42
|
|
|
$this->getTasksTable(), |
|
43
|
|
|
$this->getEvents() |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
$queue_manager = new QueueManager( |
|
47
|
|
|
$this->getConfiguration(), |
|
48
|
|
|
$this->getLogger(), |
|
49
|
|
|
$this->getEvents() |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$queue = $queue_manager->get(); |
|
53
|
|
|
|
|
54
|
|
|
if ( !empty($queue) ) { |
|
55
|
|
|
|
|
56
|
|
|
$requests = $this->jobsToRequests($queue); |
|
57
|
|
|
|
|
58
|
|
|
$queue_manager->remove($queue); |
|
59
|
|
|
unset($queue_manager); |
|
60
|
|
|
|
|
61
|
|
|
$result = $task_manager->addBulk($requests)->run(); |
|
|
|
|
|
|
62
|
|
|
unset($task_manager); |
|
63
|
|
|
|
|
64
|
|
|
} else { |
|
65
|
|
|
|
|
66
|
|
|
unset($queue_manager); |
|
67
|
|
|
unset($task_manager); |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->locker->lock([]); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function spindown() { |
|
78
|
|
|
|
|
79
|
|
|
$this->locker->release(); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.