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

Skip to content

[Scheduler] Using before, after or onFailure methods causes unhandled exception when not configured #53722

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
CoalaJoe opened this issue Feb 1, 2024 · 2 comments · Fixed by #57282

Comments

@CoalaJoe
Copy link
Contributor

CoalaJoe commented Feb 1, 2024

Symfony version(s) affected

6.4

Description

According to the documentation at:
https://symfony.com/doc/current/scheduler.html#a-strategic-event-handling
image

Using those functions like that does not work and results in a 500 error

{"message":"Uncaught Error: Call to a member function addListener() on null","context":{"exception":{"class":"Error","message":"Call to a member function addListener() on null","code":0,"file":"/app/vendor/symfony/scheduler/Schedule.php:144"}},"channel":"php","extra":{},"severity":"CRITICAL","time":"2024-02-01T13:22:04.559+01:00"}

How to reproduce

     // ...
    public function getSchedule(): Schedule
    {
        $this->schedule ??= (new Schedule())
            ->with(...$this->getGeneralMessages(), ...$this->getProductionMessages())
            ->before(function (PreRunEvent $event) {
                $this->logger?->info('Before Event');
            })
            ->after(function (PostRunEvent $event) {
                $this->logger?->info('After Event');
            })
            ->onFailure(function (FailureEvent $event) {
                $this->logger?->info('Failure Event');
                $this->logger?->info($event->getError()->getMessage());
            })
            ->lock($this->lockFactory->createLock('cron-cache'))
            ->stateful($this->cache);

        return $this->schedule;
    }

Possible Solution

Either update documentaiton, add error handling and throw our own exception or fail silently and log an error?

Additional Context

No response

@kl3sk
Copy link

kl3sk commented May 31, 2024

I face the same problem and manage to fix it by injecting the dispatcher on Scheduler contruct

    public function __construct(
        private readonly CacheInterface $cache,
        private readonly EventDispatcherInterface $dispatcher,
    )
    {
    }

    public function getSchedule(): Schedule
    {
        return $this->schedule ??= (new Schedule($this->dispatcher))
            ->before(function (PreRunEvent $event) {
...

@fabpot
Copy link
Member

fabpot commented Jun 1, 2024

Code fix is here: #57282
Doc fix is here: symfony/symfony-docs#19935

@fabpot fabpot closed this as completed Jun 1, 2024
fabpot added a commit that referenced this issue Jun 1, 2024
… passed to a Schedule (fabpot)

This PR was merged into the 6.4 branch.

Discussion
----------

[Scheduler] Throw an exception when no dispatcher has been passed to a Schedule

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #53722 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

Commits
-------

dfd65d6 [Scheduler] Throw an exception when no dispatcher has been passed to a Schedule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants