Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.10% covered (success)
87.10%
27 / 31
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scheduler
87.10% covered (success)
87.10%
27 / 31
87.50% covered (success)
87.50%
7 / 8
24.14
0.00% covered (danger)
0.00%
0 / 1
 addJob
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addSchedule
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addSchedules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getSchedules
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSchedule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasSchedules
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasSchedule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 processNext
78.95% covered (success)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 1
15.83
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\Schedule;
18use Pop\Queue\Processor\Jobs\AbstractJob;
19
20/**
21 * Scheduler class
22 *
23 * @category   Pop
24 * @package    Pop\Queue
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    1.2.0
29 */
30class Scheduler extends AbstractProcessor
31{
32
33    /**
34     * Job schedules
35     * @var Schedule[]
36     */
37    protected $schedules = [];
38
39    /**
40     * Add job
41     *
42     * @param  AbstractJob $job
43     * @return Schedule
44     */
45    public function addJob(AbstractJob $job)
46    {
47        $schedule          = new Schedule(($job));
48        $this->schedules[] = $schedule;
49
50        return $schedule;
51    }
52
53    /**
54     * Add schedule
55     *
56     * @param  Schedule $schedule
57     * @return Scheduler
58     */
59    public function addSchedule(Schedule $schedule)
60    {
61        $this->schedules[] = $schedule;
62
63        return $this;
64    }
65
66    /**
67     * Add schedules
68     *
69     * @param  array $schedules
70     * @return Scheduler
71     */
72    public function addSchedules(array $schedules)
73    {
74        foreach ($schedules as $schedule) {
75            $this->addSchedule($schedule);
76        }
77        return $this;
78    }
79
80    /**
81     * Get schedules
82     *
83     * @return array
84     */
85    public function getSchedules()
86    {
87        return $this->schedules;
88    }
89
90    /**
91     * Get schedule
92     *
93     * @param  int $index
94     * @return Schedule
95     */
96    public function getSchedule($index)
97    {
98        return (isset($this->schedules[$index])) ? $this->schedules[$index] : null;
99    }
100
101    /**
102     * Has schedules
103     *
104     * @return boolean
105     */
106    public function hasSchedules()
107    {
108        return (count($this->schedules) > 0);
109    }
110
111    /**
112     * Has schedule
113     *
114     * @param  int $index
115     * @return boolean
116     */
117    public function hasSchedule($index)
118    {
119        return (isset($this->schedules[$index]));
120    }
121
122    /**
123     * Process next job
124     *
125     * @param  Queue $queue
126     * @return void
127     */
128    public function processNext(Queue $queue = null)
129    {
130        foreach ($this->schedules as $key => $schedule) {
131            if ($schedule->isDue()) {
132                try {
133                    $application = ((null !== $queue) && (null !== $queue->hasApplication())) ? $queue->application() : null;
134                    $this->results[$key] = $schedule->getJob()->run($application);
135                    $schedule->getJob()->setAsCompleted();
136                    $this->completed[$key] = $schedule->getJob();
137
138                    if ((null !== $queue) && ($this->completed[$key]->hasJobId()) &&
139                        ($queue->adapter()->hasJob($this->completed[$key]->getJobId()))) {
140                        $queue->adapter()->updateJob($this->completed[$key]->getJobId(), false, true);
141                        $job = $queue->adapter()->getJob($this->completed[$key]->getJobId());
142                        if (($schedule->hasRunUntil()) && ($schedule->isExpired($job['attempts']))) {
143                            $queue->adapter()->updateJob($this->completed[$key]->getJobId(), true, false);
144                        }
145                    }
146                } catch (\Exception $e) {
147                    $schedule->getJob()->setAsFailed();
148                    $this->failed[$key]           = $schedule->getJob();
149                    $this->failedExceptions[$key] = $e;
150                    if ((null !== $queue) && ($this->failed[$key]->hasJobId()) &&
151                        ($queue->adapter()->hasJob($this->failed[$key]->getJobId()))) {
152                        $queue->adapter()->failed($queue->getName(), $this->failed[$key]->getJobId(), $e);
153                    }
154                }
155            }
156        }
157    }
158
159}