1 | <?php |
||
16 | class Gateway |
||
17 | { |
||
18 | |||
19 | use TimeTrait; |
||
20 | use RedisTrait; |
||
21 | use IdGeneratorTrait; |
||
22 | |||
23 | const QUEUE_DELAYED = 'message_queue:delayed'; |
||
24 | const QUEUE_IMMEDIATE = 'message_queue:immediate'; |
||
25 | const META_DATA = 'message_queue:meta_data'; |
||
26 | const RETRY_TIME = 3600; // try again after 1 hour |
||
27 | |||
28 | /** |
||
29 | * @param string $eventId |
||
30 | * @param string $eventType |
||
31 | * @return bool success |
||
32 | */ |
||
33 | 3 | public function deleteEvent(string $eventId, string $eventType = null) : bool |
|
34 | { |
||
35 | 3 | $eventId = sprintf('%s:%s', $eventType, $eventId); |
|
36 | |||
37 | 3 | $redis = $this->getRedis(); |
|
38 | 3 | $delayed = $redis->zrem(self::QUEUE_DELAYED, $eventId); |
|
39 | 3 | if ($delayed) { |
|
40 | 1 | $redis->hdel(self::META_DATA, [$eventId]); |
|
41 | 1 | return true; |
|
42 | } |
||
43 | |||
44 | 2 | $immediate = $this->getRedis()->lrange(self::QUEUE_IMMEDIATE, 0, 100); |
|
45 | 2 | foreach ($immediate as $rawJob) { |
|
46 | 1 | list($jobId) = explode('#', $rawJob, 2); |
|
47 | 1 | if (strpos($jobId, "$eventId") === 0) { |
|
|
|||
48 | 1 | return (bool)$this->redis->lrem(self::QUEUE_IMMEDIATE, 1, $rawJob); |
|
49 | } |
||
50 | } |
||
51 | |||
52 | 1 | return false; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param AbstractEvent $event |
||
57 | * @param int $timestamp |
||
58 | * @return string |
||
59 | */ |
||
60 | 2 | public function addEvent(AbstractEvent $event, int $timestamp = 0) |
|
61 | { |
||
62 | 2 | $jobId = $this->generateUniqueId('jobid:' . $event->getEventName()); |
|
63 | 2 | $jobId = sprintf('%s:%s', $event->getEventName(), $jobId); |
|
64 | |||
65 | 2 | $job = new Job($event, $jobId, $timestamp); |
|
66 | |||
67 | 2 | $this->addJob($job); |
|
68 | 2 | } |
|
69 | |||
70 | /** |
||
71 | * @param Job $job |
||
72 | */ |
||
73 | 3 | public function addJob(Job $job) |
|
74 | { |
||
75 | 3 | $serialized = base64_encode(serialize($job)); |
|
76 | |||
77 | 3 | $pipeline = $this->getRedis()->pipeline(['fire-and-forget' => true]); |
|
78 | 3 | if (empty($job->timestamp)) { |
|
79 | // immediate execution in background |
||
80 | 1 | $pipeline->lpush(self::QUEUE_IMMEDIATE, $job->jobId . '#' . $serialized); |
|
81 | } else { |
||
82 | // delayed execution |
||
83 | 2 | $pipeline->hset(self::META_DATA, $job->jobId, $serialized); |
|
84 | 2 | $pipeline->zadd(self::QUEUE_DELAYED, [ |
|
85 | 2 | $job->jobId => (int)$job->timestamp |
|
86 | ]); |
||
87 | } |
||
88 | |||
89 | 3 | $pipeline->execute(); |
|
90 | 3 | } |
|
91 | |||
92 | /** |
||
93 | * @param string $eventType |
||
94 | * @param int $since |
||
95 | * @return Job[] |
||
96 | */ |
||
97 | 3 | public function getEventsByType(string $eventType = null, int $since = 0) : array |
|
101 | |||
102 | /** |
||
103 | * @param string $eventType |
||
104 | * @param int $since |
||
105 | * @return Generator|Job[] |
||
106 | */ |
||
107 | 3 | public function getEventsByTypeGenerator(string $eventType = null, int $since = 0) : Generator |
|
128 | |||
129 | /** |
||
130 | * @param Job $job |
||
131 | */ |
||
132 | 1 | public function restoreJob(Job $job) |
|
140 | |||
141 | /** |
||
142 | * @return int |
||
143 | */ |
||
144 | 1 | public function countAllJobs() : int |
|
151 | |||
152 | /** |
||
153 | * @param array $keys |
||
154 | * @return Generator |
||
155 | */ |
||
156 | 3 | private function getFromTimesQueue(array $keys) : Generator |
|
167 | |||
168 | /** |
||
169 | * @param string $eventType |
||
170 | * @return Generator |
||
171 | */ |
||
172 | 3 | private function getFromImmediateQueue(string $eventType = null) : Generator |
|
184 | } |
||
185 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.