How to make sure a cleanup task will always run

Hello,

I have a phoenix app that serves user requests by fetching data from a storage backend and stream them to the client, taking selection parameters from the query string.

There are constraints that force me to first write the data locally on a scratch storage space and then only stream it.

Therefore, I added at the end of my pipeline a plug to clean the scratch after the data has been streamed to the user (with Plug.send_chunks).

Called like this at the very end of endpoint plug calls:

  plug WsdataselectWeb.Plug.Cleanup

But in some cases (internal error when managing a request or client aborting the connection), the pipeline is broken and the cleanup plug is not called.

I feel that my strategy is not the best. Would you suggest another way to do this ?

Maybe You could use Briefly[ briefly | Hex ], the file will live only for the time of the process.

1 Like

Definitely Briefly. Never had a problem with it, used it at least in 3 commercial projects. It just does what it says and gets out of the way.