bluejack

bluejack

In looking do a simple, but potentially slow task after the response has been delivered to the end user, I am looking for some lifecycle hook to trigger once the response is over.

In googling this I see references to “register_before_send” – but in fact I want to execute this code after send.

I see some contradictory older threads that look somewhat complicated involving task and/or task supervisor, but it’s not clear those are relevant to the current state of phoenix.

So: what is the best practice for doing work after a response has been successfully sent?

I was imagining there would be a straightforward way to add a post-processing plug or a post-action callback.

Showing Posts 1 to 7

msmithstubbs

msmithstubbs

I’m not aware of away to do this with plug, but Phoenix does emit telemetry events. You could subscribe to a telemetry [:phoenix, :endpoint, :stop] event and execute your task then.

LostKobrakai

LostKobrakai

You can decide on your own when exactly in the plug pipeline you send the response (using Plug.Conn.send_resp/1 or any code calling it). E.g. in a phoenix app that would happen as part of Phoenix.Controller.render/3. Sending a response doesn’t stop the plug pipeline though. You can have further plugs after that. You can even implement a similar callback registration to register_before_send if you want to.

If you don’t manually call Plug.Conn.send_resp will be called by plug at the end of a plug pipeline – expecting a response to be set on the conn.

bluejack

bluejack OP

Thanks for this tip; however, it appears to me that all plugs execute before the controller; so, I guess what I am going to try is having the controller call the send_resp, and then hand off to post response code.

This is a pretty typical setup for an api server with a router, in which the router opens with “pipe_through”

bluejack

bluejack OP

Additionally, when calling send_resp directly, everything works from the client’s perspective, but the Phoenix app reports an error:

[error] ** (Plug.Conn.NotSentError) a response was neither set nor sent from the connection

In the documentation for Conn send_resp, it notes that one should call halt/1, but neither doing this nor not doing this prevent the error.

LostKobrakai

LostKobrakai

You could plug a plug after the router (in endpoint.ex).

That error would suggest you did not call send_resp (at all), so not sure what’s up with that.

bluejack

bluejack OP

Right, but the response is being delivered to the client.

Update – the answer to that particular one is: I wasn’t returning the modified new conn, which probably gets marked with “response sent” for downstream record keeping.

Nonetheless, let me redirect to putting the plug in endpoint.ex instead of the pipeline, which would be the more general solution if I can make it work!

bluejack

bluejack OP

Final note: this IS the answer –

Adding the plug in endpoint.ex after the router.

This was way easier than I feared… and way harder to figure out than I expected. But I am still pretty new to Phoenix.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews