Skip to content

Commit

Permalink
Fast fail on impossible year scenarios (#51)
Browse files Browse the repository at this point in the history
* Refactor search_and_correct_date call to case on the whole response

* Make search_and_correct_date fast-fail on impossible year scenarios

* Add checks on integer-ness of target years to only match on literal years
  • Loading branch information
mtrudel authored and maennchen committed Sep 6, 2018
1 parent 078efc0 commit 40bd8e8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/crontab/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,39 @@ defmodule Crontab.Scheduler do
end

defp get_run_date(conditions, date, max_runs, direction) do
{status, corrected_date} = search_and_correct_date(conditions, date, direction)
case search_and_correct_date(conditions, date, direction) do
{:found, corrected_date} ->
{:ok, corrected_date}

case status do
:found -> {:ok, corrected_date}
_ -> get_run_date(conditions, corrected_date, max_runs - 1, direction)
{:error, :impossible} ->
{:error, "No compliant date was found for your interval."}

{:not_found, corrected_date} ->
get_run_date(conditions, corrected_date, max_runs - 1, direction)
end
end

@spec search_and_correct_date(CronExpression.condition_list(), NaiveDateTime.t(), direction) ::
NaiveDateTime.t() | {:not_found, NaiveDateTime.t()}

defp search_and_correct_date(
[{:year, [target_year]} | _],
%NaiveDateTime{year: from_year},
:increment
)
when is_integer(target_year) and target_year < from_year do
{:error, :impossible}
end

defp search_and_correct_date(
[{:year, [target_year]} | _],
%NaiveDateTime{year: from_year},
:decrement
)
when is_integer(target_year) and target_year > from_year do
{:error, :impossible}
end

defp search_and_correct_date([{interval, conditions} | tail], date, direction) do
if matches_date?(interval, conditions, date) do
search_and_correct_date(tail, date, direction)
Expand Down

0 comments on commit 40bd8e8

Please sign in to comment.