Defer tasks until after response is sent #15622
-
The The This would make it possible to defer tasks that should only happen after a response has been sent to the client. Tasks such as garbage collection, email notifications, etc., that should happen in a request in a non-blocking manner. Currently one might reach for queue jobs for such asynchronous tasks, but deferred tasks could be useful for short-lived, non-blocking processes. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Tricking PHP into continuing to execute after the response has been sent is pretty hacky. Craft makes it possible via But you’re free to call that |
Beta Was this translation helpful? Give feedback.
Tricking PHP into continuing to execute after the response has been sent is pretty hacky. Craft makes it possible via
craft\web\Response::sendAndClose()
, and we call it on queue runner requests before processing the queue. But it’s not something we would want to do automatically on every single request, just in case something has registered anON_AFTER_RESPONSE
event handler.But you’re free to call that
sendAndClose()
method yourself in cases where you need to do post-response work!