Skip to content

Latest commit

 

History

History
109 lines (84 loc) · 2.67 KB

README.md

File metadata and controls

109 lines (84 loc) · 2.67 KB

EctoJobScheduler

CircleCI Hex.pm Hex.pm

Helpers for scheduling Jobs defined in EctoJob

Thanks joaothallis, ramondelemos, victorprs

Installation

If available in Hex, the package can be installed by adding ecto_job_scheduler to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_job_scheduler, "~> 1.0.1"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_job_scheduler.

Usage

Define job

defmodule MyApplication.MyJob do
  @moduledoc false
  use EctoJobScheduler.Job,
    repo: MyApplication.Repo,
    otp_app: :my_application

  def handle_job(%JobInfo{multi: _multi}, %{"input" => input}) do
    case input do
      :a -> :ok
      :b -> {:ok, :gotta_go_fast}
      other -> {:error, :xablau}
    end
  end
end

Define job queue

defmodule MyApplication.MyJobQueue do
  @moduledoc false
  use EctoJobScheduler.JobQueue,
    table_name: "test_jobs",
    jobs: [
      MyApplication.MyJob
    ]
end

If you want new_relic instrumentation in your jobs, add new_relic_agent to the deps and

defmodule MyApplication.MyJobQueue do
  @moduledoc false
  use EctoJobScheduler.JobQueue,
    table_name: "test_jobs",
    jobs: [
      MyApplication.MyJob
    ],
    instrumenter: :new_relic
end

Define scheduler module for job queue

defmodule MyApplication.MyJobScheduler do
  @moduledoc false
  use EctoJobScheduler.JobScheduler,
  repo: MyApplication.Repo,
  job_queue: MyApplication.MyJobQueue
end

Configuration

config :my_application, MyApplication.MyJob, max_attempts: "15"

Running and scheduling jobs

Enqueuing right away

{:ok, job_queue} = MyJobScheduler.run(MyJob, %{"input" => "a"})
{:ok, job_queue} = MyJobScheduler.run(MyJob, %{"input" => "a"}, schedule:  ~N[2022-10-03 12:00:00.000000]) # pass additional options

Appending to Ecto.Multi

  multi = Multi.run(:do_my_thing, fn _repo, _changes -> {:ok, :xablau} end)
  multi = MyJobScheduler.schedule(multi, MyJob, %{"input" => "a"})
  result = MyJobScheduler.run(multi)