Helpers for scheduling Jobs defined in EctoJob
Thanks joaothallis, ramondelemos, victorprs
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.
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
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
defmodule MyApplication.MyJobScheduler do
@moduledoc false
use EctoJobScheduler.JobScheduler,
repo: MyApplication.Repo,
job_queue: MyApplication.MyJobQueue
end
config :my_application, MyApplication.MyJob, max_attempts: "15"
{: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
multi = Multi.run(:do_my_thing, fn _repo, _changes -> {:ok, :xablau} end)
multi = MyJobScheduler.schedule(multi, MyJob, %{"input" => "a"})
result = MyJobScheduler.run(multi)