Skip to content

Commit 5d4f444

Browse files
committed
Add override queue when connections is sqs
Fix code with php-cs-fixer
1 parent c3c5392 commit 5d4f444

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

ProcessMaker/Bus/SqsDispatcher.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ProcessMaker\Bus;
4+
5+
use Illuminate\Bus\Dispatcher as BaseDispatcher;
6+
7+
class SqsDispatcher extends BaseDispatcher
8+
{
9+
public function dispatch($command)
10+
{
11+
if (config('queue.default') === 'sqs' && property_exists($command, 'queue')) {
12+
// Override to the SQS default queue
13+
$command->queue = null;
14+
15+
\Log::info('Forcing queue to default for job: ' . get_class($command));
16+
}
17+
18+
return parent::dispatch($command);
19+
}
20+
}

ProcessMaker/Providers/ProcessMakerServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ public function register(): void
251251
WorkerCommandString::$command = 'exec @php artisan horizon:listen';
252252
SystemProcessCounter::$command = 'horizon:listen';
253253
}
254+
255+
$this->app->extend(\Illuminate\Bus\Dispatcher::class, function ($dispatcher, $app) {
256+
return new \ProcessMaker\Bus\SqsDispatcher($app, function ($connection) use ($app) {
257+
return $app['queue']->connection($connection);
258+
});
259+
});
254260
}
255261

256262
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Illuminate\Foundation\Bus\Dispatchable;
7+
use Illuminate\Foundation\Queue\Queueable;
8+
use Illuminate\Support\Facades\Bus;
9+
use Illuminate\Support\Facades\Config;
10+
use Illuminate\Support\Facades\Queue;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use Tests\TestCase;
13+
14+
class SqsDispatchOverrideTest extends TestCase
15+
{
16+
#[Test]
17+
public function it_forces_default_queue_when_using_sqs()
18+
{
19+
// Simulate SQS connection
20+
Config::set('queue.default', 'sqs');
21+
22+
Queue::fake();
23+
24+
// Dispatch a job with a custom queue name
25+
FakeJob::dispatch()->onQueue('bpmn');
26+
27+
// Assert the job was dispatched to the forced queue (default)
28+
Queue::assertPushed(FakeJob::class, function ($job) {
29+
return $job->queue === null;
30+
});
31+
}
32+
33+
#[Test]
34+
public function it_respects_original_queue_when_not_using_sqs()
35+
{
36+
// Simulate Redis connection
37+
Config::set('queue.default', 'redis');
38+
39+
Bus::fake();
40+
41+
// Dispatch a job with a custom queue name
42+
FakeJob::dispatch()->onQueue('bpmn');
43+
44+
Bus::assertDispatched(FakeJob::class, function ($job) {
45+
return $job->queue === 'bpmn';
46+
});
47+
}
48+
}
49+
50+
class FakeJob implements ShouldQueue
51+
{
52+
use Dispatchable, Queueable;
53+
54+
public function handle(): void
55+
{
56+
\Log::info('TestJob executed successfully.');
57+
}
58+
}

0 commit comments

Comments
 (0)