Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.50% covered (success)
97.50%
39 / 40
92.86% covered (success)
92.86%
13 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Worker
97.50% covered (success)
97.50%
39 / 40
92.86% covered (success)
92.86%
13 / 14
29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPriority
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getPriority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFifo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFilo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addJob
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addJobs
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getJobs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getJob
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasJobs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasJob
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasNextJob
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 processNext
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
11.02
 getNextIndex
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Queue\Processor;
15
16use Pop\Queue\Queue;
17use Pop\Queue\Processor\Jobs\AbstractJob;
18
19/**
20 * Worker class
21 *
22 * @category   Pop
23 * @package    Pop\Queue
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    1.2.0
28 */
29class Worker extends AbstractProcessor
30{
31
32    /**
33     * Worker priority constants
34     */
35    const FIFO = 'FIFO'; // Same as LILO
36    const FILO = 'FILO'; // Same as LIFO
37
38    /**
39     * Worker type
40     * @var string
41     */
42    protected $priority = 'FIFO';
43
44    /**
45     * Worker jobs
46     * @var AbstractJob[]
47     */
48    protected $jobs = [];
49
50    /**
51     * Constructor
52     *
53     * Instantiate the worker object
54     *
55     * @param  string $priority
56     */
57    public function __construct($priority = 'FIFO')
58    {
59        $this->setPriority($priority);
60    }
61
62    /**
63     * Set worker priority
64     *
65     * @param  string $priority
66     * @return Worker
67     */
68    public function setPriority($priority = 'FIFO')
69    {
70        if (defined('self::' . $priority)) {
71            $this->priority = $priority;
72        }
73        return $this;
74    }
75
76    /**
77     * Get worker priority
78     *
79     * @return string
80     */
81    public function getPriority()
82    {
83        return $this->priority;
84    }
85
86    /**
87     * Is worker fifo
88     *
89     * @return boolean
90     */
91    public function isFifo()
92    {
93        return ($this->priority == self::FIFO);
94    }
95
96    /**
97     * Is worker filo
98     *
99     * @return boolean
100     */
101    public function isFilo()
102    {
103        return ($this->priority == self::FILO);
104    }
105
106    /**
107     * Add job
108     *
109     * @param  AbstractJob $job
110     * @return Worker
111     */
112    public function addJob(AbstractJob $job)
113    {
114        if ($this->isFilo()) {
115            array_unshift($this->jobs, $job);
116        } else {
117            $this->jobs[] = $job;
118        }
119        return $this;
120    }
121
122    /**
123     * Add jobs
124     *
125     * @param  array $jobs
126     * @return Worker
127     */
128    public function addJobs(array $jobs)
129    {
130        foreach ($jobs as $job) {
131            $this->addJob($job);
132        }
133        return $this;
134    }
135
136    /**
137     * Get jobs
138     *
139     * @return array
140     */
141    public function getJobs()
142    {
143        return $this->jobs;
144    }
145
146    /**
147     * Get job
148     *
149     * @param  int $index
150     * @return AbstractJob
151     */
152    public function getJob($index)
153    {
154        return (isset($this->jobs[$index])) ? $this->jobs[$index] : null;
155    }
156
157    /**
158     * Has jobs
159     *
160     * @return boolean
161     */
162    public function hasJobs()
163    {
164        return (count($this->jobs) > 0);
165    }
166
167    /**
168     * Has job
169     *
170     * @param  int $index
171     * @return boolean
172     */
173    public function hasJob($index)
174    {
175        return (isset($this->jobs[$index]));
176    }
177
178    /**
179     * Has next job
180     *
181     * @return boolean
182     */
183    public function hasNextJob()
184    {
185        $current = key($this->jobs);
186        return ((null !== $current) && ($current < count($this->jobs)));
187    }
188
189    /**
190     * Process next job
191     *
192     * @param  Queue $queue
193     * @return int
194     */
195    public function processNext(Queue $queue = null)
196    {
197        $nextIndex = $this->getNextIndex();
198
199        if ($this->hasJob($nextIndex)) {
200            try {
201                $application = ((null !== $queue) && (null !== $queue->hasApplication())) ? $queue->application() : null;
202                $this->results[$nextIndex] = $this->jobs[$nextIndex]->run($application);
203                $this->jobs[$nextIndex]->setAsCompleted();
204                $this->completed[$nextIndex] = $this->jobs[$nextIndex];
205
206                if ((null !== $queue) && ($this->jobs[$nextIndex]->hasJobId()) &&
207                    ($queue->adapter()->hasJob($this->jobs[$nextIndex]->getJobId()))) {
208                    $queue->adapter()->updateJob($this->jobs[$nextIndex]->getJobId(), true, true);
209                }
210            } catch (\Exception $e) {
211                $this->jobs[$nextIndex]->setAsFailed();
212                $this->failed[$nextIndex]           = $this->jobs[$nextIndex];
213                $this->failedExceptions[$nextIndex] = $e;
214                if ((null !== $queue) && ($this->failed[$nextIndex]->hasJobId()) &&
215                    ($queue->adapter()->hasJob($this->failed[$nextIndex]->getJobId()))) {
216                    $queue->adapter()->failed($queue->getName(), $this->failed[$nextIndex]->getJobId(), $e);
217                }
218            }
219        }
220
221        return $nextIndex;
222    }
223
224    /**
225     * Get next index
226     *
227     * @return int
228     */
229    public function getNextIndex()
230    {
231        $index = key($this->jobs);
232        next($this->jobs);
233        return $index;
234    }
235
236}