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

Skip to content

[Messenger] Allow to add a custom callback / Worker to be able to do an action after each "worker run" #33843

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

Closed
jvdlaar opened this issue Oct 4, 2019 · 1 comment

Comments

@jvdlaar
Copy link

jvdlaar commented Oct 4, 2019

We run our messenger consumer in a kubernetes cluster. For the pod I want to setup a liveness probe. So kubernetes will do some sort of check inside the pod to see if it is still running.

An easy way to do this is to write a file somewhere and then kubernetes checks if this file exists. Removes it, sleeps a bit and then checks again if the file is created.

For this I need to do be able to have a place that can write a file, even when no messages arrive. The Worker class is a suitable place because inside the run function the $onHandledCallback is constantly being called with a short sleep.

The problem is that you cannot add your own Worker or $onHandledCallback currently in the ConsumeMessagesCommand.

I would like to propose that you can add a custom $onHandledCallback or a custom set of Workers to the constructor of the ConsumeMessagesCommand.

@jvdlaar
Copy link
Author

jvdlaar commented Oct 4, 2019

As a workaround I've currently implemented this by making my own transport and then I've added this behavior to the get function.

@fabpot fabpot closed this as completed Nov 4, 2019
nicolas-grekas added a commit that referenced this issue Nov 5, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Messenger] use events consistently in worker

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #32560, #32614, #33843
| License       | MIT
| Doc PR        |

The worker had the three ways to handle events
1. $onHandledCallback in `run(array $options = [], callable $onHandledCallback = null)`
2. events dispatched using the event dispatcher
3. hardcoded things inside the worker

This PR refactores the messenger worker to only use event dispatching. So instead of a hardcoded `$onHandledCallback` and worker decorators, we use event listeners and we don't need a `WorkerInterface` at all. The behavior of all the options like `--memory-limit` etc remains the same.

I introduced two new events
- `WorkerStartedEvent`
- `WorkerRunningEvent`

Together with the existing `WorkerStoppedEvent` it's very symmetrical and solves the referenced issues.

Commits
-------

201f159 [Messenger] use events consistently in worker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants