Victor Björklund

Job scheduling in Elixir Phoenix using Quantum

Published: Jun 26 2023

As a developer working with the Phoenix framework or Elixir in general, you often encounter scenarios where you need to schedule recurring tasks or jobs. Whether it’s sending periodic notifications, updating data, or performing regular maintenance, a reliable job scheduler is essential. Cron, a popular UNIX utility, is widely used for such purposes.

Job scheduling in Elixir

However, in Elixir, we have several alternatives for this. You can build your own solution using a genserver, using Oban for scheduling using a database or using Quantum that provides cron-like job scheduling capabilities.

In this blog post, we will explore how to integrate Quantum into your Phoenix Elixir application and leverage its powerful features for job scheduling. Let’s get started!

What is Quantum?

Quantum is a versatile job scheduler library for Elixir. It provides an elegant API for defining and scheduling recurring jobs based on cron-like expressions. Quantum is built on top of OTP (Open Telecom Platform) principles, making it reliable, efficient, and highly scalable.

Setting Up Quantum in Phoenix

To start using Quantum in your Phoenix Elixir application, you need to first add it as a dependency in your mix.exs file. Open the file and locate the deps function. Add Quantum as a dependency like this:


defp deps do
  [
    {:quantum, "~> 3.5"}
  ]
end

Save the file and run mix deps.get to fetch the Quantum dependency.

The next step is to create a scheduler for your app:

defmodule MyApp.Scheduler do 
  use Quantum, otp_app: :my_app 
end

And finally we need to add our scheduler to our application’s supervision tree. Open your applicaion.ex file and add the scheduler to the list of children:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    Children = [
      # This is the new line
      MyApp.Scheduler
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Defining Cron Jobs

Now that we have Quantum set up, let’s define some cron jobs. A cron job specification consists of three components: the name of the job, the cron expression, and the module and function to be executed. Here’s an example of a simple cron job:

Next, we need to configure Quantum. Create a new file called quantum.ex in your application’s config directory. In this file, we’ll define the Quantum configuration. Here’s a basic example to get you started:


use Mix.Config

config :my_app, MyApp.Scheduler,
  Jobs: [
    # Define your cron jobs here
    # 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, []}}
  ]

Feel free to define as many cron jobs as you need. Quantum allows you to schedule jobs with different intervals and precise timing using cron expressions.

Running Your Phoenix Application

With Quantum configured and jobs defined, you can now start your Phoenix application and let Quantum handle the scheduling. Run mix phx.server or your preferred command to start the Phoenix.

Frequently asked questions