-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Oban.Cron with schedule_interval/4
The new Cron module allows processes, namely plugins, to get cron-like scheduled functionality with a single function call. This will allow plugins to removes boilerplate around parsing, scheduling, and evaluating for cron behavior.
- Loading branch information
Showing
5 changed files
with
60 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Oban.Cron do | ||
@moduledoc false | ||
|
||
alias Oban.Cron.Expression | ||
|
||
@spec schedule_interval(pid(), term(), binary(), Calendar.time_zone()) :: :ok | ||
def schedule_interval(pid, message, schedule, timezone \\ "Etc/UTC") do | ||
expression = Expression.parse!(schedule) | ||
|
||
:timer.apply_after(interval_to_next_minute(), fn -> | ||
now = DateTime.now!(timezone) | ||
|
||
if Expression.now?(expression, now) do | ||
send(pid, message) | ||
end | ||
|
||
schedule_interval(pid, message, schedule, timezone) | ||
end) | ||
|
||
:ok | ||
end | ||
|
||
@spec interval_to_next_minute(Time.t()) :: pos_integer() | ||
def interval_to_next_minute(time \\ Time.utc_now()) do | ||
time | ||
|> Time.add(60) | ||
|> Map.put(:second, 0) | ||
|> Time.diff(time) | ||
|> Integer.mod(86_400) | ||
|> :timer.seconds() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Oban.CronTest do | ||
use ExUnit.Case, async: true | ||
|
||
use ExUnitProperties | ||
|
||
alias Oban.Cron | ||
|
||
describe "interval_to_next_minute/1" do | ||
property "calculated time is always within a short future range" do | ||
check all hour <- integer(0..23), | ||
minute <- integer(0..59), | ||
second <- integer(0..59), | ||
max_runs: 1_000 do | ||
{:ok, time} = Time.new(hour, minute, second) | ||
|
||
assert Cron.interval_to_next_minute(time) in 1_000..60_000 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters