jschaeff
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 ?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
kokolegorille
Maybe You could use Briefly[ briefly | Hex ], the file will live only for the time of the process.
dimitarvp
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.
jschaeff
Thanks for the suggestion, I’ll give it a try.
By the way, I love this forum, always nice people giving good advice !
rhcarvalho
I never used Briefly / was unaware of its existence. If you truly care about the cleaning up eventually (and timely) happening, I’d consider having an out-of-band recurring “task” that cleans up orphaned files, e.g. as an Oban cron job (optional, but gives you visibility into past executions).
From reading the Briefly docs, my takeaway is that it aligns the lifetime of a temp dir with the one of a Process. You can likely achieve the same thing without the external dependency if you care about that. It also gives you a way to “clean up all”, which is probably not what you want in the system you described, but you could build your own “clean up all stale temp dirs” code.
jschaeff
I switched to Briefly. Even though I’m not sure what is the extent of a process in my context (is it a request handling ie. the whole plug pipeline, is it a function end?), it seems to work super well.
After 3 days in pre-production, (handling 3 reqs/s), not one temporary directory is left behind. All cleaned up nicely, my problem is solved
Thanks for the idea of Oban, I might use it if I need other jobs scheduling for this app.
rhcarvalho
Indeed in normal circumstances you should not see any problem. The same would be true with explicitly creating and deleting files (i.e. no errors/process crashes in between).
You can simulate a hard crash (which could be caused by a deployment, NIF or other conditions) with
kill -9 BEAM_PID. Then, depending on your deployment strategy, your app comes back (e.g. restarted bysystemdorDocker) and you have a handful of directories left over (because it is impossible to guarantee a certain line of code will always execute, think a hard powerloss, no programming language/environment can give that guarantee).Maybe you’ll be fine ignoring that, maybe you account for it. Other than a scheduled job, you can also clean up on startup.
The idea of having visibility on when and how often those things are happening would also help you operate the system in the long-run. Again, may not be relevant in your particular case.
Those decisions are on you depending on your system and goals.
Wish you success!
krasenyp
Just for future reference, I usually use an extra process which monitors my “main” process, the one which does the work. In your case it would be the request/response pipeline. You can spawn a process and when it receives a down message, it will clean the temporary resources.
Some people are scared of spawning processes but processes are great exactly for managing lifecycle and concurrency.
jschaeff
A quick feedback.
The reason why Briefly was so efficient was because of me calling
Briefly.cleanup removes every temporary directories. Period.
Briefly.cleanup/0as a final step of my plug pipeline. I thoughts there was some kind of scope for Briefly to only remove the directories related to the current plug, but it was naive of meSo, this was not that great for my service
The solution I now deployed in pre-production is based on a GenServer running a periodic cleanup. Oban seemed oversized for this.
This was the first GenServer I wrote, and it seems to work great.