Completed
Push — 2.0 ( dd3689...ee0168 )
by Marco
12:16
created

Manager::queued()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Comodojo\Extender\Jobs;
2
3
use \Comodojo\Dispatcher\Components\Configuration;
4
use \Psr\Log\LoggerInterface;
5
6
/**
7
 * Job object
8
 *
9
 * @package     Comodojo extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class Manager {
30
31
    private $logger;
32
33
    private $running_jobs = array();
34
35
    private $completed_jobs = array();
36
37
    private $queued_jobs = array();
38
39
    private $queue_file = 'extender.queue';
40
41
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
42
43
        $queue_file = $configuration->get('queue-file');
44
45
        if ( $queue_file !== null ) $this->queue_file = $queue_file;
46
47
        $this->logger = $logger;
48
49
    }
50
51
    public function isQueued(Job $job) {
52
53
        $this->logger->debug('Adding job '.$job->name.' (uid '.$job->uid.') to queue');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Comodojo\Extender\Jobs\Job>. 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...
Documentation introduced by
The property uid does not exist on object<Comodojo\Extender\Jobs\Job>. 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...
54
55
        $uid = $job->uid;
0 ignored issues
show
Documentation introduced by
The property uid does not exist on object<Comodojo\Extender\Jobs\Job>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
56
57
        $this->queued_jobs[$uid] = $job;
58
59
        $this->dump();
60
61
        return true;
62
63
    }
64
65
    public function isStarting($uid, $pid) {
66
67
        $job = $this->queued_jobs[$uid];
68
69
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') is starting with pid '.$pid);
70
71
        $job->pid = $pid;
72
73
        $job->start_timestamp = microtime(true);
74
75
        $this->running_jobs[$uid] = $job;
76
77
        unset($this->queued_jobs[$uid]);
78
79
        $this->dump();
80
81
        return $this;
82
83
    }
84
85
    public function isCompleted($uid, $success, $result, $wid = null) {
86
87
        $job = $this->running_jobs[$uid];
88
89
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') completed with '.($success ? 'success' : 'error'));
90
91
        $job->success = $success;
92
93
        $job->result = $result;
94
95
        $job->wid = $wid;
96
97
        $job->end_timestamp = microtime(true);
98
99
        $this->completed_jobs[$uid] = $job;
100
101
        unset($this->running_jobs[$uid]);
102
103
        $this->dump();
104
105
        return $this;
106
107
    }
108
109
    public function isAborted($uid, $error) {
110
111
        $job = $this->running_jobs[$uid];
112
113
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') aborted, reason: '.$error);
114
115
        $job->success = false;
116
117
        $job->result = $error;
118
119
        $job->wid = null;
120
121
        $job->end_timestamp = microtime(true);
122
123
        $this->completed_jobs[$uid] = $job;
124
125
        unset($this->queued_jobs[$uid]);
126
127
        $this->dump();
128
129
        return $this;
130
131
    }
132
133
    public function queued() {
134
135
        return $this->queued_jobs;
136
137
    }
138
139
    public function running() {
140
141
        return $this->running_jobs;
142
143
    }
144
145
    public function completed() {
146
147
        return $this->completed_jobs;
148
149
    }
150
151
    public function free() {
152
153
        $this->queued_jobs = array();
154
        $this->running_jobs = array();
155
        $this->completed_jobs = array();
156
157
        $this->dump();
158
159
    }
160
161
    public function release() {
162
163
        $lock = file_exists($this->queue_file) ? unlink($this->queue_file) : true;
164
165
        return $lock;
166
167
    }
168
169
    private function dump() {
170
171
        $data = array(
172
            'QUEUED' => count($this->queued_jobs),
173
            'RUNNING' => count($this->running_jobs),
174
            'COMPLETED' => count($this->completed_jobs)
175
        );
176
177
        $content = serialize($data);
178
179
        return file_put_contents($this->queue_file, $content);
180
181
    }
182
183
}
184