Skip to content

Conversation

dwalck
Copy link

@dwalck dwalck commented May 22, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
License MIT

Hi!

My proposal is to add a --duration option to the messenger:stop-workers command, which would allow to "pause" the workers for the amount of time specified in this option.

For example, if we pass --duration=10, then for the next 10 seconds, running messenger:consume will return a message indicating how many seconds are left until the "pause" ends.

I think this option would be helpful during deployments (we're using it this way in my company).

image

image

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title Messenger: add --duration option to messenger:stop-workers command [Messenger] Messenger: add --duration option to messenger:stop-workers command May 23, 2025
@OskarStark
Copy link
Contributor

Please add a line in the changelog. Please use 7.4 there, as 7.3 is already in feature freeze 🥶

It looks that your committer email is not associated with your GitHub account, you might want to check this.

@dwalck dwalck force-pushed the feature/messenger-stop-workers-duration branch 2 times, most recently from 3c603a7 to 779fcb2 Compare May 23, 2025 06:19
7.4
---

* Add `--duration` option to `messenger:stop-workers` command to keep workers in paused state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add `--duration` option to `messenger:stop-workers` command to keep workers in paused state.
* Add `--duration` option to `messenger:stop-workers` command to keep workers in paused state

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

@OskarStark OskarStark changed the title [Messenger] Messenger: add --duration option to messenger:stop-workers command [Messenger] Messenger: add --duration option to messenger:stop-workers command May 23, 2025
@dwalck dwalck force-pushed the feature/messenger-stop-workers-duration branch 2 times, most recently from b604d41 to 60b0c31 Compare May 25, 2025 17:18
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@@ -44,6 +48,11 @@ protected function configure(): void
Each worker command will finish the message they are currently processing
and then exit. Worker commands are *not* automatically restarted: that
should be handled by a process control system.

Use the --duration option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use the --duration option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).
Use the <comment>--duration</comment> option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).

@@ -35,7 +37,9 @@ public function __construct(
protected function configure(): void
{
$this
->setDefinition([])
->setDefinition([
new InputOption('duration', 'd', InputOption::VALUE_REQUIRED, 'Duration in seconds to keep the workers stopped'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new InputOption('duration', 'd', InputOption::VALUE_REQUIRED, 'Duration in seconds to keep the workers stopped'),
new InputOption('duration', 'd', InputOption::VALUE_REQUIRED, 'Duration in seconds during which workers are paused (not processing messages)'),

$this->restartSignalCachePool->save($cacheItem);

$io->success('Signal successfully sent to stop any running workers.');
if ($duration > 0) {
$io->info(sprintf('Workers will be stopped for next %s seconds.', $duration));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$io->info(sprintf('Workers will be stopped for next %s seconds.', $duration));
$io->info(sprintf('Workers will be paused for %s seconds.', $duration));

if ($this->shouldRestart()) {
$event->getWorker()->stop();
$remainingStopSeconds = ceil($this->getEndOfStopTime()) - time();
$this->logger?->info(sprintf('Worker is stopped and message processing is paused for the next %d seconds.', $remainingStopSeconds));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->logger?->info(sprintf('Worker is stopped and message processing is paused for the next %d seconds.', $remainingStopSeconds));
$this->logger?->info(sprintf('The worker is paused and message processing will resume in %d seconds.', $remainingStopSeconds));

@dwalck
Copy link
Author

dwalck commented Jun 1, 2025

@fabpot done

update: Rebased due to some failing tests ([Windows / x86 / minimal-exts)) 😸

@dwalck dwalck force-pushed the feature/messenger-stop-workers-duration branch from 9e94973 to a75bc99 Compare June 1, 2025 11:36
@@ -44,6 +48,11 @@ protected function configure(): void
Each worker command will finish the message they are currently processing
and then exit. Worker commands are *not* automatically restarted: that
should be handled by a process control system.

Use the <comment>--duration</comment> option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use the <comment>--duration</comment> option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).
Use the <info>--duration</info> option to keep the workers in a paused state (not processing messages) for the given duration (in seconds).

Comment on lines 65 to 69
if (null !== $duration = $input->getOption('duration')) {
if (!is_numeric($duration) || 0 >= $duration) {
throw new InvalidOptionException(\sprintf('Option "duration" must be a positive integer, "%s" passed.', $duration));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (null !== $duration = $input->getOption('duration')) {
if (!is_numeric($duration) || 0 >= $duration) {
throw new InvalidOptionException(\sprintf('Option "duration" must be a positive integer, "%s" passed.', $duration));
}
}
$duration = (int) $input->getOption('duration')

$this->restartSignalCachePool->save($cacheItem);

$io->success('Signal successfully sent to stop any running workers.');
if ($duration > 0) {
$io->info(sprintf('Workers will be paused for %s seconds.', $duration));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$io->info(sprintf('Workers will be paused for %s seconds.', $duration));
$io->info(\sprintf('Workers will be paused for %s seconds.', $duration));

if ($this->shouldRestart()) {
$event->getWorker()->stop();
$remainingStopSeconds = ceil($this->getEndOfStopTime()) - time();
$this->logger?->info(sprintf('The worker is paused and message processing will resume in %d seconds.', $remainingStopSeconds));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->logger?->info(sprintf('The worker is paused and message processing will resume in %d seconds.', $remainingStopSeconds));
$this->logger?->info(\sprintf('The worker is paused and message processing will resume in %d seconds.', $remainingStopSeconds));

@dwalck dwalck force-pushed the feature/messenger-stop-workers-duration branch from a75bc99 to 9c32d93 Compare August 18, 2025 22:45
@dwalck
Copy link
Author

dwalck commented Aug 18, 2025

Done.

I think Fabbot is mistaken in this case 😄

image

@OskarStark
Copy link
Contributor

Maybe I miss something here, but calling a stop command with a duration, which does not stop anything sounds weird. Why not a dedicated "pause" command?

@dwalck
Copy link
Author

dwalck commented Aug 19, 2025

@OskarStark using this option does not change the default behavior of the command. I’m open to suggestions regarding creating a new command, but from my perspective this functionality extends the existing behavior of stopping workers, so I added it as an option to the existing command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants