Hi, I’ve got a list of Things – actually called Apps which can be a confusing name so let’s stick with Things – that I display to the user. The user can click on the ‘Load’ button next to each Thing. That kicks of a series of HTTPoison calls and then some database writes. Any user can click on multiple buttons and there’s a slight chance there could be multiple users using the system.
If I simply call the load/1
function and the user will be stuck in that page. Specially if the user makes various load/1
calls.
I was looking for a different alternative by reading some documentation, forums, and chatting it up with Claude. The acceptance criteria is something like:
- a Thing can only have 1
load/1
workflow running on it at a time - if the workflow is running on a given Thing, users should not be allowed to start another one – aka disable the button.
I’m unsure of what approach I should use.
My standard approach would be to use the database, meaning
def load(thing) do
Thing.update_thing(thing, %{processing: true})
Task.async(... do the work ...)
Thing.update_thing(think, %{processing: false})
end
But I’m encountering lots of sharp edges that I would have to deal with:
- what happens if the user navigates away from the original page?
- how can I update the status of the button when the Thing is no longer blocked?
- is writing to the same db record twice a good idea?
- …
So if Elixir or Phoenix have a more elegant solution. I’m wondering if this is not the right opportunity to implement a GenServer (or GenStage)?