Running a function everyday on an app that is deployed to Kubernetes.
config :myapp, MyApp.Scheduler,
jobs: [
{"0 11 * * *", fn -> MyApp.MyModule.my_function() end}
]
The function is making a few api calls that takes about 10s in total. What happens in production is that it runs multiple times in parallel, only the first process is showing up in the k8s pod logs but from the db logs and api service logs it’s clear that it ran multiple times.
All the solutions I’ve found so far uses a GenServer which is overkill since I only need to prevent a simple function to run more than once each day when called by Quantum.
One GenServer solution I found looks like this snippet source
def start_link() do
case GenServer.start_link(__MODULE__, nil, name: {:global, __MODULE__}) do
{:ok, pid} ->
{:ok, pid}
{:error, {:already_started, pid}} ->
Process.link(pid)
{:ok, pid}
:ignore ->
:ignore
end
end
Is there a simple solution to this that I have missed or can I use a Supervisor to prevent multiple instances? Is it possible to name a Task and get the same functionality as above?
Need help getting pointed in the right direction.
Thanks.