I have a website where I'll have to create multiple background processes. Each process will be alive at most 20-30 minutes and it won't do any computations, instead it'll merely check a state of some application once in a minute passing to it a certain list of arguments. That's it. Supposedly per hour I'll create 5 such tasks.
These process don't have to communicate with each other.
How is it better to organize all that in Elixir? Via Task? Or is there a better way?
To make a simple scheduler like this you can for instance use Process.send_after
combined with GenServer's handle_info
, because message sent from the function above is handle as an info.
Eg.
defmodule YourGenServer do
use GenServer
def init(initial_value) do
Process.send_after(self, :check_state, 1000 * 60 * 20) # 20 minutes delay
end
def handle_info(:check_state, state) do
# do the task
Process.send_after(self, :check_state, 1000 * 60 * 20)
end
end
What's better solution for that? Tasks and Agents are GenServers as well, just their API is simpler. I used in my example GenServer to show you how it can look like. Propably the best idea for you is to use wrapper, that encapsulates server's callbacks under the hood and if you want to change the engine, you just do it smoothly.