Completed
Push — 2.0 ( dbbe01...d43752 )
by Marco
11:55
created

ScheduleWorker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 15.91 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 12
dl 14
loc 88
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A spinup() 14 14 1
A loop() 0 47 3
A spindown() 0 5 1
A refreshPlans() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Extender\Workers;
2
3
use \Comodojo\Daemon\Worker\AbstractWorker;
4
use \Comodojo\Extender\Task\Manager as TaskManager;
5
use \Comodojo\Extender\Task\Locker;
6
use \Comodojo\Extender\Schedule\Manager as ScheduleManager;
7
use \Comodojo\Foundation\Base\ConfigurationTrait;
8
use \Comodojo\Extender\Traits\TasksTableTrait;
9
use \Comodojo\Extender\Traits\WorkerTrait;
10
use \Comodojo\Foundation\Logging\LoggerTrait;
11
use \Comodojo\Foundation\Events\EventsTrait;
12
13
class ScheduleWorker extends AbstractWorker {
14
15
    use ConfigurationTrait;
16
    use LoggerTrait;
17
    use EventsTrait;
18
    use TasksTableTrait;
19
    use WorkerTrait;
20
21
    protected $locker;
22
23
    protected $wakeup_time = 0;
24
25 View Code Duplication
    public function spinup() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
27
        $configuration = $this->getConfiguration();
28
29
        $base_path = $configuration->get('base-path');
30
        $lock_path = $configuration->get('run-path');
31
        $lock_file = "$base_path/$lock_path/schedule.worker.lock";
32
33
        $this->locker = new Locker($lock_file);
34
        $this->locker->lock([]);
35
36
        $this->getEvents()->subscribe('daemon.worker.refresh', '\Comodojo\Extender\Listeners\RefreshScheduler');
37
38
    }
39
40
    public function loop() {
41
42
        if ( $this->wakeup_time > time() ) {
43
            $this->logger->debug('Still in sleep time, next planned wakeup is '.date('r', $this->wakeup_time));
44
            return;
45
        }
46
47
        $schedule_manager = new ScheduleManager(
48
            $this->getConfiguration(),
49
            $this->getLogger(),
50
            $this->getEvents()
51
        );
52
53
        $task_manager = new TaskManager(
54
            $this->locker,
55
            $this->getConfiguration(),
56
            $this->getLogger(),
57
            $this->getTasksTable(),
58
            $this->getEvents()
59
        );
60
61
        $jobs = $schedule_manager->getAll(true);
62
63
        if ( empty($jobs) ) {
64
65
            $this->logger->debug('Nothing to do right now, sleeping... zzZZzZzZzz');
66
67
        } else {
68
69
            $this->logger->debug(count($jobs)." jobs will be executed");
70
71
            $requests = $this->jobsToRequests($jobs);
72
73
            $results = $task_manager->addBulk($requests)->run();
74
75
            $schedule_manager->updateFromResults($results);
76
77
        }
78
79
        $this->wakeup_time = $schedule_manager->getNextCycleTimestamp();
80
81
        unset($task_manager);
82
        unset($schedule_manager);
83
84
        $this->locker->lock([]);
85
86
    }
87
88
    public function spindown() {
89
90
        $this->locker->release();
91
92
    }
93
94
    public function refreshPlans() {
95
96
        $this->wakeup_time = 0;
97
98
    }
99
100
}
101