Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d27f62e

Browse files
committed
[Messenger] Add bury_on_reject option to Beanstalkd bridge
1 parent e2d5c4a commit d27f62e

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

src/Symfony/Component/Messenger/Bridge/Beanstalkd/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `bury_on_reject` option
8+
49
5.2.0
510
-----
611

src/Symfony/Component/Messenger/Bridge/Beanstalkd/Tests/Transport/ConnectionTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testFromDsn()
4747
$this->assertSame('default', $configuration['tube_name']);
4848
$this->assertSame(0, $configuration['timeout']);
4949
$this->assertSame(90, $configuration['ttr']);
50+
$this->assertFalse($configuration['bury_on_reject']);
5051

5152
$this->assertEquals(
5253
$connection = new Connection([], Pheanstalk::create('foobar', 15555)),
@@ -58,22 +59,32 @@ public function testFromDsn()
5859
$this->assertSame('default', $configuration['tube_name']);
5960
$this->assertSame(0, $configuration['timeout']);
6061
$this->assertSame(90, $configuration['ttr']);
62+
$this->assertFalse($configuration['bury_on_reject']);
6163
$this->assertSame('default', $connection->getTube());
6264
}
6365

6466
public function testFromDsnWithOptions()
6567
{
6668
$this->assertEquals(
67-
$connection = Connection::fromDsn('beanstalkd://localhost', ['tube_name' => 'foo', 'timeout' => 10, 'ttr' => 5000]),
68-
Connection::fromDsn('beanstalkd://localhost?tube_name=foo&timeout=10&ttr=5000')
69+
$connectionWithOptions = Connection::fromDsn('beanstalkd://localhost', ['tube_name' => 'foo', 'timeout' => 10, 'ttr' => 5000, 'bury_on_reject' => true]),
70+
$connectionWithQuery = Connection::fromDsn('beanstalkd://localhost?tube_name=foo&timeout=10&ttr=5000&bury_on_reject=true')
6971
);
7072

71-
$configuration = $connection->getConfiguration();
73+
$configuration = $connectionWithOptions->getConfiguration();
7274

7375
$this->assertSame('foo', $configuration['tube_name']);
7476
$this->assertSame(10, $configuration['timeout']);
7577
$this->assertSame(5000, $configuration['ttr']);
76-
$this->assertSame('foo', $connection->getTube());
78+
$this->assertTrue($configuration['bury_on_reject']);
79+
$this->assertSame('foo', $connectionWithOptions->getTube());
80+
81+
$configuration = $connectionWithQuery->getConfiguration();
82+
83+
$this->assertSame('foo', $configuration['tube_name']);
84+
$this->assertSame(10, $configuration['timeout']);
85+
$this->assertSame(5000, $configuration['ttr']);
86+
$this->assertTrue($configuration['bury_on_reject']);
87+
$this->assertSame('foo', $connectionWithOptions->getTube());
7788
}
7889

7990
public function testFromDsnOptionsArrayWinsOverOptionsFromDsn()
@@ -82,18 +93,20 @@ public function testFromDsnOptionsArrayWinsOverOptionsFromDsn()
8293
'tube_name' => 'bar',
8394
'timeout' => 20,
8495
'ttr' => 6000,
96+
'bury_on_reject' => false,
8597
];
8698

8799
$this->assertEquals(
88100
$connection = new Connection($options, Pheanstalk::create('localhost', 11333)),
89-
Connection::fromDsn('beanstalkd://localhost:11333?tube_name=foo&timeout=10&ttr=5000', $options)
101+
Connection::fromDsn('beanstalkd://localhost:11333?tube_name=foo&timeout=10&ttr=5000&bury_on_reject=true', $options)
90102
);
91103

92104
$configuration = $connection->getConfiguration();
93105

94106
$this->assertSame($options['tube_name'], $configuration['tube_name']);
95107
$this->assertSame($options['timeout'], $configuration['timeout']);
96108
$this->assertSame($options['ttr'], $configuration['ttr']);
109+
$this->assertSame($options['bury_on_reject'], $configuration['bury_on_reject']);
97110
$this->assertSame($options['tube_name'], $connection->getTube());
98111
}
99112

@@ -214,6 +227,21 @@ public function testReject()
214227
$connection->reject((string) $id);
215228
}
216229

230+
public function testRejectWithBury()
231+
{
232+
$id = 123456;
233+
234+
$tube = 'baz';
235+
236+
$client = $this->createMock(PheanstalkInterface::class);
237+
$client->expects($this->once())->method('useTube')->with($tube)->willReturn($client);
238+
$client->expects($this->once())->method('bury')->with($this->callback(fn (JobId $jobId): bool => $jobId->getId() === $id));
239+
240+
$connection = new Connection(['tube_name' => $tube, 'bury_on_reject' => true], $client);
241+
242+
$connection->reject((string) $id);
243+
}
244+
217245
public function testRejectWhenABeanstalkdExceptionOccurs()
218246
{
219247
$id = 123456;

src/Symfony/Component/Messenger/Bridge/Beanstalkd/Transport/Connection.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Connection
3232
'tube_name' => PheanstalkInterface::DEFAULT_TUBE,
3333
'timeout' => 0,
3434
'ttr' => 90,
35+
'bury_on_reject' => false,
3536
];
3637

3738
/**
@@ -40,12 +41,14 @@ class Connection
4041
* * tube_name: name of the tube
4142
* * timeout: message reservation timeout (in seconds)
4243
* * ttr: the message time to run before it is put back in the ready queue (in seconds)
44+
* * bury_on_reject: bury rejected messages instead of deleting them
4345
*/
4446
private array $configuration;
4547
private PheanstalkInterface $client;
4648
private string $tube;
4749
private int $timeout;
4850
private int $ttr;
51+
private bool $buryOnReject;
4952

5053
public function __construct(array $configuration, PheanstalkInterface $client)
5154
{
@@ -54,6 +57,7 @@ public function __construct(array $configuration, PheanstalkInterface $client)
5457
$this->tube = $this->configuration['tube_name'];
5558
$this->timeout = $this->configuration['timeout'];
5659
$this->ttr = $this->configuration['ttr'];
60+
$this->buryOnReject = $this->configuration['bury_on_reject'];
5761
}
5862

5963
public static function fromDsn(#[\SensitiveParameter] string $dsn, array $options = []): self
@@ -73,7 +77,15 @@ public static function fromDsn(#[\SensitiveParameter] string $dsn, array $option
7377
}
7478

7579
$configuration = [];
76-
$configuration += $options + $query + self::DEFAULT_OPTIONS;
80+
foreach (self::DEFAULT_OPTIONS as $k => $v) {
81+
$value = $options[$k] ?? $query[$k] ?? $v;
82+
83+
$configuration[$k] = match (\gettype($v)) {
84+
'integer' => filter_var($value, \FILTER_VALIDATE_INT),
85+
'boolean' => filter_var($value, \FILTER_VALIDATE_BOOL),
86+
default => $value,
87+
};
88+
}
7789

7890
// check for extra keys in options
7991
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
@@ -173,7 +185,11 @@ public function ack(string $id): void
173185
public function reject(string $id): void
174186
{
175187
try {
176-
$this->client->useTube($this->tube)->delete(new JobId((int) $id));
188+
if ($this->buryOnReject) {
189+
$this->client->useTube($this->tube)->bury(new JobId((int) $id));
190+
} else {
191+
$this->client->useTube($this->tube)->delete(new JobId((int) $id));
192+
}
177193
} catch (Exception $exception) {
178194
throw new TransportException($exception->getMessage(), 0, $exception);
179195
}

0 commit comments

Comments
 (0)