Locker   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueued() 0 3 1
A getRunning() 0 3 1
A getAborted() 0 3 1
A getFailed() 0 3 1
A getSucceeded() 0 3 1
A getCompleted() 0 3 1
A release() 0 3 1
B updateCounters() 0 16 9
A lock() 0 11 1
A updateUsage() 0 6 1
A __construct() 0 5 1
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Daemon\Locker\AbstractLocker;
4
use \Comodojo\Daemon\Utils\ProcessTools;
5
6
/**
7
 * @package     Comodojo Extender
8
 * @author      Marco Giovinazzi <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22
class Locker extends AbstractLocker {
23
24
    protected $usage = [
25
        'STARTTIMESTAMP' => null,
26
        'PID' => null,
27
        'MEMORYUSAGE' => null,
28
        'MEMORYPEAKUSAGE' => null
29
    ];
30
31
    protected $counters = [
32
        'QUEUED' => 0,
33
        'RUNNING' => 0,
34
        'COMPLETED' => 0,
35
        'SUCCEEDED' => 0,
36
        'FAILED' => 0,
37
        'ABORTED' => 0
38
    ];
39
40
    /**
41
     * Lock file name
42
     *
43
     * @var string
44
     */
45
    private $lockfile;
46
47
    public function __construct($lockfile) {
48
49
        $this->lockfile = $lockfile;
50
        $this->usage_data['STARTTIMESTAMP'] = time();
0 ignored issues
show
Bug Best Practice introduced by
The property usage_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        $this->usage_data['PID'] = ProcessTools::getPid();
52
53
    }
54
55
    public function getQueued() {
56
57
        return $this->counters['QUEUED'];
58
59
    }
60
61
    public function getRunning() {
62
63
        return $this->counters['RUNNING'];
64
65
    }
66
67
    public function getCompleted() {
68
69
        return $this->counters['COMPLETED'];
70
71
    }
72
73
    public function getSucceeded() {
74
75
        return $this->counters['SUCCEEDED'];
76
77
    }
78
79
    public function getFailed() {
80
81
        return $this->counters['FAILED'];
82
83
    }
84
85
    public function getAborted() {
86
87
        return $this->counters['ABORTED'];
88
89
    }
90
91
    public function lock($what) {
92
93
        $usage = $this->updateUsage();
94
        $counters = $this->updateCounters($what);
95
96
        $data = serialize([
97
            'USAGE' => $usage,
98
            'COUNTERS' => $counters
99
        ]);
100
101
        return self::writeLock($this->lockfile, $data);
102
103
    }
104
105
    public function release() {
106
107
        return self::releaseLock($this->lockfile);
108
109
    }
110
111
    protected function updateUsage() {
112
113
        $this->usage_data['MEMORYUSAGE'] = memory_get_usage();
0 ignored issues
show
Bug Best Practice introduced by
The property usage_data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        $this->usage_data['MEMORYPEAKUSAGE'] = memory_get_peak_usage();
115
116
        return $this->usage_data;
117
118
    }
119
120
    protected function updateCounters($counters) {
121
122
        if ( isset($counters['QUEUED']) && is_int($counters['QUEUED']) ) $this->counters['QUEUED'] = $counters['QUEUED'];
123
        if ( isset($counters['RUNNING']) && is_int($counters['RUNNING']) ) $this->counters['RUNNING'] = $counters['RUNNING'];
124
        if ( !empty($counters['COMPLETED']) ) $this->counters['COMPLETED'] = $this->counters['COMPLETED'] + (int) $counters['COMPLETED'];
125
        if ( !empty($counters['SUCCEEDED']) ) $this->counters['SUCCEEDED'] = $this->counters['SUCCEEDED'] + (int) $counters['SUCCEEDED'];
126
        if ( !empty($counters['FAILED']) ) $this->counters['FAILED'] = $this->counters['FAILED'] + (int) $counters['FAILED'];
127
        if ( !empty($counters['ABORTED']) ) $this->counters['ABORTED'] = $this->counters['ABORTED'] + (int) $counters['ABORTED'];
128
129
        // array_walk($this->counters, function(&$counter, $name) use ($counters) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
        //
131
        //     if ( !empty($counters[$name]) ) $counter = $counter + (int) $counters[$name];
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
        //
133
        // });
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
135
        return $this->counters;
136
137
    }
138
139
}
140