So let's say I want to send a bunch of emails or recreate sitemap or whatever every 4 hours, how would I do that in Phoenix or just with Elixir?
There is a simple alternative that does not require any external dependencies:
defmodule MyApp.Periodically do
use GenServer
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
# Do the work you desire here
schedule_work() # Reschedule once more
{:noreply, state}
end
defp schedule_work() do
Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours
end
end
Now in your supervision tree:
worker(MyApp.Periodically, [])
-
159
-
3Where should I put this file? Under lib/ directory of Phoenix project? Where do the test go, to test/periodically/*? – EugZol Nov 21 '15 at 18:59
-
9In lib because it is a long running process. You can put the test whatever makes sense, maybe "test/my_app/periodically_test.exs". – José Valim Nov 28 '15 at 11:14
-
2Any particular reason to not move
Process.send_after
into its own function so that the function can be called from bothinit
andhandle_info
? – Ryan Bigg Jan 11 '16 at 20:11 -
23@CodyPoll
:timer.send_interval
is fine but keep in mind that the intervals will constant. So imagine you want to do something every minute and, in the future, the work itself takes more than a minute. In such cases, you'd be working all the time and your message queue would grow unbounded. The solution above will always wait the given period after the work is done. – José Valim Jul 1 '16 at 9:26
Quantum lets you create, find and delete jobs at runtime.
Furthermore, you can pass arguments to the task function when creating a cronjob, and even modify the timezone if you're not happy with UTC.
If your app is running as multiple isolated instances (e.g. Heroku), there are job processors backed by Redis, that also support task scheduling:
Exq: https://github.com/akira/exq
-
1I think it will be an overkill for a lot of simple tasks that don't require it but thank you for the answer anyway. – NoDisplayName Aug 4 '16 at 22:04
-
You can use erlcron for that. You use it like
job = {{:weekly, :thu, {2, :am}},
{:io, :fwrite, ["It's 2 Thursday morning~n"]}}
:erlcron.cron(job)
A job
is a 2-element tuple. The first element is a tuple that represents the schedule for the job and the second element is the function or an MFA(Module, Function, Arity). In the above example, we run :io.fwrite("It's 2 Thursday morning")
every 2am of Thursday.
Hope that helps!
-
Yeah it's better than nothing, thank you. I will leave the question unanswered for a while, maybe there will be other suggestions – NoDisplayName Aug 19 '15 at 10:27
-
4You're welcome! There's also github.com/c-rack/quantum-elixir which is an elixir lib, if you prefer – Gjaldon Aug 19 '15 at 10:47
I used Quantum library Quantum- Elixir.
Follow below instructions.
#your_app/mix.exs
defp deps do
[{:quantum, ">= 1.9.1"},
#rest code
end
#your_app/mix.exs
def application do
[mod: {AppName, []},
applications: [:quantum,
#rest code
]]
end
#your_app/config/dev.exs
config :quantum, :your_app, cron: [
# Every minute
"* * * * *": fn -> IO.puts("Hello QUANTUM!") end
]
All set. Start the server by running below command.
iex -S mix phoenix.server