vikram7
Scheduling a task to run at 8AM UTC time every day
I currently have a task that runs once an hour. This is pretty straightforward using a GenServer. I can call Process.send_after(self(), :work, @interval) in the handle_info/2 function of that module.
If I need to run another task once every day at 8AM UTC, what would be a good way to do this natively (without any external libraries)? Is there something equivalent to Process.send_after except at a specific time?
Most Liked
efrenfuentes
You can check Quantum, you can config it like cron:
config :quantum, :your_app,
cron: [
# Every minute
"* * * * *": {"Heartbeat", :send},
# Every 15 minutes
"*/15 * * * *": fn -> System.cmd("rm", ["/tmp/tmp_"]) end,
# Runs on 18, 20, 22, 0, 2, 4, 6:
"0 18-6/2 * * *": fn -> :mnesia.backup('/var/backup/mnesia') end,
# Runs every midnight:
"@daily": &Backup.backup/0
]
idi527
I would calculate the initial_interval to 8AM on init, and then send_after 24 hours every time after that.
manukall
One thing to keep in mind when using Process.send_after for something like this is that your app might get restarted in between and that message won’t get send.
You could work around that by scheduling a message again on application startup, though.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










