Skip to content

Commit c2c3ece

Browse files
authored
Merge pull request #12 from freyo/plain
Add Plain Support
2 parents ad9f01a + 1ff0a11 commit c2c3ece

File tree

11 files changed

+340
-29
lines changed

11 files changed

+340
-29
lines changed

.scrutinizer.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tools:
2+
# external_code_coverage: true
3+
php_mess_detector: true
4+
php_code_sniffer: true
5+
sensiolabs_security_checker: true
6+
# php_code_coverage: true
7+
php_pdepend: true
8+
php_loc:
9+
enabled: true
10+
excluded_dirs: [vendor, tests]
11+
checks:
12+
php:
13+
code_rating: true
14+
duplication: true
15+
filter:
16+
excluded_paths:
17+
- 'tests/*'
18+
build:
19+
tests:
20+
override:
21+
-
22+
command: vendor/bin/phpunit --coverage-clover=my-coverage-file
23+
coverage:
24+
file: my-coverage-file
25+
format: php-clover

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- 7.2
8+
9+
sudo: false
10+
11+
install: travis_retry composer install --no-interaction --prefer-source
12+
13+
script: vendor/bin/phpunit --verbose

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
5757
CMQ_QUEUE_HOST=https://cmq-queue-region.api.qcloud.com
5858
CMQ_QUEUE=queue_name #default queue name
59-
CMQ_QUEUE_POLLING_WAIT_SECONDS=30
59+
CMQ_QUEUE_POLLING_WAIT_SECONDS=0
6060
6161
CMQ_TOPIC_ENABLE=false # set to true to use topic
6262
CMQ_TOPIC_FILTER=routing # or msgtag
@@ -118,6 +118,10 @@ Configure `config/queue.php`
118118
'name' => '',
119119
],
120120
],
121+
'plain' => [
122+
'enable' => false,
123+
'job' => 'App\Jobs\CMQPlainJob@handle',
124+
],
121125
];
122126
//...
123127
];
@@ -129,6 +133,65 @@ Configure `config/queue.php`
129133
php artisan queue:work {connection-name} --queue={queue-name}
130134
```
131135

136+
#### Plain Mode (Experimental)
137+
138+
```
139+
CMQ_PLAIN_ENABLE=true
140+
CMQ_PLAIN_JOB=App\Jobs\CMQPlainJob@handle
141+
```
142+
143+
```php
144+
<?php
145+
146+
namespace App\Jobs;
147+
148+
use Illuminate\Bus\Queueable;
149+
use Illuminate\Queue\InteractsWithQueue;
150+
use Illuminate\Contracts\Queue\ShouldQueue;
151+
use Illuminate\Queue\Jobs\Job;
152+
153+
class CMQPlainJob implements ShouldQueue
154+
{
155+
use InteractsWithQueue, Queueable;
156+
157+
protected $payload;
158+
159+
/**
160+
* Create a new job instance.
161+
*
162+
* @return void
163+
*/
164+
public function __construct($payload)
165+
{
166+
$this->payload = $payload;
167+
}
168+
169+
/**
170+
* Get the plain payload of the job.
171+
*
172+
* @return string
173+
*/
174+
public function getPayload()
175+
{
176+
return $this->payload;
177+
}
178+
179+
/**
180+
* Execute the job.
181+
*
182+
* @return void
183+
*/
184+
public function handle(Job $job, $payload)
185+
{
186+
if (! $job->isDeletedOrReleased()) {
187+
$job->delete();
188+
}
189+
190+
var_dump($payload);
191+
}
192+
}
193+
```
194+
132195
## References
133196

134197
- [Product Documentation](https://cloud.tencent.com/document/product/406?lang=en)

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"description": "Queue Adapter for Tencent Qcloud CMQ SDK",
44
"keywords": ["laravel-queue", "cmq", "qcloud"],
55
"require": {
6-
"php": "^7.1.3",
7-
"illuminate/queue": "5.6.*|5.7.*"
6+
"php": ">=5.6.4",
7+
"illuminate/queue": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*"
88
},
99
"require-dev": {
10-
"php": "^7.1.3",
11-
"phpunit/phpunit": "^7.0"
10+
"php": ">=5.6.4",
11+
"phpunit/phpunit": "~4.8"
1212
},
1313
"autoload": {
1414
"psr-4": {

config/cmq.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@
2828
],
2929
],
3030

31+
'plain' => [
32+
'enable' => env('CMQ_PLAIN_ENABLE', false),
33+
'job' => env('CMQ_PLAIN_JOB', 'App\Jobs\CMQPlainJob@handle'),
34+
],
35+
3136
];

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/LaravelQueueCMQServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function boot()
3131
$queue = $this->app['queue'];
3232

3333
$queue->addConnector('cmq', function () {
34-
return new CMQConnector($this->app['events']);
34+
return new CMQConnector();
3535
});
3636
}
3737
}

src/Queue/CMQQueue.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Freyo\LaravelQueueCMQ\Queue\Jobs\CMQJob;
1010
use Illuminate\Contracts\Queue\Queue as QueueContract;
1111
use Illuminate\Queue\Queue;
12+
use Illuminate\Support\Arr;
1213

1314
class CMQQueue extends Queue implements QueueContract
1415
{
@@ -29,13 +30,36 @@ class CMQQueue extends Queue implements QueueContract
2930
private $queueAccount;
3031
private $topicAccount;
3132

33+
/**
34+
* @var array
35+
*/
36+
protected $plainOptions;
37+
3238
public function __construct(Account $queueAccount, Account $topicAccount, array $config)
3339
{
3440
$this->queueAccount = $queueAccount;
3541
$this->topicAccount = $topicAccount;
3642

3743
$this->queueOptions = $config['options']['queue'];
3844
$this->topicOptions = $config['options']['topic'];
45+
46+
$this->plainOptions = Arr::get($config, 'plain', []);
47+
}
48+
49+
/**
50+
* @return bool
51+
*/
52+
public function isPlain()
53+
{
54+
return Arr::get($this->plainOptions, 'enable', false);
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getPlainJob()
61+
{
62+
return Arr::get($this->plainOptions, 'job');
3963
}
4064

4165
/**
@@ -63,7 +87,9 @@ public function size($queue = null)
6387
*/
6488
public function push($job, $data = '', $queue = null)
6589
{
66-
return $this->pushRaw($this->createPayload($job, $data), $queue);
90+
$payload = $this->isPlain() ? $job->getPayload() : $this->createPayload($job, $data);
91+
92+
return $this->pushRaw($payload, $queue);
6793
}
6894

6995
/**

src/Queue/Connectors/CMQConnector.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,11 @@
44

55
use Freyo\LaravelQueueCMQ\Queue\CMQQueue;
66
use Freyo\LaravelQueueCMQ\Queue\Driver\Account;
7-
use Illuminate\Contracts\Events\Dispatcher;
8-
use Illuminate\Contracts\Queue\Queue;
97
use Illuminate\Queue\Connectors\ConnectorInterface;
10-
use Illuminate\Queue\Events\WorkerStopping;
8+
use Illuminate\Support\Arr;
119

1210
class CMQConnector implements ConnectorInterface
1311
{
14-
/**
15-
* @var Dispatcher
16-
*/
17-
private $dispatcher;
18-
19-
public function __construct(Dispatcher $dispatcher)
20-
{
21-
$this->dispatcher = $dispatcher;
22-
}
23-
2412
/**
2513
* Establish a queue connection.
2614
*
@@ -30,13 +18,17 @@ public function __construct(Dispatcher $dispatcher)
3018
*/
3119
public function connect(array $config)
3220
{
33-
$queue = new Account(array_get($config, 'options.queue.host'), $config['secret_id'], $config['secret_key']);
34-
35-
$topic = new Account(array_get($config, 'options.topic.host'), $config['secret_id'], $config['secret_key']);
36-
37-
$this->dispatcher->listen(WorkerStopping::class, function () {
38-
//
39-
});
21+
$queue = new Account(
22+
Arr::get($config, 'options.queue.host'),
23+
$config['secret_id'],
24+
$config['secret_key']
25+
);
26+
27+
$topic = new Account(
28+
Arr::get($config, 'options.topic.host'),
29+
$config['secret_id'],
30+
$config['secret_key']
31+
);
4032

4133
return new CMQQueue($queue, $topic, $config);
4234
}

src/Queue/Jobs/CMQJob.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,33 @@ public function attempts()
5959
*/
6060
public function fire()
6161
{
62-
method_exists($this, 'resolveAndFire')
63-
? $this->resolveAndFire(json_decode($this->getRawBody(), true))
62+
method_exists($this, 'resolveAndFire')
63+
? $this->resolveAndFire($this->payload())
6464
: parent::fire();
6565
}
6666

67+
/**
68+
* Get the decoded body of the job.
69+
*
70+
* @return array
71+
*/
72+
public function payload()
73+
{
74+
if ($this->connection->isPlain()) {
75+
$job = $this->connection->getPlainJob();
76+
77+
return [
78+
'displayName' => is_string($job) ? explode('@', $job)[0] : null,
79+
'job' => $job,
80+
'maxTries' => null,
81+
'timeout' => null,
82+
'data' => $this->getRawBody(),
83+
];
84+
}
85+
86+
return json_decode($this->getRawBody(), true);
87+
}
88+
6789
/**
6890
* Delete the job from the queue.
6991
*

0 commit comments

Comments
 (0)