Skip to content

[Console][Messenger] Add $seconds to keepalive() methods #58552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public function setAlarmInterval(?int $seconds): void
$this->scheduleAlarm();
}

/**
* Gets the interval in seconds on which a SIGALRM signal is dispatched.
*/
public function getAlarmInterval(): ?int
{
return $this->alarmInterval;
}

private function scheduleAlarm(): void
{
if (null !== $this->alarmInterval) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,7 @@ public function testAlarmDispatchWithoutEventDispatcher()
$application = $this->createSignalableApplication($command, null);

$this->assertSame(1, $application->run(new ArrayInput(['alarm'])));
$this->assertSame(1, $application->getAlarmInterval());
$this->assertTrue($command->signaled);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function reject(Envelope $envelope): void
$this->connection->reject($this->findBeanstalkdReceivedStamp($envelope)->getId());
}

public function keepalive(Envelope $envelope): void
public function keepalive(Envelope $envelope, ?int $seconds = null): void
{
$this->connection->keepalive($this->findBeanstalkdReceivedStamp($envelope)->getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function reject(Envelope $envelope): void
$this->getReceiver()->reject($envelope);
}

public function keepalive(Envelope $envelope): void
public function keepalive(Envelope $envelope, ?int $seconds = null): void
{
$this->getReceiver()->keepalive($envelope);
$this->getReceiver()->keepalive($envelope, $seconds);
}

public function getMessageCount(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
if (\SIGALRM === $signal) {
$this->logger?->info('Sending keepalive request.', ['transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->keepalive();
$this->worker->keepalive($this->getApplication()->getAlarmInterval());

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
if (\SIGALRM === $signal) {
$this->logger?->info('Sending keepalive request.', ['transport_names' => $this->worker->getMetadata()->getTransportNames()]);

$this->worker->keepalive();
$this->worker->keepalive($this->getApplication()->getAlarmInterval());

return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ public function testKeepalive()

try {
$oldAsync = pcntl_async_signals(true);
pcntl_signal(\SIGALRM, fn () => $worker->keepalive());
pcntl_signal(\SIGALRM, fn () => $worker->keepalive(2));
pcntl_alarm(2);

$worker->run();
Expand All @@ -654,7 +654,7 @@ public function testKeepalive()
$this->assertSame($expectedEnvelopes, $receiver->keepaliveEnvelopes);

$receiver->keepaliveEnvelopes = [];
$worker->keepalive();
$worker->keepalive(2);

$this->assertCount(0, $receiver->keepaliveEnvelopes);
}
Expand All @@ -672,7 +672,7 @@ class DummyKeepaliveReceiver extends DummyReceiver implements KeepaliveReceiverI
{
public array $keepaliveEnvelopes = [];

public function keepalive(Envelope $envelope): void
public function keepalive(Envelope $envelope, ?int $seconds = null): void
{
$this->keepaliveEnvelopes[] = $envelope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ interface KeepaliveReceiverInterface extends ReceiverInterface
/**
* Informs the transport that the message is still being processed to avoid a timeout on the transport's side.
*
* @param int|null $seconds The minimum duration the message should be kept alive
*
* @throws TransportException If there is an issue communicating with the transport
*/
public function keepalive(Envelope $envelope): void;
public function keepalive(Envelope $envelope, ?int $seconds = null): void;
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function stop(): void
$this->shouldStop = true;
}

public function keepalive(): void
public function keepalive(?int $seconds): void
{
foreach ($this->keepalives as $message) {
[$transportName, $envelope] = $this->keepalives[$message];
Expand All @@ -303,7 +303,7 @@ public function keepalive(): void
'transport' => $transportName,
'message_id' => $envelope->last(TransportMessageIdStamp::class)?->getId(),
]);
$this->receivers[$transportName]->keepalive($envelope);
$this->receivers[$transportName]->keepalive($envelope, $seconds);
}
}

Expand Down
Loading