Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 701c5b1

Browse files
committed
Experimental Redis queue driver.
1 parent 09ec31f commit 701c5b1

5 files changed

Lines changed: 427 additions & 2 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace Illuminate\Queue\Connectors;
2+
3+
use Illuminate\Redis\Database;
4+
use Illuminate\Queue\RedisQueue;
5+
6+
class RedisConnector implements ConnectorInterface {
7+
8+
/**
9+
* The Redis database instance.
10+
*
11+
* @var \Illuminate\Redis\Database
12+
*/
13+
protected $redis;
14+
15+
/**
16+
* The connection name.
17+
*
18+
* @var string
19+
*/
20+
protected $connection;
21+
22+
/**
23+
* Create a new Redis queue connector instance.
24+
*
25+
* @param \Illuminate\Redis\Database $redis
26+
* @param string|null $connection
27+
* @return void
28+
*/
29+
public function __construct(Database $redis, $connection = null)
30+
{
31+
$this->redis = $redis;
32+
$this->connection = $connection;
33+
}
34+
35+
/**
36+
* Establish a queue connection.
37+
*
38+
* @param array $config
39+
* @return \Illuminate\Queue\QueueInterface
40+
*/
41+
public function connect(array $config)
42+
{
43+
return new RedisQueue($this->redis, $config['queue'], $this->connection);
44+
}
45+
46+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php namespace Illuminate\Queue\Jobs;
2+
3+
use Illuminate\Queue\RedisQueue;
4+
use Illuminate\Container\Container;
5+
6+
class RedisJob extends Job {
7+
8+
/**
9+
* The Redis queue instance.
10+
*
11+
* @var \Illuminate\Queues\RedisQueue
12+
*/
13+
protected $redis;
14+
15+
/**
16+
* The Redis job payload.
17+
*
18+
* @var string
19+
*/
20+
protected $job;
21+
22+
/**
23+
* The queue the job belongs to.
24+
*
25+
* @var string
26+
*/
27+
protected $queue;
28+
29+
/**
30+
* Create a new job instance.
31+
*
32+
* @param \Illuminate\Container $container
33+
* @param \Illuminate\Redis\Queue $queue
34+
* @param string $job
35+
* @param string $queue
36+
* @return void
37+
*/
38+
public function __construct(Container $container, RedisQueue $redis, $job, $queue)
39+
{
40+
$this->job = $job;
41+
$this->redis = $redis;
42+
$this->queue = $queue;
43+
$this->container = $container;
44+
}
45+
46+
/**
47+
* Fire the job.
48+
*
49+
* @return void
50+
*/
51+
public function fire()
52+
{
53+
$this->resolveAndFire(json_decode($this->job, true));
54+
}
55+
56+
/**
57+
* Delete the job from the queue.
58+
*
59+
* @return void
60+
*/
61+
public function delete()
62+
{
63+
$this->redis->deleteReserved($this->queue, $this->job);
64+
}
65+
66+
/**
67+
* Release the job back into the queue.
68+
*
69+
* @param int $delay
70+
* @return void
71+
*/
72+
public function release($delay = 0)
73+
{
74+
$this->delete();
75+
76+
$this->redis->release($this->queue, $this->job, $delay, $this->attempts() + 1);
77+
}
78+
79+
/**
80+
* Get the number of times the job has been attempted.
81+
*
82+
* @return int
83+
*/
84+
public function attempts()
85+
{
86+
return array_get(json_decode($this->job, true), 'attempts');
87+
}
88+
89+
/**
90+
* Get the job identifier.
91+
*
92+
* @return string
93+
*/
94+
public function getJobId()
95+
{
96+
return array_get(json_decode($this->job, true), 'id');
97+
}
98+
99+
/**
100+
* Get the IoC container instance.
101+
*
102+
* @return \Illuminate\Container
103+
*/
104+
public function getContainer()
105+
{
106+
return $this->container;
107+
}
108+
109+
/**
110+
* Get the underlying queue driver instance.
111+
*
112+
* @return \Illuminate\Redis\Database
113+
*/
114+
public function getRedisQueue()
115+
{
116+
return $this->redis;
117+
}
118+
119+
/**
120+
* Get the underlying Redis job.
121+
*
122+
* @return string
123+
*/
124+
public function getRedisJob()
125+
{
126+
return $this->job;
127+
}
128+
129+
}

src/Illuminate/Queue/QueueServiceProvider.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Queue\Connectors\SqsConnector;
88
use Illuminate\Queue\Connectors\SyncConnector;
99
use Illuminate\Queue\Connectors\IronConnector;
10+
use Illuminate\Queue\Connectors\RedisConnector;
1011
use Illuminate\Queue\Connectors\BeanstalkdConnector;
1112

1213
class QueueServiceProvider extends ServiceProvider {
@@ -145,7 +146,7 @@ protected function registerSubscriber()
145146
*/
146147
public function registerConnectors($manager)
147148
{
148-
foreach (array('Sync', 'Beanstalkd', 'Sqs', 'Iron') as $connector)
149+
foreach (array('Sync', 'Beanstalkd', 'Redis', 'Sqs', 'Iron') as $connector)
149150
{
150151
$this->{"register{$connector}Connector"}($manager);
151152
}
@@ -179,6 +180,22 @@ protected function registerBeanstalkdConnector($manager)
179180
});
180181
}
181182

183+
/**
184+
* Register the Redis queue connector.
185+
*
186+
* @param \Illuminate\Queue\QueueManager $manager
187+
* @return void
188+
*/
189+
protected function registerRedisConnector($manager)
190+
{
191+
$app = $this->app;
192+
193+
$manager->addConnector('redis', function() use ($app)
194+
{
195+
return new RedisConnector($app['redis']);
196+
});
197+
}
198+
182199
/**
183200
* Register the Amazon SQS queue connector.
184201
*
@@ -219,4 +236,4 @@ public function provides()
219236
return array('queue', 'queue.worker', 'queue.listener', 'command.queue.work', 'command.queue.listen', 'command.queue.subscribe');
220237
}
221238

222-
}
239+
}

0 commit comments

Comments
 (0)