Completed
Push — master ( 959f68...c6517b )
by Alexey
29s queued 12s
created

RedisFailedJobProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 142
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A log() 0 10 1
A all() 0 13 2
A find() 0 12 2
A forget() 0 4 1
A flush() 0 4 1
A getClient() 0 4 1
A buildJob() 0 12 1
1
<?php
2
3
namespace SfCod\QueueBundle\Failer;
4
5
use Exception;
6
use Predis\Client;
7
use Predis\Collection\Iterator\HashKey;
8
use SfCod\QueueBundle\Base\RandomizeTrait;
9
use SfCod\QueueBundle\Entity\Job;
10
use SfCod\QueueBundle\Service\RedisDriver;
11
12
/**
13
 * Redis provider for failed jobs
14
 *
15
 * @author Virchenko Maksim <[email protected]>
16
 */
17
class RedisFailedJobProvider implements FailedJobProviderInterface
18
{
19
    use RandomizeTrait;
20
21
    /**
22
     * @var RedisDriver
23
     */
24
    private $redis;
25
26
    /**
27
     * The database collection.
28
     *
29
     * @var string
30
     */
31
    private $collection;
32
33
    /**
34
     * Create a new database failed job provider.
35
     *
36
     * @param RedisDriver $redis
37
     * @param string $collection
38
     */
39
    public function __construct(RedisDriver $redis, string $collection = 'queue_jobs_failed')
40
    {
41
        $this->redis = $redis;
42
        $this->collection = $collection;
43
    }
44
45
    /**
46
     * Log a failed job into storage.
47
     *
48
     * @param string $connection
49
     * @param string $queue
50
     * @param string $payload
51
     * @param Exception $exception
52
     *
53
     * @return int|void|null
54
     *
55
     * @throws Exception
56
     */
57
    public function log(string $connection, string $queue, string $payload, Exception $exception)
58
    {
59
        $this->getClient()->hset($this->collection, $this->getRandomId(), json_encode([
60
            'connection' => $connection,
61
            'queue' => $queue,
62
            'payload' => json_decode($payload, true),
63
            'exception' => $exception->getMessage(),
64
            'failed_at' => time(),
65
        ]));
66
    }
67
68
    /**
69
     * Get a list of all failed jobs.
70
     *
71
     * @return array
72
     */
73
    public function all(): array
74
    {
75
        $result = [];
76
        $cursor = new HashKey($this->getClient(), $this->collection);
77
78
        foreach ($cursor as $key => $value) {
79
            $data = json_decode($value, true);
80
81
            $result[] = $this->buildJob($key, $data['queue'], $data['payload']);
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * Get a single failed job.
89
     *
90
     * @param string $id
91
     *
92
     * @return Job|null
93
     */
94
    public function find(string $id): ?Job
95
    {
96
        $json = $this->getClient()->hget($this->collection, $id);
97
98
        if (!$json) {
99
            return null;
100
        }
101
102
        $data = json_decode($json, true);
103
104
        return $this->buildJob($id, $data['queue'], $data['payload']);
105
    }
106
107
    /**
108
     * Delete a single failed job from storage.
109
     *
110
     * @param string $id
111
     *
112
     * @return bool
113
     */
114
    public function forget(string $id): bool
115
    {
116
        return (bool)$this->getClient()->hdel($this->collection, [$id]);
117
    }
118
119
    /**
120
     * Flush all of the failed jobs from storage.
121
     */
122
    public function flush()
123
    {
124
        $this->getClient()->del([$this->collection]);
125
    }
126
127
    /**
128
     * Get redis client
129
     *
130
     * @return Client
131
     */
132
    private function getClient(): Client
133
    {
134
        return $this->redis->getClient();
135
    }
136
137
    /**
138
     * Build job from database data
139
     *
140
     * @param string $id
141
     * @param string $queue
142
     * @param array $payload
143
     *
144
     * @return Job
145
     */
146
    private function buildJob(string $id, string $queue, array $payload): Job
147
    {
148
        $job = new Job();
149
        $job->setId($id);
150
        $job->setQueue($queue);
151
        $job->setAttempts(0);
152
        $job->setReserved(false);
153
        $job->setReservedAt(null);
154
        $job->setPayload($payload);
155
156
        return $job;
157
    }
158
}
159