Skip to content

Commit 2220c08

Browse files
milmazzsorentwo
authored andcommitted
Accept period durations for plugin timing options
Lifeline and Pruner intervals, along with Reindexer timeouts, now accept Oban.Period tuples such as `{30, :seconds}` in addition to millisecond integers.
1 parent ee1048a commit 2220c08

6 files changed

Lines changed: 53 additions & 20 deletions

File tree

lib/oban/plugins/lifeline.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ defmodule Oban.Plugins.Lifeline do
3232
3333
## Options
3434
35-
* `:interval` — the number of milliseconds between rescue attempts. The default is `60_000ms`.
35+
* `:interval` — the time between rescue attempts, as either an integer number of milliseconds
36+
or an `Oban.Period` tuple like `{1, :minute}`. The default is `60_000ms`.
3637
3738
* `:rescue_after` — the maximum amount of time a job may execute before being rescued, as either
3839
an integer number of milliseconds or an `Oban.Period` tuple like `{1, :hour}`. 1 hour
@@ -61,7 +62,7 @@ defmodule Oban.Plugins.Lifeline do
6162

6263
@type option ::
6364
Plugin.option()
64-
| {:interval, timeout()}
65+
| {:interval, Period.t()}
6566
| {:rescue_after, Period.t()}
6667

