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?
相关问题
- Convert a map into a keyword list in Elixir
- Understanding pattern matching in Elixir function
- What does “&1” mean in elixir function?
- Preload all Relationships
- How to select id with max date group by category i
相关文章
- Macro expansion in elixir: how to define 2 macros
- Chunking list based on struct type changing
- How to get data from Ecto in a custom mix task
- Elixir case on a single line
- How do you embed double-quotes an Elixir string?
- How To Get The Root Directory Of an Elixir Project
- How can I make Elixir mix test output more verbose
- elixir Logger for lists, tuples, etc
I used Quantum library Quantum- Elixir.
Follow below instructions.
All set. Start the server by running below command.
There is a simple alternative that does not require any external dependencies:
Now in your supervision tree:
You can use erlcron for that. You use it like
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!
Besides to use
Process.send_after
, you can also use :timer.apply_interval.I find
:timer.send_interval/2
slightly more ergonomic to use with aGenServer
thanProcess.send_after/4
(used in the accepted answer).Instead of having to reschedule your notification each time you handle it,
:timer.send_interval/2
sets up an interval on which you receive a message endlessly—no need to keep callingschedule_work()
like the accepted answer uses.Every 1000 ms (i.e., once a second),
IntervalServer.handle_info/2
will be called, print the currentcount
, and update the GenServer's state (count + 1
), giving you output like:Quantum is great, we use it at work as a cron replacement with a phoenix front-end and we also add jobs in real-time which is very neat.