Insolita /
circular-queue
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by solly [30.10.17 23:36] |
||||
| 4 | */ |
||||
| 5 | |||||
| 6 | namespace insolita\cqueue\Mixins; |
||||
| 7 | |||||
| 8 | use Carbon\Carbon; |
||||
| 9 | use function in_array; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * @mixin \insolita\cqueue\CircularQueue |
||||
| 13 | **/ |
||||
| 14 | trait DelayingTrait |
||||
| 15 | { |
||||
| 16 | |||||
| 17 | public function resume($payload, int $delay = 0, bool $force = false) |
||||
| 18 | { |
||||
| 19 | $identity = $this->converter->toIdentity($payload); |
||||
| 20 | if ($delay === 0) { |
||||
| 21 | if ($this->storage->zSetExists($this->delayedKey(), $identity)) { |
||||
| 22 | $this->storage->moveFromZSetToList($this->queueKey(), $this->delayedKey(), $identity); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 23 | }elseif ($force === true || !in_array($identity, $this->storage->listItems($this->queueKey()))){ |
||||
| 24 | $this->storage->listPush($this->queueKey(), [$identity]); |
||||
| 25 | } |
||||
| 26 | } else { |
||||
| 27 | $timestamp = Carbon::now()->timestamp + $delay; |
||||
| 28 | $this->storage->zSetPush($this->delayedKey(), $timestamp, $identity); |
||||
| 29 | } |
||||
| 30 | } |
||||
| 31 | public function resumeAt($payload, int $timestamp) |
||||
| 32 | { |
||||
| 33 | $identity = $this->converter->toIdentity($payload); |
||||
| 34 | $this->storage->zSetPush($this->delayedKey(), $timestamp, $identity); |
||||
| 35 | } |
||||
| 36 | public function countDelayed(): int |
||||
| 37 | { |
||||
| 38 | return $this->storage->zSetCount($this->delayedKey()); |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | public function listDelayed($converted = false): array |
||||
| 42 | { |
||||
| 43 | $list = $this->storage->zSetItems($this->delayedKey()); |
||||
| 44 | if ($converted === false || empty($list)) { |
||||
| 45 | return $list; |
||||
| 46 | } else { |
||||
| 47 | return array_map([$this->converter, 'toPayload'], $list); |
||||
| 48 | } |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | public function resumeAllDelayed() |
||||
| 52 | { |
||||
| 53 | $inUse = $this->listDelayed(); |
||||
| 54 | if (!empty($inUse)) { |
||||
| 55 | foreach ($inUse as $identity) { |
||||
| 56 | $this->storage->moveFromZSetToList($this->queueKey(), $this->delayedKey(), $identity); |
||||
| 57 | } |
||||
| 58 | } |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | public function purgeDelayed() |
||||
| 62 | { |
||||
| 63 | $this->storage->delete($this->delayedKey()); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | protected function listExpired($expireTime): array |
||||
| 67 | { |
||||
| 68 | return $this->storage->zSetExpiredItems($this->delayedKey(), $expireTime); |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | protected function resumeExpired() |
||||
| 72 | { |
||||
| 73 | $inUseExpired = $this->listExpired(Carbon::now()->timestamp); |
||||
| 74 | if (!empty($inUseExpired)) { |
||||
| 75 | foreach ($inUseExpired as $identity) { |
||||
| 76 | $this->storage->moveFromZSetToList($this->queueKey(), $this->delayedKey(), $identity); |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | protected function delayedKey(): string |
||||
| 82 | { |
||||
| 83 | return $this->getName() . ':Wait'; |
||||
|
0 ignored issues
–
show
It seems like
getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 84 | } |
||||
| 85 | } |
||||
| 86 |