Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Scheduler] Allow modifying the schedule at runtime and recalculate heap#51553

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:6.4fromJeroeny:recalc
Sep 26, 2023

Conversation

Jeroeny
Copy link
Contributor

@JeroenyJeroeny commentedSep 4, 2023
edited
Loading

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#51206
LicenseMIT

Schedules are created statically by a ScheduleProviderInterface. This is nice if you have a small app with some predetermined scheduled actions. But if you want to schedule business logic dynamically, e.g. have admins Crud a schedule in the DB, then the schedule needs to be updateable at runtime. This also allows for the app to re-add schedules that have returnednull as nextRunDate in the past (making them unscheduled) and to remove schedules.

Usage example: reading the schedule from a DB repository, adding messages based on an custom event:

class MyScheduleimplements ScheduleProviderInterface{private ?Schedule$schedule =null;publicfunction__construct(privateRecurrenceRepository$repository)    {    }publicfunctiongetSchedule():Schedule    {return$this->schedule ??= Schedule::with(...$this->repository->all());    }    #[AsEventListener(ScheduledMessageAdded::class)]publicfunctiononScheduleAdded(ScheduledMessageAdded$event):void    {$this->schedule->add($event->getMessage());    }}

Or in the context of Messenger, if some periodic batch task has no more work to do, the recurring message can be removed:

class MyScheduleimplements ScheduleProviderInterface{private ?Schedule$schedule =null;publicfunctiongetSchedule():Schedule    {return$this->schedule ??= Schedule::with(RecurringMessage::every(60,newBatchGenerateFiles()));    }publicfunctionclear()    {$this->schedule->clear();    }}class MyHandler {    #[AsMessageHandler]publicfunctionhandle(BatchGenerateFiles$message)    {// <-- handle message code -->if ($noMoreFilesToGenerate) {$this->mySchedule->clear();        }    }}

vtsykun, kbond, asrorbekh, Sophikitis, and gueguefun reacted with thumbs up emoji
@kbond
Copy link
Member

Interesting, is this a companion PR for#51542?

@Jeroeny
Copy link
ContributorAuthor

@kbond they don't depend on each other. But when both of them are merged, it enables a lot of capabilities for the Scheduler.

Copy link
Member

@fabpotfabpot left a comment

Choose a reason for hiding this comment

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

Can you add some usage example in the PR description?

Jeroeny reacted with thumbs up emoji
@@ -10,6 +10,7 @@ CHANGELOG
* Add `AbstractTriggerDecorator`
* Make `ScheduledStamp` "send-able"
* Add `ScheduledStamp` to `RedispatchMessage`
* Allow modifying the Schedule at runtime
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
* Allow modifyingtheSchedule at runtime
* Allow modifying Schedule instances at runtime

@fabpot
Copy link
Member

Thank you@Jeroeny.

@fabpotfabpot merged commitcb0974c intosymfony:6.4Sep 26, 2023
@JeroenyJeroeny deleted the recalc branchSeptember 27, 2023 12:52
fabpot added a commit that referenced this pull requestOct 16, 2023
This PR was merged into the 6.4 branch.Discussion----------[Scheduler] pre_run and post_run events| Q             | A| ------------- | ---| Branch?       | 6.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       |#49803 (comment)| License       | MITBased on#49803 `@kbond`  and taking into account#51553The aim of this PR is to be able to act on the accumulated messages 'if something happens' and to be able to recalculate the heap via events (currently pre_run and post_run).The aim is to have access to- the  the schedule and therefore add/cancel a certain type of message.- MessageContexte (e.g. access the id)- The message itselfThis PR would complement `@Jeroeny`#51553 PR by enabling action via events.Commits-------20fd21a [Scheduler] add PRE_RUN and POST_RUN events
This was referencedOct 21, 2023
@Sophikitis
Copy link

Hi !

Do you have any examples of use?
I'm trying to implement this, to dynamically retrieving frequencies and modifying the behavior with a custom event.
But this->schedule is always null in the listener function.
When I retrieve the instance from $this->getSchedule() I get an instance but when I try to delete a message or add one, nothing happens.

And as far as I understand, there's no documentation for this feature.
I'll try my luck here.
Thanks in advance

tolean and Hanmac reacted with thumbs up emoji

@Hanmac
Copy link
Contributor

Hanmac commentedApr 16, 2024
edited
Loading

@fabpot I'm also looking for an example, currently i uses this Doctrine Query to build the Schedule

publicfunctiongetSchedule():Schedule    {$result =newSchedule();$repo =$this->manager->getRepository(Terminal::class);$q =$repo->createQueryBuilder('t');$q->where('t.status = true AND t.automatic = true');foreach ($q->getQuery()->getResult()as$r) {/**             * @var Terminal $r             */$result->add(RecurringMessage::cron($r->getCronExpr(),newKassenschlussTask($r->getId()),$this->timezoneDetector->getTimezone())            );        }return$result;    }

because each object might have its own CronExpr, each of them needs its own RecurringMessage

and how do i handle if they get disabled?

Sophikitis and GhazanfarMir reacted with thumbs up emoji

@Hanmac
Copy link
Contributor

@Sophikitis while maybe not the best way, i foundStopWorkersCommand

$cacheItem =$this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);$cacheItem->set(microtime(true));$this->restartSignalCachePool->save($cacheItem);

i used[AsEntityListener] on my DB Entities, and then onEvents::postRemove, Events::postUpdate, Events::postPersist i stop the messenger worker

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@fabpotfabpotfabpot approved these changes

@kbondkbondAwaiting requested review from kbondkbond is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
6.4
Development

Successfully merging this pull request may close these issues.

[Scheduler] "dynamic" schedules
6 participants
@Jeroeny@kbond@fabpot@Sophikitis@Hanmac@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp