-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.php
More file actions
83 lines (64 loc) · 1.74 KB
/
Queue.php
File metadata and controls
83 lines (64 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace LogadApp\Queue;
use Exception;
use LogadApp\Queue\Contracts\StoreInterface;
use LogadApp\Queue\Stores\FileStore;
use RuntimeException;
/**
* @todo Remove "delete", should update the job status instead
* @todo Retry shouldn't delete the job but reset it and push it again
* @todo Update the job fail reason along with failure
*/
class Queue
{
private static ?StoreInterface $store = null;
public static function useStore(StoreInterface $store): void
{
static::$store = $store;
}
private static function getStore(): StoreInterface
{
if (!isset(static::$store)) {
static::$store = new FileStore();
}
return static::$store;
}
public static function add(string $queue, Job $job): string
{
$id = static::getStore()->add($queue, serialize($job));
$job->setId($id);
return $id;
}
public static function next(string $queue): ?Job
{
$job = static::getStore()->next($queue);
if (!$job) return null;
try {
$jobObject = unserialize($job['payload']);
if (!$jobObject instanceof Job) {
throw new RuntimeException('Invalid job payload');
}
$jobObject->setId($job['id']);
return $jobObject;
} catch (Exception $e) {
error_log("Failed to unserialize job: " . $e->getMessage());
static::getStore()->delete($queue, $job['id']);
}
return null;
}
public static function retry(string $queue, Job $job): void
{
$newId = static::getStore()->retry($queue, serialize($job));
$job->setId($newId);
}
public static function delete(string $queue, Job $job): void
{
static::getStore()->delete($queue, $job->getId());
}
// list jos for debug
public static function list(string $queue = 'default'): array
{
return static::getStore()->listJobs($queue);
}
}