6768
defstruct [
@@ -82,19 +83,21 @@ defmodule Oban.Plugins.Lifeline do
8283

8384
state = struct!(State, opts)
8485

85-
GenServer.start_link(
86-
__MODULE__,
87-
%{state | rescue_after: Period.to_milliseconds(state.rescue_after)},
88-
name: name
89-
)
86+
state = %{
87+
state
88+
| interval: Period.to_milliseconds(state.interval),
89+
rescue_after: Period.to_milliseconds(state.rescue_after)
90+
}
91+
92+
GenServer.start_link(__MODULE__, state, name: name)
9093
end
9194

9295
@impl Plugin
9396
def validate(opts) do
9497
Validation.validate_schema(opts,
9598
conf: :any,
9699
name: :any,
97-
interval: :pos_integer,
100+
interval: :period,
98101
rescue_after: :period
99102
)
100103
end

lib/oban/plugins/pruner.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ defmodule Oban.Plugins.Pruner do
3030
3131
## Options
3232
33-
* `:interval` — the number of milliseconds between pruning attempts. The default is `30_000ms`.
33+
* `:interval` — the time between pruning attempts, as either an integer number of milliseconds
34+
or an `Oban.Period` tuple like `{30, :seconds}`. The default is `30_000ms`.
3435
3536
* `:limit` — the maximum number of jobs to prune at one time. The default is 10,000 to prevent
3637
request timeouts. Applications that steadily generate more than 10k jobs a minute should
@@ -59,7 +60,7 @@ defmodule Oban.Plugins.Pruner do
5960

6061
@type option ::
6162
Plugin.option()
62-
| {:interval, pos_integer()}
63+
| {:interval, Period.t()}
6364
| {:limit, pos_integer()}
6465
| {:max_age, Period.t()}
6566

@@ -82,17 +83,21 @@ defmodule Oban.Plugins.Pruner do
8283

8384
state = struct!(State, opts)
8485

85-
GenServer.start_link(__MODULE__, %{state | max_age: Period.to_seconds(state.max_age)},
86-
name: name
87-
)
86+
state = %{
87+
state
88+
| interval: Period.to_milliseconds(state.interval),
89+
max_age: Period.to_seconds(state.max_age)
90+
}
91+
92+
GenServer.start_link(__MODULE__, state, name: name)
8893
end
8994

9095
@impl Plugin
9196
def validate(opts) do
9297
Validation.validate_schema(opts,
9398
conf: :any,
9499
name: :any,
95-
interval: :pos_integer,
100+
interval: :period,
96101
limit: :pos_integer,
97102
max_age: :period
98103
)

lib/oban/plugins/reindexer.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ defmodule Oban.Plugins.Reindexer do
3030
3131
* `:schedule` — a cron expression that controls when to reindex. Defaults to `"@midnight"`.
3232
33-
* `:timeout` - time in milliseconds to wait for each query call to finish. Defaults to 15 seconds.
33+
* `:timeout` - the time to wait for each query call to finish, as either an integer number of
34+
milliseconds, or an `Oban.Period` tuple like `{15, :seconds}`. Defaults to 15 seconds.
3435
3536
* `:timezone` — which timezone to use when evaluating the schedule. To use a timezone other than
3637
the default of "Etc/UTC" you *must* have a timezone database like [tz][tz] installed and
@@ -43,7 +44,7 @@ defmodule Oban.Plugins.Reindexer do
4344

4445
use GenServer
4546

46-
alias Oban.{Cron, Peer, Plugin, Repo, Validation}
47+
alias Oban.{Cron, Peer, Period, Plugin, Repo, Validation}
4748
alias __MODULE__, as: State
4849

4950
require Logger
@@ -52,8 +53,8 @@ defmodule Oban.Plugins.Reindexer do
5253
Plugin.option()
5354
| {:indexes, [String.t()]}
5455
| {:schedule, String.t()}
56+
| {:timeout, Period.t()}
5557
| {:timezone, Calendar.time_zone()}
56-
| {:timeout, timeout()}
5758

5859
defstruct [
5960
:conf,
@@ -72,7 +73,11 @@ defmodule Oban.Plugins.Reindexer do
7273
def start_link(opts) do
7374
{name, opts} = Keyword.pop(opts, :name)
7475

75-
GenServer.start_link(__MODULE__, struct!(State, opts), name: name)
76+
state = struct!(State, opts)
77+
78+
GenServer.start_link(__MODULE__, %{state | timeout: Period.to_milliseconds(state.timeout)},
79+
name: name
80+
)
7681
end
7782

7883
@impl Plugin
@@ -82,7 +87,7 @@ defmodule Oban.Plugins.Reindexer do
8287
name: :any,
8388
indexes: {:list, :string},
8489
schedule: :schedule,
85-
timeout: :timeout,
90+
timeout: {:or, [:timeout, :period]},
8691
timezone: :timezone
8792
)
8893
end

test/oban/plugins/lifeline_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ defmodule Oban.Plugins.LifelineTest do
1414
assert :ok = Lifeline.validate(rescue_after: :timer.minutes(30))
1515
end
1616

17+
test "validating interval and rescue_after as period tuples" do
18+
assert {:error, _} = Lifeline.validate(interval: {0, :seconds})
19+
assert {:error, _} = Lifeline.validate(rescue_after: {1, :eon})
20+
21+
assert :ok = Lifeline.validate(interval: {1, :minute})
22+
assert :ok = Lifeline.validate(rescue_after: {30, :minutes})
23+
end
24+
1725
test "providing suggestions for unknown options" do
1826
assert {:error, "unknown option :inter, did you mean :interval?"} =
1927
Lifeline.validate(inter: 1)

test/oban/plugins/pruner_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ defmodule Oban.Plugins.PrunerTest do
1717
assert :ok = Pruner.validate(limit: 1_000)
1818
end
1919

20+
test "validating interval as a period tuple" do
21+
assert {:error, _} = Pruner.validate(interval: {0, :seconds})
22+
assert {:error, _} = Pruner.validate(interval: {1, :eon})
23+
24+
assert :ok = Pruner.validate(interval: {30, :seconds})
25+
assert :ok = Pruner.validate(interval: {1, :minute})
26+
end
27+
2028
test "validating max_age as a period tuple" do
2129
assert {:error, _} = Pruner.validate(max_age: {0, :days})
2230
assert {:error, _} = Pruner.validate(max_age: {1, :eon})

test/oban/plugins/reindexer_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ defmodule Oban.Plugins.ReindexerTest do
3030
assert :ok = Reindexer.validate(timezone: "America/Chicago")
3131
end
3232

33-
test "validating that :timeout is a non negative integer" do
33+
test "validating that :timeout is a non negative integer, :infinity, or a period tuple" do
3434
assert {:error, _} = Reindexer.validate(timeout: "")
3535
assert {:error, _} = Reindexer.validate(timeout: -1)
36+
assert {:error, _} = Reindexer.validate(timeout: {0, :seconds})
37+
assert {:error, _} = Reindexer.validate(timeout: {1, :eon})
3638

3739
assert :ok = Reindexer.validate(timeout: :timer.minutes(1))
40+
assert :ok = Reindexer.validate(timeout: :infinity)
41+
assert :ok = Reindexer.validate(timeout: {15, :seconds})
3842
end
3943

4044
test "providing suggestions for unknown options" do

0 commit comments

Comments
 (0)