QueueWorker::spindown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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
use \Comodojo\Extender\Events\QueueEvent;
13
14
/**
15
 * @package     Comodojo Extender
16
 * @author      Marco Giovinazzi <[email protected]>
17
 * @license     MIT
18
 *
19
 * LICENSE:
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 */
29
30
class QueueWorker extends AbstractWorker {
31
32
    use ConfigurationTrait;
33
    use LoggerTrait;
34
    use EventsTrait;
35
    use TasksTableTrait;
36
    use WorkerTrait;
37
38
    protected $locker;
39
40
    public function spinup() {
41
42
        $configuration = $this->getConfiguration();
43
44
        $base_path = $configuration->get('base-path');
45
        $lock_path = $configuration->get('run-path');
46
        $lock_file = "$base_path/$lock_path/queue.worker.lock";
47
48
        $this->locker = new Locker($lock_file);
49
        $this->locker->lock([]);
50
51
    }
52
53
    public function loop() {
54
55
        $configuration = $this->getConfiguration();
56
        $logger = $this->getLogger();
57
        $events = $this->getEvents();
58
59
        $queue_manager = new QueueManager(
60
            $configuration,
61
            $logger,
62
            $events
63
        );
64
65
        $queue = $queue_manager->get();
66
67
        if ( !empty($queue) ) {
68
69
            $events->emit( new QueueEvent('process', null, $queue) );
70
71
            $requests = $this->jobsToRequests($queue);
72
73
            $queue_manager->flush($queue);
74
            unset($queue_manager);
75
76
            $task_manager = new TaskManager(
77
                $this->locker,
78
                $configuration,
79
                $logger,
80
                $this->getTasksTable(),
81
                $events
82
            );
83
84
            $result = $task_manager->addBulk($requests)->run();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
85
            unset($task_manager);
86
87
        } else {
88
89
            unset($queue_manager);
90
91
        }
92
93
        $this->locker->lock([]);
94
95
    }
96
97
    public function spindown() {
98
99
        $this->locker->release();
100
101
    }
102
103
}
104