Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
created

Locker::isAborted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Locker::countRunning() 0 5 1
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Extender\Traits\ConfigurationTrait;
4
use \Comodojo\Foundation\Base\Configuration;
5
use \Comodojo\Daemon\Traits\LoggerTrait;
6
use \Psr\Log\LoggerInterface;
7
8
/**
9
 * @package     Comodojo Extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class Locker {
25
26
    use ConfigurationTrait;
27
    use LoggerTrait;
28
29
    /**
30
     * @var array
31
     */
32
    private $queued = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $running = [];
38
39
    /**
40
     * @var array
41
     */
42
    private $completed = [];
43
44
    /**
45
     * @var string
46
     */
47
    private $lock_file;
48
49
    /**
50
     * Manager constructor
51
     *
52
     * @param string $name
53
     * @param Configuration $configuration
54
     * @param LoggerInterface $logger
55
     */
56
    public function __construct($name, Configuration $configuration, LoggerInterface $logger) {
57
58
        $this->setConfiguration($configuration);
59
        $this->setLogger($logger);
60
61
        $lock_path = $configuration->get('lockfiles-path');
62
        $this->lock_file = "$lock_path/$name.lock";
63
64
    }
65
66
    public function getQueued() {
67
68
        return $this->queued;
69
70
    }
71
72
    public function setQueued(Request $request) {
73
74
        $uid = $request->getUid();
75
76
        $this->logger->debug("Adding task with uid $uid to queue");
77
78
        $this->queued[$uid] = $request;
79
80
        $this->dump();
81
82
        return $this;
83
84
    }
85
86
    public function countQueued() {
87
88
        return count($this->queued);
89
90
    }
91
92
    public function getRunning() {
93
94
        return $this->running;
95
96
    }
97
98
    public function setRunning($uid, $pid) {
99
100
        $request = $this->queued[$uid];
101
102
        $this->logger->debug("Task ".$request->getName()." (uid $uid) is starting with pid $pid");
103
104
        $request->setPid($pid);
105
        $request->setStartTimestamp(microtime(true));
106
107
        $this->running[$uid] = $request;
108
109
        unset($this->queued[$uid]);
110
111
        $this->dump();
112
113
        return $this;
114
115
    }
116
117
    public function countRunning() {
118
119
        return count($this->running);
120
121
    }
122
123
    public function getCompleted() {
124
125
        return $this->completed;
126
127
    }
128
129 View Code Duplication
    public function setCompleted($uid, Result $result) {
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...
130
131
        $request = $this->running[$uid];
132
133
        $this->logger->debug("Task ".$request->getName()." (uid $uid) completed with ".($result->success ? 'success' : 'error'));
0 ignored issues
show
Documentation introduced by
The property success does not exist on object<Comodojo\Extender\Task\Result>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
135
        $this->completed[$uid] = $result;
136
137
        unset($this->running[$uid]);
138
139
        $this->dump();
140
141
        return $this;
142
143
    }
144
145 View Code Duplication
    public function setAborted($uid, Result $result) {
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...
146
147
        $request = $this->queued[$uid];
148
149
        $this->logger->debug("Task ".$request->getName()." (uid $uid) aborted: internal error");
150
151
        $this->completed[$uid] = $result;
152
153
        unset($this->queued[$uid]);
154
155
        $this->dump();
156
157
        return $this;
158
159
    }
160
161
    public function getSucceeded() {
162
163
        return array_filter($this->completed, function($result) {
164
            return $result->success;
165
        });
166
167
    }
168
169
    public function getFailed() {
170
171
        return array_filter($this->completed, function($result) {
172
            return !$result->success;
173
        });
174
175
    }
176
177
    public function freeCompleted() {
178
179
        $this->completed = array_filter($this->completed, function($result) {
180
            return $result->success;
181
        });
182
183
    }
184
185
    public function free() {
186
187
        $this->queued = [];
188
        $this->running = [];
189
        $this->completed = [];
190
191
        $this->dump();
192
193
    }
194
195
    public function release() {
196
197
        $lock = file_exists($this->lock_file) ? unlink($this->lock_file) : true;
198
199
        return $lock;
200
201
    }
202
203
    private function dump() {
204
205
        return @file_put_contents($this->lock_file, serialize(
206
            [
207
                'QUEUED' => count($this->getQueued()),
208
                'RUNNING' => count($this->getRunning()),
209
                'COMPLETED' => count($this->getCompleted()),
210
                'SUCCEEDED' => count($this->getSucceeded()),
211
                'FAILED' => count($this->getFailed())
212
            ]
213
        ));
214
215
    }
216
217
    public static function create($name, Configuration $configuration, LoggerInterface $logger) {
218
219
        return new Locker($name, $configuration, $logger);
220
221
    }
222
223
}
224