elixirphoenix-framework

Retry function on failure after x amount of time


PROBLEM:

I have a function that gets triggered VIA Cronjob. This cronjob fires the function at 10:00pm but let's say the data required is not present for the function to properly be handled. I want to fail but then have the function retry an 1 hour. How would I handle this problem?

CODE:

  def check_question do
    case question = Repo.get_by(Question, active: true, closed: true) do

    question when not(is_nil(question)) ->
      case ActiveQuestion.ready_for_answer_status(conn, question) do

      end
    _ ->
      # RETRY CODE HERE
    end
  end

So as you can see from this bit of code. If the query does not find the question I send the case statement to a failure but inside that fail case I want to return the function in 1 hour. How can I achieve this with Elixir/Phoenix?


Solution

  • I prefer to enqueue tasks into a job system like Exq (redis backed) or ecto_job (postgresql backed) from the cron job.

    From there it will automatically retry up to some maximum with a delay between retries.

    It also has the benefit of surviving server reboots etc.