Skip to content

Commit 92533b6

Browse files
authored
Merge pull request #3 from pdffiller/feature/custom-handler
Custom handler
2 parents 4477ac4 + 977dc49 commit 92533b6

11 files changed

+222
-37
lines changed

README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Setup connection in `config/queue.php`
1818
// ...
1919
'qless' => [
2020
'driver' => 'qless',
21-
'connection' => 'qless',
21+
'redis_connection' => 'qless',
2222
'queue' => 'default',
2323
],
2424
// ...
@@ -85,6 +85,56 @@ Than you can put job to all subscribers.
8585

8686
```
8787

88+
### Custom Handler
89+
Job Handler helps you to create a custom route for jobs.
90+
91+
Custom Handler example:
92+
93+
```php
94+
95+
class CustomHandler implements JobHandler
96+
{
97+
public function perform(BaseJob $job): void
98+
{
99+
if ($job->getQueue() === 'queue_name') { // by queue
100+
(new QueueNameJob)->perform($job);
101+
return;
102+
}
103+
104+
if ($job->getData()['option_name'] === 'value') { // by payload
105+
(new OptionNameJob)->perform($job);
106+
return;
107+
}
108+
109+
if (in_array('tag_name', $job->getTags())) { // by tag
110+
(new TagNameJob)->perform($job);
111+
return;
112+
}
113+
114+
// other
115+
116+
$job->perform(); // Default
117+
}
118+
}
119+
120+
```
121+
122+
You must add to a service provider.
123+
124+
```php
125+
126+
public function boot()
127+
{
128+
// other code
129+
130+
$this->app->bind(JobHandler::class, CustomHandler::class);
131+
132+
// other code
133+
}
134+
135+
```
136+
137+
88138
### Recurring Jobs
89139
Sometimes it's not enough simply to schedule one job, but you want to run jobs regularly.
90140
In particular, maybe you have some batch operation that needs to get run once an hour and you don't care what
@@ -117,6 +167,6 @@ Laravel Qless Queue driver is open-sourced software licensed under the MIT Licen
117167
See the [`LICENSE.txt`](https://github.com/pdffiller/laravel-queue-qless/blob/master/LICENSE.txt) file for more.
118168

119169

120-
© 2018 PDFfiller<br>
170+
© 2018-2019 PDFfiller<br>
121171

122172
All rights reserved.

src/Contracts/JobHandler.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace LaravelQless\Contracts;
4+
5+
use Qless\Jobs\PerformAwareInterface;
6+
7+
interface JobHandler extends PerformAwareInterface
8+
{
9+
10+
}

src/Handler/DefaultHandler.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace LaravelQless\Handler;
4+
5+
use LaravelQless\Contracts\JobHandler;
6+
use Qless\Jobs\BaseJob;
7+
8+
class DefaultHandler implements JobHandler
9+
{
10+
public function perform(BaseJob $job): void
11+
{
12+
$job->perform();
13+
}
14+
}

src/Job/QlessJob.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Queue\Jobs\Job;
77
use Illuminate\Contracts\Queue\Job as JobContract;
88
use Qless\Jobs\BaseJob;
9+
use LaravelQless\Contracts\JobHandler;
910

1011
/**
1112
* Class QlessJob
@@ -38,11 +39,17 @@ class QlessJob extends Job implements JobContract
3839
*/
3940
protected $released = false;
4041

42+
/**
43+
* @var JobHandler
44+
*/
45+
protected $handler;
46+
4147
public function __construct(BaseJob $job, string $payload, string $connectionName)
4248
{
4349
$this->job = $job;
4450
$this->payload = $payload;
4551
$this->connectionName = $connectionName;
52+
$this->handler = app()->make(JobHandler::class);
4653
}
4754

4855
/**
@@ -80,7 +87,7 @@ public function payload()
8087
*/
8188
public function fire()
8289
{
83-
$this->job->perform();
90+
$this->handler->perform($this->job);
8491
}
8592

8693
/**
@@ -91,9 +98,9 @@ public function fire()
9198
*/
9299
public function release($delay = 0)
93100
{
94-
//$this->delete();
95101
$this->released = true;
96-
return \Queue::later($delay, $this->job->getKlass(), $this->job->getData(), $this->job->getQueue());
102+
return \Queue::connection($this->getConnectionName())
103+
->later($delay, $this->job->getKlass(), $this->job->getData(), $this->job->getQueue());
97104
}
98105

99106
/**

src/LaravelQlessServiceProvider.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
namespace LaravelQless;
44

55
use Illuminate\Queue\QueueManager;
6-
use Illuminate\Support\Facades\Config;
76
use Illuminate\Support\ServiceProvider;
7+
use LaravelQless\Contracts\JobHandler;
8+
use LaravelQless\Handler\DefaultHandler;
89
use LaravelQless\Queue\QlessConnector;
9-
use LaravelQless\Topics\QlessTopic;
10-
use Qless\Client;
11-
use Qless\Topics\Topic;
1210

1311
/**
1412
* Class LaravelQlessServiceProvider
@@ -29,5 +27,7 @@ public function boot()
2927
$queue->addConnector('qless', function () {
3028
return new QlessConnector;
3129
});
30+
31+
$this->app->bindif(JobHandler::class, DefaultHandler::class);
3232
}
3333
}

src/Queue/QlessQueue.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
class QlessQueue extends Queue implements QueueContract
1717
{
18+
private const WORKER_PREFIX = 'laravel_';
19+
1820
/**
1921
* @var Client
2022
*/
@@ -136,7 +138,7 @@ public function recur(int $interval, string $job, array $data, ?string $queueNam
136138
* Pop the next job off of the queue.
137139
*
138140
* @param string $queueName
139-
* @return \Illuminate\Contracts\Queue\Job|null
141+
* @return \Illuminate\Contracts\Queue\Job|null|QlessJob
140142
*/
141143
public function pop($queueName = null)
142144
{
@@ -145,7 +147,7 @@ public function pop($queueName = null)
145147
/** @var \Qless\Queues\Queue $queue */
146148
$queue = $this->getConnection()->queues[$queueName];
147149

148-
$job = $queue->pop();
150+
$job = $queue->pop(self::WORKER_PREFIX . $this->connect->getWorkerName());
149151

150152
if (!$job) {
151153
return null;

tests/Queue/CustomHandler.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace LaravelQless\Tests\Queue;
4+
5+
use LaravelQless\Contracts\JobHandler;
6+
use Qless\Jobs\BaseJob;
7+
8+
class CustomHandler implements JobHandler
9+
{
10+
public function perform(BaseJob $job): void
11+
{
12+
if ($job->getQueue() === 'handler_test') {
13+
(new Job)->perform($job);
14+
}
15+
}
16+
}

tests/Queue/HandlerTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace LaravelQless\Tests\Queue;
4+
5+
use LaravelQless\Contracts\JobHandler;
6+
use LaravelQless\Queue\QlessQueue;
7+
use Orchestra\Testbench\TestCase;
8+
use Qless\Client;
9+
10+
class HandlerTest extends TestCase
11+
{
12+
public function testCustomHandler()
13+
{
14+
$queue = $this->getQueue();
15+
16+
$queue->push(\stdClass::class, ['firstKey' => 'firstValue'], 'handler_test');
17+
18+
$job = $queue->pop('handler_test');
19+
20+
$job->fire();
21+
22+
$this->assertEquals($job->getData()['classHandler'], Job::class);
23+
}
24+
25+
protected function getQueue()
26+
{
27+
return new QlessQueue(
28+
new Client([
29+
'host' => REDIS_HOST,
30+
'port' => REDIS_PORT,
31+
]),
32+
[
33+
'queue' => 'test_qless_queue'
34+
]
35+
);
36+
}
37+
38+
protected function getApplicationProviders($app)
39+
{
40+
$app->bind(JobHandler::class, CustomHandler::class);
41+
42+
return $app['config']['app.providers'];
43+
}
44+
}

tests/Queue/Job.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class Job implements QlessJob
99
{
1010
public function perform(BaseJob $job)
1111
{
12+
if ($job->getQueue() === 'handler_test') {
13+
$job->getData()['classHandler'] = self::class;
14+
}
15+
1216
$job->complete();
1317
}
14-
}
18+
}

tests/Queue/QlessConnectorTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaravelQless\Tests\Queue;
44

5+
use LaravelQless\Contracts\JobHandler;
56
use Orchestra\Testbench\TestCase;
67
use Illuminate\Queue\Connectors\ConnectorInterface;
78
use LaravelQless\Queue\QlessConnector;
@@ -36,4 +37,11 @@ protected function getEnvironmentSetUp($app)
3637
'port' => REDIS_PORT,
3738
]);
3839
}
40+
41+
protected function getApplicationProviders($app)
42+
{
43+
$app->bindIf(JobHandler::class, CustomHandler::class);
44+
45+
return $app['config']['app.providers'];
46+
}
3947
}

0 commit comments

Comments
 (0)