exq icon indicating copy to clipboard operation
exq copied to clipboard

Add queuing_timeout and execution_timeout option for job

Open bluzky opened this issue 4 years ago • 2 comments

This PR implement 2 timeout options for exq.

  • queuing_timeout (in second): TTL for job in queue, timed out job will be discard using a middle ware
  • execution_timeout (in second): if execution time is longer than execution_timeout the worker is killed and not retry any more.

This could be used as an example for those who want to implement timeout. This PR does not included test and execution

bluzky avatar Oct 07 '21 13:10 bluzky

Both of these can be implemented easily with existing APIs/middleware provided by Exq. I am not convinced about the benefit of adding this in Exq itself.

expiration

def perform() do
  job = Exq.worker_job()
  if !expired?(job.enqueued_at) do
    do_work()
  end
end

timeout

def perform() do
  {:ok, tref} = :timer.kill_after(:timer.seconds(timeout))

  try do
    do_work()
  after
    :timer.cancel(tref)
  end
end

Disable retry

for certain errors like the kill above

defmodule RetryMiddleware do
  def before_work(pipeline), do: pipeline
  def after_processed_work(pipeline), do: pipeline

  def after_failed_work(pipeline) do
    if non_retryable?(pipeline.assigns.error) do
      put_in(pipeline.assigns.job.retry, pipeline.assigns.job.retry_count || 0)
    else
      pipeline
    end
  end
end

ananthakumaran avatar Oct 07 '21 14:10 ananthakumaran

@ananthakumaran wow thank you for your simple solution

bluzky avatar Oct 08 '21 02:10 bluzky