Hi,
I have the following worker:
defmodule MyApp.Workers.ProductWelcomeEmail do
use Oban.Worker,
max_attempts: 3,
queue: :email,
tags: ["product_welcome_email"]
@impl Oban.Worker
def perform(%Oban.Job{}) do
:ok
end
end
This executes fine in production, and schedules as expected. When calling it in a test:
IO.inspect(
Workers.ProductWelcomeEmail.new(
%{},
schedule_in: 3600
)
|> Oban.insert()
)
I receive the following output:
{:ok,
%Oban.Job{
__meta__: #Ecto.Schema.Metadata<:built, "oban_jobs">,
id: nil,
state: "completed",
queue: "email",
worker: "MyApp.Workers.ProductWelcomeEmail",
args: %{},
meta: %{},
tags: ["product_welcome_email"],
errors: [],
attempt: 1,
attempted_by: ["Marks-MBP"],
max_attempts: 3,
priority: 0,
attempted_at: ~U[2023-05-25 09:27:36.593744Z],
cancelled_at: nil,
completed_at: ~U[2023-05-25 09:27:36.642205Z],
discarded_at: nil,
inserted_at: nil,
scheduled_at: ~U[2023-05-25 09:27:36.593745Z],
conf: %Oban.Config{
dispatch_cooldown: 5,
engine: Oban.Engines.Basic,
get_dynamic_repo: nil,
log: false,
name: Oban,
node: "Marks-MBP",
notifier: Oban.Notifiers.Postgres,
peer: false,
plugins: [],
prefix: "public",
queues: [],
repo: MyApp.Repo,
shutdown_grace_period: 15000,
testing: :inline
},
conflict?: false,
replace: nil,
unique: nil,
unsaved_error: nil
}}
The job is executing immediately, meaning I canāt make any assertions on a worker being enqueued.
Putting an IO.inspect inside the workerās logic confirms itās being executed immediately upon being scheduled.
Iāve tried using both scheduled_at
and schedule_in
without any joy - and Iām triggering the code inside the test with:
Oban.Testing.with_testing_mode(:manual, fn ->
conn
|> as_user(user)
|> post(Routes.book_and_pay_path(conn, :complete_free_booking), %{
"payment_id" => payment.id
})
|> json_response(200)
end)
I believe using with_testing_mode(:manual)
is the way to go but if thatās wrong please let me know, any advice on how to get the jobs scheduling properly in test mode is greatly appreciated.