alexcastano

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

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.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement