betinajessen

betinajessen

How to run a job periodically

How can I schedule code to run every few hours in Elixir or Phoenix framework?

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?

Most Liked

dimitarvp

dimitarvp

I recommend you to make a small GenServer that you put in your app’s supervision tree – so it stays alive for as long as the app is live – and have it do something like this:

defmodule PeriodicWorker do
  use GenServer

  @impl true
  def init(period_in_millis) do
    # The `{:continue, :init}` tuple here instructs OTP to run `handle_continue`
    # which in this case will fire the first `:do_stuff` message so the worker
    # does its job once and then schedules itself to run again in the future.
    # Without this you'd have to manually fire the message to the worker
    # when your app starts.
    {:ok, period_in_millis, {:continue, :init}}
  end

  def handle_continue(:init, period_in_millis) do
    GenServer.call(self(), {:do_stuff, period_in_millis})
  end

  @impl true
  def handle_call(:do_stuff, _caller_pid, period_in_millis) do
    do_the_thing_you_need_done_periodically_here()

    schedule_next_do_stuff(period_in_millis)

    # or change `:ok` to the return value of the function that does the real work.
    {:reply, :ok}
  end

  def schedule_next_do_stuff(period_in_millis) do
    Process.send_after(self(), :do_stuff, period_in_millis)
  end
end

You can then supervise it like this in your app:

defmodule YourApp do
  use Application

  def start(_type, _args) d
    children = [
      {PeriodicWorker, 4 * 60 * 60 * 1000}, # 4 hours
      # ... other children ....
    ]

    options = [strategy: :one_for_one, name: YourApp.Supervisor]
    Supervisor.start_link(children, options)
  end
end

Not tested but I’ve done this a number of times and it should match reality closely enough.

derpycoder

derpycoder

Here’s a concrete example, so you can copy and learn from it.

Clone the Plausible Analytics repo, and search by Oban.Worker, you will see tons of example!!

Some Excerpts:

for site <- sites do
    SendEmailReport.new(%{site_id: site.id, interval: "weekly"},
      scheduled_at: monday_9am(site.timezone)
    )
    |> Oban.insert!()
end

def monday_9am(timezone) do
    Timex.now(timezone)
    |> Timex.shift(weeks: 1)
    |> Timex.beginning_of_week()
    |> Timex.shift(hours: 9)
end

OR

for site <- sites do
    SendEmailReport.new(%{site_id: site.id, interval: "monthly"},
      scheduled_at: first_of_month_9am(site.timezone)
    )
    |> Oban.insert!()
end

def first_of_month_9am(timezone) do
    Timex.now(timezone)
    |> Timex.shift(months: 1)
    |> Timex.beginning_of_month()
    |> Timex.shift(hours: 9)
end

EmailReports Module

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"interval" => "weekly", "site_id" => site_id}}) do
    # Send weekly report email
  end

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"interval" => "monthly", "site_id" => site_id}}) do
    # Send monthly report email
  end
derpycoder

derpycoder

Here’s a site I found that mentions 3 ways to get it done:

https://blog.kommit.co/3-ways-to-schedule-tasks-in-elixir-i-learned-in-3-years-working-with-it-a6ca94e9e71d

The first approach is GenServer which @dimitarvp mentioned.

If your requirements are not complex, you don’t need instrumentation and are not running in distributed mode, then you don’t need anything more.

But if you would like something more, checkout: Oban Git & Documentation

Oban: Robust job processing in Elixir, backed by modern PostgreSQL. Reliable,
observable and loaded with enterprise grade features.

AstonJ

AstonJ

You may also be interested in previous threads on this topic:

:smiley:

cmo

cmo

It depends how accurate you want it to be. If that release is restarted when the timer is a 1min then it’s going to be 1min between those two jobs. You could persist last_executed_at and use that to determine the initial delay.

Oban is a library option.

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement