Skip to content

Commit dbcc7da

Browse files
committed
Add override queue when connections is sqs
1 parent 330919e commit dbcc7da

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

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
@@ -241,6 +241,12 @@ public function register(): void
241241
});
242242

243243
$this->app->instance('tenant-resolved', false);
244+
245+
$this->app->extend(\Illuminate\Bus\Dispatcher::class, function ($dispatcher, $app) {
246+
return new \ProcessMaker\Bus\SqsDispatcher($app, function ($connection) use ($app) {
247+
return $app['queue']->connection($connection);
248+
});
249+
});
244250
}
245251

246252
/**
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\Queue;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use Tests\TestCase;
11+
use Illuminate\Support\Facades\Config;
12+
use Illuminate\Support\Facades\Bus;
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)