Completed
Push — master ( e481c7...bcdb1b )
by Marco
35:35 queued 20:00
created

Tracker::countRunning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Foundation\Base\ConfigurationTrait;
4
use \Comodojo\Foundation\Base\Configuration;
5
use \Comodojo\Foundation\Logging\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 Tracker {
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
     * Tracker constructor
46
     *
47
     * @param string $name
48
     * @param Configuration $configuration
49
     * @param LoggerInterface $logger
50
     */
51
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
52
53
        $this->setConfiguration($configuration);
54
        $this->setLogger($logger);
55
56
    }
57
58
    public function getQueued() {
59
60
        return $this->queued;
61
62
    }
63
64
    public function setQueued(Request $request) {
65
66
        $uid = $request->getUid();
67
68
        $this->logger->debug("Adding task with uid $uid to queue");
69
70
        $this->queued[$uid] = $request;
71
72
        return $this;
73
74
    }
75
76
    public function countQueued() {
77
78
        return count($this->queued);
79
80
    }
81
82
    public function getRunning() {
83
84
        return $this->running;
85
86
    }
87
88
    public function setRunning($uid, $pid) {
89
90
        $request = $this->queued[$uid];
91
92
        $this->logger->debug("Task ".$request->getName()." (uid $uid) is starting with pid $pid");
93
94
        $request->setPid($pid);
95
        $request->setStartTimestamp(microtime(true));
96
97
        $this->running[$uid] = $request;
98
99
        unset($this->queued[$uid]);
100
101
        return $this;
102
103
    }
104
105
    public function countRunning() {
106
107
        return count($this->running);
108
109
    }
110
111
    public function getCompleted() {
112
113
        return $this->completed;
114
115
    }
116
117
    public function countCompleted() {
118
119
        return count($this->completed);
120
121
    }
122
123
    public function setCompleted($uid, Result $result) {
124
125
        $request = $this->running[$uid];
126
127
        $this->logger->debug("Task ".$request->getName()." (uid $uid) completed with ".($result->success ? 'success' : 'error'));
0 ignored issues
show
Bug Best Practice introduced by
The property success does not exist on Comodojo\Extender\Task\Result. Since you implemented __get, consider adding a @property annotation.
Loading history...
128
129
        $this->completed[$uid] = $result;
130
131
        unset($this->running[$uid]);
132
133
        return $this;
134
135
    }
136
137
    public function setAborted($uid, Result $result) {
138
139
        $request = $this->queued[$uid];
140
141
        $this->logger->debug("Task ".$request->getName()." (uid $uid) aborted: ".$result->message);
0 ignored issues
show
Bug Best Practice introduced by
The property message does not exist on Comodojo\Extender\Task\Result. Since you implemented __get, consider adding a @property annotation.
Loading history...
142
143
        $this->completed[$uid] = $result;
144
145
        unset($this->queued[$uid]);
146
147
        return $this;
148
149
    }
150
151
    public function getSucceeded() {
152
153
        return array_filter($this->completed, function($result) {
154
            return $result->success;
155
        });
156
157
    }
158
159
    public function countSucceeded() {
160
161
        return count($this->getSucceeded());
162
163
    }
164
165
    public function getFailed() {
166
167
        return array_filter($this->completed, function($result) {
168
            return !$result->success;
169
        });
170
171
    }
172
173
    public function countFailed() {
174
175
        return count($this->getFailed());
176
177
    }
178
179
    public static function create(Configuration $configuration, LoggerInterface $logger) {
180
181
        return new Tracker($configuration, $logger);
182
183
    }
184
185
}
186