Skip to content

Commit fd33c90

Browse files
authored
Merge pull request #7 from pdffiller/no-queue-realization
Add sync mode
2 parents b1d71e2 + b84289d commit fd33c90

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"require": {
1818
"ext-json": "*",
19-
"pdffiller/qless-php": "3.4.0"
19+
"pdffiller/qless-php": "3.6.1"
2020
},
2121
"require-dev": {
2222
"illuminate/events": "5.6.*|5.7.*",

src/Job/AbstractJob.php

+47
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Queue\ShouldQueue;
77
use Illuminate\Contracts\Support\Arrayable;
88
use Illuminate\Foundation\Bus\Dispatchable;
9+
use LaravelQless\Queue\QlessQueue;
910
use Qless\Jobs\BaseJob;
1011
use LaravelQless\Contracts\QlessJob;
1112

@@ -21,6 +22,8 @@ abstract class AbstractJob implements QlessJob, ShouldQueue, Arrayable
2122
*/
2223
protected $data = [];
2324

25+
protected $isSync = false;
26+
2427
public function __construct(array $data = [])
2528
{
2629
$this->data = $data;
@@ -32,11 +35,55 @@ public function __construct(array $data = [])
3235
*/
3336
abstract public function perform(BaseJob $job);
3437

38+
/**
39+
* {@inheritdoc}
40+
* @return AbstractJob
41+
*/
42+
public static function dispatchNow()
43+
{
44+
return (new static(...func_get_args()))->completeSync();
45+
}
46+
3547
/**
3648
* @return array
3749
*/
3850
public function toArray(): array
3951
{
4052
return (array) $this->data;
4153
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
protected function completeSync(): self
59+
{
60+
$this->isSync = true;
61+
return $this;
62+
}
63+
64+
private function completeImmediately(): void
65+
{
66+
/**@var QlessQueue $queue */
67+
$queue = app(QlessQueue::class, [
68+
'config' => [
69+
'queue' => $this->queue,
70+
'connection' => $this->connection
71+
]
72+
]);
73+
74+
$jid = $queue->push($this, $this->data, $this->queue);
75+
$connection = $queue->getConnection()->queues[$this->queue];
76+
77+
/**@var BaseJob $job */
78+
$job = $connection->popByJid($jid);
79+
80+
$job->perform();
81+
}
82+
83+
public function __destruct()
84+
{
85+
if ($this->isSync) {
86+
$this->completeImmediately();
87+
}
88+
}
4289
}

tests/Queue/Job.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public function perform(BaseJob $job)
1717
$job->getData()['tags'] = $job->getTags();
1818
}
1919

20+
$_SERVER['payload'] = $job->getData();
21+
2022
$job->complete();
2123
}
2224
}

tests/Queue/QueueTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ public function testDispatchJob()
199199
$this->assertEquals('work', $data['dispatch']);
200200
}
201201

202+
public function testDispatchNowJob()
203+
{
204+
$this->setEnv();
205+
206+
$queueName = str_random();
207+
208+
$dispatch = Job::dispatchNow(['dispatchNow' => 'work_as_sync'])
209+
->onQueue($queueName)
210+
->onConnection('qless');
211+
212+
unset($dispatch);
213+
214+
$queue = $this->getQueue();
215+
216+
$job = $queue->pop($queueName);
217+
218+
$this->assertEquals($_SERVER['payload']['dispatchNow'], 'work_as_sync');
219+
220+
$this->assertNull($job);
221+
}
222+
202223
protected function getQueue()
203224
{
204225
$queue = new QlessQueue(

0 commit comments

Comments
 (0)