Problem/Motivation
We just finished #3336243: Update Package Manager event documentation in package_manager.api.php but PostApplyEvent is not documented very well.
I thought of this because in #3318587-8: Research alternatives to curl request on post apply event during cron updates I had tried explain what our 2 subscribes to PostApplyEvent do and why if for some reason they did not run during a cron update it would not be a catastrophe
I was wondering if we should document that PostApplyEvent should always be used in this way, for nice to have clean-ups but not mission critical items.
Right now we don't consider your site is wrecked beyond repair if PostApplyEvent does not run, like we do if the apply operation itself fails, see #3293417: If an update failed to apply don't allow more use of the module. Also Automatic Updates does not have a UI running PostApplyEvent if for some reason get run in a cron update or even someone in the form.
This is fine as long as other subscribers to PostApplyEvent follow our example and don't use it for things that absolutely have to run or your site is considered broken.
One reason to not use PostApplyEvent for things that absolutely have to run after an update is that this event is only dispatched for Automatic Updates not updates run in other ways. We already have hook_update_n() and post update hooks for this. Core also has a mechanism to determine if these functions were actually run. We don't need to rebuild that.
Proposed resolution
Document that PostApplyEvent is not a replacement for hook_update_n and post update hooks and describe when it should be used
Remaining tasks
Issue fork automatic_updates-3341406
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
tedbowComment #3
wim leersSo you're thinking about a theoretical case where a contrib module would prefer implementing a
\Drupal\package_manager\Event\PostApplyEventsubscriber over usinghook_update_N()or a post-update hook?P.S.: also see my proposal at #3318587-13: Research alternatives to curl request on post apply event during cron updates which would likely allow us to remove all current
PostApplyEventsubscribers. If you agree with my proposal there, we could potentially remove thePostApplyEventaltogether, or at least make it@internalfor now? That'd eliminate this DX challenge completely!Comment #4
tedbowComment #5
wim leersFor #3.
Comment #6
tedbowUgghh. I was going to propose removing PostApplyEvent before because the need to do a second request in cron and in the console leads to a lot of complexity but then I realized we probably remove the 2nd second request anyways because it is probably important to send emails about cron update very soon after they happen, that would not work in my idea to get rid of PostApplyEvent
I will post it here anyways for now:
The not so great idea
Since we don't have concrete ideas about what
PostApplyEventshould be used besides our own use case and we if release a 3.0.x version with PostApply or core version with this event it will be BC break to remove, we should just remove it now and add it back if need.Right now our non-test subscribers are \Drupal\package_manager\EventSubscriber\UpdateDataSubscriber to clear the update data so the site shows there the correct available updates are a change and in
\Drupal\package_manager\StageBase::postApplywe calldrupal_flush_all_caches();so unless we have another solution topostApply()we would still need to do and we probably should not do it in the same request.idea for how to replace current functionality we need
\Drupal\package_manager\StageBase::applywe change the section to something like:We would set the state before the apply because afterwards it might not be possible if enough files have changed.
\Symfony\Component\HttpKernel\KernelEvents::REQUESTand checks theStageBase::NEEDS_POST_APPLYstate and runs the 2 things we do now in UpdateDataSubscriber and also drupal_flush_all_caches();.it then would delete the
StageBase::NEEDS_POST_APPLYstate.We would have to move the logic in
\Drupal\package_manager\StageBase::setNotApplyingdirectly into\Drupal\package_manager\StageBase::applyafter the commit.Remaining tasks
Determine how to handle deleting stages in cron.
In form we can just delete the stage in the next batch step but in cron that won't work. Right now we delete the stage in \Drupal\automatic_updates\CronUpdateStage::handlePostApply but hopefully we can get rid of that function.
End of idea
what I realized at the end of this is that
CronUpdateStage::handlePostApplyis where we send the emails about the update that just happened. We probably don't want to do that in the same request because the code is in an indeterminate state so we want to get out of the request as soon as possible. So if we can't get rid ofCronUpdateStage::handlePostApplyand the need to make a second request getting rid ofPostApplyEventprobably is not super important .Also @phenaproxima on idea for the PostApplyEvent was that it would allow a contrib module that handle git commit/push after an update.
Comment #7
wim leersDo I understand correctly:
PostApplyEventsubscribers? Examples: notifying site owners, housekeeping tasks such as destroying the stage, infrastructure tasks such as generating git commits. Example of what should NOT happen: modify codebase, modify database (e.g. modules abusing this to apply DB updates). All of these should take a short time, if they don't, then it's up to that particular logic to handle queueing and running it a later time.?
If so: 👍
Comment #8
wim leersPlease also see the related comment at #3318587-18: Research alternatives to curl request on post apply event during cron updates.
Comment #9
tedbowI gave a first try at the docs. I am sure they will need improvement.
Comment #11
phenaproximaI think this is a pretty good start, but I would change some phrasing. Here's the text I propose:
Comment #12
tedbowComment #13
phenaproximaLooks clear to me. If we agree on the wording, then I guess this is good to go.
Comment #15
phenaproxima