Completed
Push — master ( d19f31...6d358f )
by Matthew
02:58
created

Resque.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Mpclarkson\ResqueBundle;
4
5
use Psr\Log\NullLogger;
6
7
class Resque implements EnqueueInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $kernelOptions;
13
14
    /**
15
     * @var array
16
     */
17
    private $redisConfiguration;
18
19
    /**
20
     * @var array
21
     */
22
    private $globalRetryStrategy = array();
23
24
    /**
25
     * @var array
26
     */
27
    private $jobRetryStrategy = array();
28
29
    public function __construct(array $kernelOptions)
30
    {
31
        $this->kernelOptions = $kernelOptions;
32
    }
33
34
    public function setPrefix($prefix)
35
    {
36
        \Resque_Redis::prefix($prefix);
37
    }
38
39
    public function setRedisConfiguration($host, $port, $database)
40
    {
41
        $this->redisConfiguration = array(
42
            'host'     => $host,
43
            'port'     => $port,
44
            'database' => $database,
45
        );
46
        $host = substr($host, 0, 1) == '/' ? $host : $host.':'.$port;
0 ignored issues
show
Consider using a different name than the parameter $host. This often makes code more readable.
Loading history...
47
48
        \Resque::setBackend($host, $database);
49
    }
50
51
    public function setGlobalRetryStrategy($strategy)
52
    {
53
        $this->globalRetryStrategy = $strategy;
54
    }
55
56
    public function setJobRetryStrategy($strategy)
57
    {
58
        $this->jobRetryStrategy = $strategy;
59
    }
60
61
    public function getRedisConfiguration()
62
    {
63
        return $this->redisConfiguration;
64
    }
65
66
    public function enqueue(Job $job, $trackStatus = false)
67
    {
68
        if ($job instanceof ContainerAwareJob) {
69
            $job->setKernelOptions($this->kernelOptions);
70
        }
71
72
        $this->attachRetryStrategy($job);
73
74
        $result = \Resque::enqueue($job->queue, \get_class($job), $job->args, $trackStatus);
75
76
        if ($trackStatus) {
77
            return new \Resque_Job_Status($result);
0 ignored issues
show
It seems like $result defined by \Resque::enqueue($job->q...ob->args, $trackStatus) on line 74 can also be of type false; however, Resque_Job_Status::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
78
        }
79
80
        return null;
81
    }
82
83
    public function enqueueOnce(Job $job, $trackStatus = false)
84
    {
85
        $queue = new Queue($job->queue);
86
        $jobs  = $queue->getJobs();
87
88
        foreach ($jobs AS $j) {
89
            if ($j->job->payload['class'] == get_class($job)) {
90
                if (count(array_intersect($j->args, $job->args)) == count($job->args)) {
91
                    return ($trackStatus) ? $j->job->payload['id'] : null;
92
                }
93
            }
94
        }
95
96
        return $this->enqueue($job, $trackStatus);
97
    }
98
99 View Code Duplication
    public function enqueueAt($at, Job $job)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        if ($job instanceof ContainerAwareJob) {
102
            $job->setKernelOptions($this->kernelOptions);
103
        }
104
105
        $this->attachRetryStrategy($job);
106
107
        \ResqueScheduler::enqueueAt($at, $job->queue, \get_class($job), $job->args);
108
109
        return null;
110
    }
111
112 View Code Duplication
    public function enqueueIn($in, Job $job)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        if ($job instanceof ContainerAwareJob) {
115
            $job->setKernelOptions($this->kernelOptions);
116
        }
117
118
        $this->attachRetryStrategy($job);
119
120
        \ResqueScheduler::enqueueIn($in, $job->queue, \get_class($job), $job->args);
121
122
        return null;
123
    }
124
125 View Code Duplication
    public function removedDelayed(Job $job)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        if ($job instanceof ContainerAwareJob) {
128
            $job->setKernelOptions($this->kernelOptions);
129
        }
130
        
131
        $this->attachRetryStrategy($job);
132
133
        return \ResqueScheduler::removeDelayed($job->queue, \get_class($job), $job->args);
134
    }
135
136 View Code Duplication
    public function removeFromTimestamp($at, Job $job)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        if ($job instanceof ContainerAwareJob) {
139
            $job->setKernelOptions($this->kernelOptions);
140
        }
141
        
142
        $this->attachRetryStrategy($job);
143
144
        return \ResqueScheduler::removeDelayedJobFromTimestamp($at, $job->queue, \get_class($job), $job->args);
145
    }
146
147
    public function getQueues()
148
    {
149
        return \array_map(function($queue) {
150
            return new Queue($queue);
151
        }, \Resque::queues());
152
    }
153
154
    /**
155
     * @param $queue
156
     * @return Queue
157
     */
158
    public function getQueue($queue)
159
    {
160
        return new Queue($queue);
161
    }
162
163
    public function getWorkers()
164
    {
165
        return \array_map(function($worker) {
166
            return new Worker($worker);
167
        }, \Resque_Worker::all());
168
    }
169
170
    public function getWorker($id)
171
    {
172
        $worker = \Resque_Worker::find($id);
173
174
        if (!$worker) {
175
            return null;
176
        }
177
178
        return new Worker($worker);
179
    }
180
181
    public function pruneDeadWorkers()
182
    {
183
        // HACK, prune dead workers, just in case
184
        $worker = new \Resque_Worker('temp');
185
        $worker->setLogger(new NullLogger());
186
        $worker->pruneDeadWorkers();
187
    }
188
189
    public function getDelayedJobTimestamps()
190
    {
191
        $timestamps = \Resque::redis()->zrange('delayed_queue_schedule', 0, -1);
192
193
        //TODO: find a more efficient way to do this
194
        $out = array();
195
        foreach ($timestamps as $timestamp) {
196
            $out[] = array($timestamp, \Resque::redis()->llen('delayed:'.$timestamp));
197
        }
198
199
        return $out;
200
    }
201
202
    public function getFirstDelayedJobTimestamp()
203
    {
204
        $timestamps = $this->getDelayedJobTimestamps();
205
        if (count($timestamps) > 0) {
206
            return $timestamps[0];
207
        }
208
209
        return array(null, 0);
210
    }
211
212
    public function getNumberOfDelayedJobs()
213
    {
214
        return \ResqueScheduler::getDelayedQueueScheduleSize();
215
    }
216
217 View Code Duplication
    public function getJobsForTimestamp($timestamp)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        $jobs = \Resque::redis()->lrange('delayed:'.$timestamp, 0, -1);
220
        $out = array();
221
        foreach ($jobs as $job) {
222
            $out[] = json_decode($job, true);
223
        }
224
225
        return $out;
226
    }
227
228
    /**
229
     * @param $queue
230
     * @return int
231
     */
232
    public function clearQueue($queue)
233
    {
234
        $length = \Resque::redis()->llen('queue:'.$queue);
235
        \Resque::redis()->del('queue:'.$queue);
236
237
        return $length;
238
    }
239
240 View Code Duplication
    public function getFailedJobs($start = -100, $count = 100)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        $jobs = \Resque::redis()->lrange('failed', $start, $count);
243
244
        $result = array();
245
246
        foreach ($jobs as $job) {
247
            $result[] = new FailedJob(json_decode($job, true));
248
        }
249
250
        return $result;
251
    }
252
253
    /**
254
     * Attach any applicable retry strategy to the job.
255
     *
256
     * @param Job $job
257
     */
258
    protected function attachRetryStrategy($job)
259
    {
260
        $class = get_class($job);
261
262
        if (isset($this->jobRetryStrategy[$class])) {
263
            if (count($this->jobRetryStrategy[$class])) {
264
                $job->args['resque.retry_strategy'] = $this->jobRetryStrategy[$class];
265
            }
266
            $job->args['resque.retry_strategy'] = $this->jobRetryStrategy[$class];
267
        } elseif (count($this->globalRetryStrategy)) {
268
            $job->args['resque.retry_strategy'] = $this->globalRetryStrategy;
269
        }
270
    }
271
}
272