sheharyarn

sheharyarn

Update Oban Job's scheduled_at time

I have an Elixir app that uses Oban to schedule jobs at a time specified by users. For example, users can specify the date and time to send a certain email.

Oban works great at scheduling the job initially, and with other features like uniqueness, but I can’t find a sensible way to update the scheduled time of an existing job (when the user wants to change the time).

Currently, two approaches come to mind:

1. Cancel job and queue again

job =
  Oban.Job
  |> where(fragment("args->>'task_id'") == ^task_id)
  |> where([j], j.state in ~w[available scheduled])
  |> Repo.one()

Oban.cancel_job(TaskWorker, job.id)

job.args
|> TaskWorker.new(schedule_at: new_time)
|> Oban.insert()

2. Update job manually

Oban.Job
|> where(fragment("args->>'task_id'") == ^task_id)
|> where([j], j.state in ~w[available scheduled])
|> Repo.one()
|> Ecto.Changeset.cast(%{scheduled_at: new_time}, [:scheduled_at])
|> Repo.update()

But I worry that Oban might have its own hooks which do not get called when updating manually or if there’s a better way to do this.

Also posted on Stackoverflow:

Marked As Solved

sorentwo

sorentwo

Oban Core Team

Very soon, possibly today :slightly_smiling_face:

Also Liked

sorentwo

sorentwo

Oban Core Team

As of a recent (and unreleased) PR, there is built in support for this exact situation:

https://github.com/sorentwo/oban/pull/464

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement