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

Skip to content

[Scheduler] add dependency on symfony/messenger #50096

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
wants to merge 2 commits into from

Conversation

kbond
Copy link
Member

@kbond kbond commented Apr 21, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #50085, Fix #49864, Helps with #49838, #49865
License MIT
Doc PR n/a

I think we should create a hard dependency on symfony/messenger. This makes adding messenger-related features much easier. When using standalone, I've made the fact that messenger is used under the hood completely transparent.

  • 2d05655: switches the Scheduler to use messenger's Worker under the hood
  • 8a9e1af: Adds useful context to the ScheduleStamp (schedule name, trigger, current run time, next run time). This could be moved to a followup PR but wanted to demonstrate how much easier it is to add this context with the hard dep.

/cc @upyx, any objections here?

public function __construct(
public readonly ?string $name,
public readonly TriggerInterface $trigger,
public readonly \DateTimeImmutable $runAt,
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we need a better name? This might not be the true run date (if the scheduler is catching up). Maybe $expectedRunAt?

/cc @tucksaun

Copy link
Contributor

Choose a reason for hiding this comment

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

expectedRunAt sounds indeed better than runAt.

Other suggestions:

  • scheduledAt
  • triggeredAt (to keep the idea of the trigger)

@upyx
Copy link
Contributor

upyx commented Apr 21, 2023

@kbond On the one hand, it simplifies adding features. On the other hand, I'm afraid of lacking incapsulation and spreading internal details, which complicate future extension.

@kbond
Copy link
Member Author

kbond commented Apr 21, 2023

Ok, I understand your concern, what about the following ideas:

  1. Add events: before the MessageGenerator yeilds a message, it dispatches an event that has the context and listeners can manipulate the message (turn it into an envelope with stamps). I'm thinking this doesn't fully address your concern though.
  2. Extract most of the logic from MessageGenerator into an abstract BaseMessageGenerator (or another object entirely). We then can create a MessengerMessageGenerator that takes this data (object, time, next time, trigger) and wraps in an envelope. In this case we choose to make this internal data available in a stamp. This would allow removing the hard-dependency on messenger.

I'm very open to other ideas!

@tucksaun
Copy link
Contributor

I might have something by mixing your approach and the one in #50085: we could imagine an intermediary class, not bound to Messenger, yield from the generator.
Then we attach it to the stamp in the SchedulerTransport. Direct users of the MessageGenerator can decide whether to use or not the provided context, or extend it, and as such we don't introduce any hard dependencies on Messenger.

See a draft here: https://github.com/symfony/symfony/compare/6.3...tucksaun:symfony:scheduler/generator-context?expand=1

@kbond
Copy link
Member Author

kbond commented Apr 21, 2023

Nice (simple) approach! You chose not to add the next run timestamp? (I don't have a strong opinion on this as it can be retrieved from the trigger).

What do you think about including the schedule name? This is important context for me. Should it be added to the MessageContext, done like in #49864, or something else?

@tucksaun
Copy link
Contributor

You chose not to add the next run timestamp.

I didn't think about it tbh, I just wanted to draft quickly if this could be a way to do things or not.
But I like it, so I could easily add it if we say that I should open a PR with this approach.

What do you think about including the schedule name

In fact, I started to pick your change but then I was confused: should it go into the schedule or the generator? 🤔
So I went back to the same "draft" approach and left it aside for now.
Also, I was wondering, isn't it kind of available within the ReceivedStamp (in the context of Messenger only though)?

@kbond
Copy link
Member Author

kbond commented Apr 22, 2023

I didn't think about it tbh, I just wanted to draft quickly if this could be a way to do things or not.

I think it can't hurt to add. This will provide the expected next run time which may or may not match TriggerInterface::getNextRunDate().

But I like it, so I could easily add it if we say that I should open a PR with this approach.

I say open a PR - I like your approach better than this one. @upyx, what do you think?

In fact, I started to pick your change but then I was confused: should it go into the schedule or the generator?

I've struggled with this myself. The Schedule object itself doesn't have access to it's name. This is configured with the AsSchedule service attribute so it's only currently available in SchedulerTransportFactory as a service alias. I think it makes sense as an optional argument for MessageGenerator - then could be added to your MessageContext object.

Since MessageGenerator::__construct() takes the $checkpoint as a string|CheckpointInterface, if it's a string and no $name is passed, we could use that.

@kbond
Copy link
Member Author

kbond commented Apr 22, 2023

Also, I was wondering, isn't it kind of available within the ReceivedStamp (in the context of Messenger only though)?

It is, and marked with the schedule transport name (schedule_<name>). This is how I'm accessing it now (by removing schedule_). So, if we don't add, I'll continue to use this.


For some context, I'm working on a generic messenger-monitor bundle that has scheduler support. This is why I want all this context. In this bundle, a "monitored message" has queued_at, received_at and handled_at timestamps. This allows for some nice reporting. Currently, for messages that are from a scheduler transport, the queued_at and received_at are identical (because scheduled messages aren't async). My plan, once this triggeredAt value is available, is to set this as the queued_at value for these monitored messages. This will allow reporting on how far off the expected run time and the actual run time are. I think this is what you were looking to do in #50085.

@tucksaun
Copy link
Contributor

I think this is what you were looking to do in #50085.

This is not the same reason but this requires the same data indeed 😃

I say open a PR - I like your approach better than this one

Done. See #50130

@fabpot
Copy link
Member

fabpot commented Apr 24, 2023

Close in favor of #49792

@fabpot fabpot closed this Apr 24, 2023
fabpot added a commit that referenced this pull request Apr 24, 2023
…ing context (tucksaun)

This PR was merged into the 6.3 branch.

Discussion
----------

[Scheduler] Make `MessageGenerator` yield some scheduling context

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #50085, "replaces" #50096?, could help with #49864
| License       | MIT
| Doc PR        | no doc yet for Scheduler

This PR introduces a new `MessageContext` that `MessageGenerator` should use as key when yielding messages to provide more context about the scheduled message.
The consumer of the scheduled messages can decide to use it or not.
The PR also adds the wiring to add this context to the `ScheduledStamp` when using Scheduler within Messenger.
This allows to add more context but without introducing a hard dependency on Messenger.

/cc `@kbond`

Note: this PR is not adding the scheduler name (cf #49864) because I don't know exactly where to wire it yet but I don't see anything preventing it to be added to the context once we know where it should belong.

Commits
-------

3664d08 [Scheduler] Allow MessageGenerator to provide a MessageContext
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.

[Scheduler] Expose the run date?
5 participants