Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 844554b

Browse files
committed
laravel-queue-replaceable
1 parent 88269e1 commit 844554b

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/.idea
3+
/composer.lock

‎README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Laravel Queue Driver For Delayed jobs Can be replaced
2+
3+
4+
## Installing
5+
6+
```sh
7+
$ composer require "al-one/laravel-queue-replaceable" -vvv
8+
```
9+
10+
11+
## Usage
12+
13+
```php
14+
# config/app.php
15+
<?php
16+
17+
return [
18+
'providers' => [
19+
Alone\LaravelQueueReplaceable\ServiceProvider::class,
20+
],
21+
];
22+
```
23+
24+
```php
25+
# config/queue.php
26+
<?php
27+
28+
return [
29+
'default' => env('QUEUE_DRIVER','replaceable_database'),
30+
'connections' => [
31+
'replaceable_database' => [
32+
'driver' => 'replaceable_database',
33+
'connection' => 'default', // database connection
34+
'queue' => 'default',
35+
'retry_after' => 90,
36+
],
37+
'replaceable_redis' => [
38+
'driver' => 'replaceable_redis',
39+
'connection' => 'default', // redis connection
40+
'queue' => 'default',
41+
'retry_after' => 90,
42+
],
43+
],
44+
];
45+
```
46+
47+
```php
48+
<?php
49+
50+
ProcessPodcast::dispatch($podcast)
51+
->onConnection('replaceable_database')
52+
->delay(now()->addMinutes(10));
53+
```
54+
55+
56+
## License
57+
58+
MIT

‎composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "al-one/laravel-queue-replaceable",
3+
"description": "Laravel Queue Driver For Delayed jobs Can be replaced",
4+
"keywords": ["laravel", "illuminate", "queue"],
5+
"authors": [
6+
{
7+
"name": "Alone",
8+
"email": "hi@anlo.ng"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.5.9",
13+
"illuminate/queue": "~5.1"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Alone\\LaravelQueueReplaceable\\": "src/"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"Alone\\LaravelQueueReplaceable\\ServiceProvider"
24+
]
25+
}
26+
},
27+
"license": "MIT"
28+
}

‎src/Connectors/DatabaseConnector.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Alone\LaravelQueueReplaceable\Connectors;
4+
5+
use Alone\LaravelQueueReplaceable\DatabaseQueue;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Queue\Connectors\DatabaseConnector as BaseDatabaseConnector;
8+
9+
class DatabaseConnector extends BaseDatabaseConnector
10+
{
11+
12+
/**
13+
* Establish a queue connection.
14+
*
15+
* @param array $config
16+
* @return \Illuminate\Contracts\Queue\Queue
17+
*/
18+
public function connect(array $config)
19+
{
20+
return new DatabaseQueue(
21+
$this->connections->connection(Arr::get($config, 'connection')),
22+
$config['table'],
23+
$config['queue'],
24+
Arr::get($config, 'retry_after', 60)
25+
);
26+
}
27+
28+
}

‎src/Connectors/RedisConnector.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Alone\LaravelQueueReplaceable\Connectors;
4+
5+
use Alone\LaravelQueueReplaceable\RedisQueue;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Queue\Connectors\RedisConnector as BaseRedisConnector;
8+
9+
class RedisConnector extends BaseRedisConnector
10+
{
11+
12+
/**
13+
* Establish a queue connection.
14+
*
15+
* @param array $config
16+
* @return \Illuminate\Contracts\Queue\Queue
17+
*/
18+
public function connect(array $config)
19+
{
20+
return new RedisQueue(
21+
$this->redis, $config['queue'],
22+
Arr::get($config, 'connection', $this->connection),
23+
Arr::get($config, 'retry_after', 60)
24+
);
25+
}
26+
27+
}

‎src/DatabaseQueue.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Alone\LaravelQueueReplaceable;
4+
5+
use Illuminate\Queue\DatabaseQueue as BaseDatabaseQueue;
6+
7+
class DatabaseQueue extends BaseDatabaseQueue
8+
{
9+
10+
/**
11+
* Push an array of jobs onto the queue.
12+
*
13+
* @param array $jobs
14+
* @param mixed $data
15+
* @param string $queue
16+
* @return mixed
17+
*/
18+
public function bulk($jobs, $data = '', $queue = null)
19+
{
20+
$payloads = collect((array)$jobs)->map(function($job) use($data)
21+
{
22+
return $this->createPayload($job,$data);
23+
})->all();
24+
$this->database->table($this->table)
25+
->where('queue',$this->getQueue($queue))
26+
->whereIn('payload',$payloads)
27+
->delete();
28+
return parent::bulk($jobs,$data,$queue);
29+
}
30+
31+
/**
32+
* Push a raw payload to the database with a given delay.
33+
*
34+
* @param string|null $queue
35+
* @param string $payload
36+
* @param \DateTime|int $delay
37+
* @param int $attempts
38+
* @return mixed
39+
*/
40+
protected function pushToDatabase($queue, $payload, $delay = 0, $attempts = 0)
41+
{
42+
$this->database->table($this->table)
43+
->where('queue',$this->getQueue($queue))
44+
->where('payload',$payload)
45+
->delete();
46+
return parent::pushToDatabase($queue,$payload,$delay,$attempts);
47+
}
48+
49+
}

‎src/RedisQueue.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Alone\LaravelQueueReplaceable;
4+
5+
use Illuminate\Queue\RedisQueue as BaseRedisQueue;
6+
7+
class RedisQueue extends BaseRedisQueue
8+
{
9+
10+
/**
11+
* Create a payload string from the given job and data.
12+
*
13+
* @param string $job
14+
* @param mixed $data
15+
* @param string $queue
16+
* @return array
17+
*/
18+
protected function createPayloadArray($job, $data = '', $queue = null)
19+
{
20+
$payload = parent::createPayloadArray($job,$data,$queue);
21+
$replaceableId = null;
22+
if(is_object($job) && method_exists($job,'getReplaceableId'))
23+
{
24+
$replaceableId = $job->getReplaceableId();
25+
}
26+
elseif(is_array($data) || is_object($data))
27+
{
28+
$replaceableId = (string)data_get($data,'replaceable');
29+
}
30+
if($replaceableId)
31+
{
32+
data_set($payload,'id',$replaceableId);
33+
}
34+
return $payload;
35+
}
36+
37+
}

‎src/ServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Alone\LaravelQueueReplaceable;
4+
5+
use Illuminate\Support\Facades\Queue;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
class ServiceProvider extends BaseServiceProvider
9+
{
10+
11+
public function register()
12+
{
13+
Queue::extend('replaceable_database',function()
14+
{
15+
return new Connectors\DatabaseConnector($this->app['db']);
16+
});
17+
Queue::extend('replaceable_redis',function()
18+
{
19+
return new Connectors\RedisConnector($this->app['redis']);
20+
});
21+
}
22+
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /