Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractTaskAdapter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 schedule
n/a
0 / 0
n/a
0 / 0
0
 getTasks
n/a
0 / 0
n/a
0 / 0
0
 getTask
n/a
0 / 0
n/a
0 / 0
0
 getAllTasks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 updateTask
n/a
0 / 0
n/a
0 / 0
0
 removeTask
n/a
0 / 0
n/a
0 / 0
0
 getTaskCount
n/a
0 / 0
n/a
0 / 0
0
 hasTasks
n/a
0 / 0
n/a
0 / 0
0
 clearTasks
n/a
0 / 0
n/a
0 / 0
0
 claimTaskRun
n/a
0 / 0
n/a
0 / 0
0
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Queue\Adapter;
16
17use Pop\Queue\Process\Task;
18
19/**
20 * Adapter abstract class
21 *
22 * @category   Pop
23 * @package    Pop\Queue
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    3.0.0
28 */
29abstract class AbstractTaskAdapter extends AbstractAdapter implements TaskAdapterInterface
30{
31
32    /**
33     * How long, in seconds, a claim blocks a *same-window* re-claim.
34     * Shared by every concrete adapter's claimTaskRun() implementation.
35     * Not configurable - it has no relationship to any task's cron
36     * recurrence interval (the explicit window value each implementation
37     * compares against is what makes that safe). It does need to outlast
38     * the longest window a claim must survive: a claim is never refreshed
39     * or released while its task runs, and a coarse (non-sub-minute) task
40     * is due across its entire ~60-second window (evaluate() stays true
41     * for the whole minute, not just at :00), so a second worker can
42     * legitimately re-evaluate the same coarse task's window many seconds
43     * after the first worker claimed it. 90 seconds covers a full
44     * 60-second coarse window plus slack, not just one claim-then-execute
45     * round trip.
46     */
47    protected const TASK_CLAIM_TTL = 90;
48
49    /**
50     * Schedule job with queue
51     *
52     * @param  Task $task
53     * @return AbstractTaskAdapter
54     */
55    abstract public function schedule(Task $task): AbstractTaskAdapter;
56
57    /**
58     * Get scheduled tasks
59     *
60     * @return array
61     */
62    abstract public function getTasks(): array;
63
64    /**
65     * Get scheduled task
66     *
67     * @param  string $taskId
68     * @return ?Task
69     */
70    abstract public function getTask(string $taskId): ?Task;
71
72    /**
73     * Get every scheduled task, keyed by task ID.
74     *
75     * Concrete, not abstract, so an adapter only overrides it if its storage can
76     * genuinely do better than one fetch per task - which the Database and Redis
77     * adapters both can, and do. This fallback is the loop it replaces, kept so
78     * that adding the method to TaskAdapterInterface doesn't oblige every
79     * adapter to reimplement it.
80     *
81     * @return array  taskId => Task
82     */
83    public function getAllTasks(): array
84    {
85        $tasks = [];
86
87        foreach ($this->getTasks() as $taskId) {
88            $task = $this->getTask($taskId);
89            if ($task !== null) {
90                $tasks[$taskId] = $task;
91            }
92        }
93
94        return $tasks;
95    }
96
97    /**
98     * Update scheduled task
99     *
100     * @param  Task $task
101     * @return AbstractTaskAdapter
102     */
103    abstract public function updateTask(Task $task): AbstractTaskAdapter;
104
105    /**
106     * Remove scheduled task
107     *
108     * @param  string $taskId
109     * @return AbstractTaskAdapter
110     */
111    abstract public function removeTask(string $taskId): AbstractTaskAdapter;
112
113    /**
114     * Get scheduled tasks count
115     *
116     * @return int
117     */
118    abstract public function getTaskCount(): int;
119
120    /**
121     * Has scheduled tasks
122     *
123     * @return bool
124     */
125    abstract public function hasTasks(): bool;
126
127    /**
128     * Clear all scheduled task
129     *
130     * @return AbstractTaskAdapter
131     */
132    abstract public function clearTasks(): AbstractTaskAdapter;
133
134    /**
135     * Atomically claim a task's current due-window
136     *
137     * @param  string $taskId
138     * @param  string $window
139     * @return bool
140     */
141    abstract public function claimTaskRun(string $taskId, string $window): bool;
142
143}