I'm developing an app in my desktop computer (Mac OS) for which I created some cron tasks that run every 5 minutes. The code is the following (taken from here):
defmodule MyApp.CronJobs do
use GenServer
@shops ["billa","kaufland","lidl"]
def start_link do
GenServer.start_link(__MODULE__, %{})
end
def init(state) do
schedule_work() # Schedule work to be performed at some point
{:ok, state}
end
def handle_info(:work, state) do
Enum.each(@shops, &monitor_prices/1)
schedule_work() # Reschedule once more
{:noreply, state}
end
defp monitor_price(shop)
Mix.Task.run "monitor.#{shop}.all_prices"
end
defp schedule_work() do
Process.send_after(self(), :work, 5 * 60 * 1000)
end
end
On Supervision tree:
...
children = [
supervisor(MyApp.CronJobs, [])
...
]
opts = [ strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
The problem is this runs with the server, so whenever the computer goes on Sleep mode it stops running.
Is there a way to have the processes running on the background permanently without having the computer all the time in Full Power mode? Even better, Is there a way to have mix Tasks triggered every 5 minutes without running the server?