alexcastano
Exclusive Periodic Task in Elixir Cluster
Hello,
I want to run a task every X minutes, but I’d like to be done only once. Because of this, I chose a solution using Genserver & Timeout, like this one: https://nesteryuk.info/2017/08/13/recurring-tasks-in-elixir-solution-2.html
I added this GenServer to my application tree with the name: {:global, __MODULE__}. This way avoid this task is executed each time in each cluster. After deploying, I saw a big problem. Only one node is alive because the other ones died trying to set up the periodic task which is impossible because is already registered in the alive node.
So I was thinking to check in the init function if the name has already been registered. In this case, it could return :ignore. But I see a big problem with this approach, if the whole node died unexpectly, the task won’t be executed again until a new node goes up. Not very resilient.
So, my main question is: if is there a simple way to achieve my goals or I should go with a more complex solution like using quantum?
PD: I like the GenServer solution because I can change the interval in runtime.
Thanks for your time
Marked As Solved
dom
This does what you were trying to do: Singleton - global, supervised singleton processes for Elixir
The key is to do the name registration after the process starts, and then branch on the result:
- If the registration succeeds, act as the “master”, running the periodic tasks
- If it fails, then act as “follower” and just monitor the master. Once the master dies, try to take its place.
Popular in Questions
Other popular topics
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
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








