Cancel an Oban job stuck for too long in available queue

Hi everyone,

Some of our jobs might take long to execute depending of the amount of data to process, blocking the queue for few minutes and being problematic for user experience.
This kind of event is rare and is due to our infra slowing down for whatever reason, and we should fix it in the medium term. Yet, for now when it happens we’d like to return an error message to users and let them retry by themselves, instead of letting them waiting for minutes.

I’m looking for ways to cancel a job that is waiting for too long in the available queue, and I’m afraid I’m missing a simple option before writing a homemade solution.

Would you have any idea of how to easily manage this case ?
(we use Oban Pro)

It sounds like you’re describing head-of-line blocking from long-running jobs. Depending on what you mean by “too long” and why you’re looking to cancel, there are a few possibilities.

Since you have Pro, here’s one possible solution—use Pro’s Relay to await a job from the insertion site. That will let you insert a job into the queue where it can run on any node and then await the results until a timeout period. For example:

relay =
  %{:ok, 4}
  |> Oban.Pro.Relay.async()

case Oban.Pro.Relay.await(relay, :timer.seconds(30)) do
  {:ok, result} ->
    # Do something with the result

  {:error, timeout} ->
    # Notify the customer that their job is still running

That way, you don’t have to cancel any jobs and can communicate when the queues run slowly.