-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from akira/dequeue_controller
Add ability to control the dequeue
- Loading branch information
Showing
9 changed files
with
217 additions
and
105 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,41 @@ | ||
defmodule Exq.Dequeue.Behaviour do | ||
@moduledoc """ | ||
Custom concurreny or rate limiting at a queue level can be achieved | ||
by implementing the Dequeue behaviour | ||
The following config can be used to customize dequeue behaviour for a queue | ||
config :exq, | ||
queues: [{"default", {RateLimiter, options}}] | ||
RateLimiter module should implement `Exq.Dequeue.Behaviour`. The | ||
options supplied here would be passed as the second argument to the | ||
`c:init/2` function. | ||
### Life cycle | ||
`c:init/2` will be invoked on initialization. The first argument will contain info | ||
like queue and the second argument is user configurable. | ||
`c:available?/1` will be invoked before each poll. If the | ||
returned value contains `false` as the second element of the tuple, | ||
the queue will not polled | ||
`c:dispatched/1` will be invoked once a job is dispatched to the worker | ||
`c:processed/1` will be invoked if a job completed successfully | ||
`c:failed/1` will be invoked if a job failed | ||
`c:stop/1` will be invoked when a queue is unsubscribed or before the | ||
node terminates. Note: there is no guarantee this will be invoked if | ||
the node terminates abruptly | ||
""" | ||
|
||
@callback init(info :: %{queue: String.t()}, args :: term) :: {:ok, term} | ||
@callback stop(state :: term) :: :ok | ||
@callback available?(state :: term) :: {:ok, boolean, term} | ||
@callback dispatched(state :: term) :: {:ok, term} | ||
@callback processed(state :: term) :: {:ok, term} | ||
@callback failed(state :: term) :: {:ok, term} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Exq.Dequeue.Local do | ||
@behaviour Exq.Dequeue.Behaviour | ||
|
||
defmodule State do | ||
@moduledoc false | ||
|
||
defstruct max: nil, current: 0 | ||
end | ||
|
||
@impl true | ||
def init(_, options) do | ||
{:ok, %State{max: Keyword.fetch!(options, :concurrency)}} | ||
end | ||
|
||
@impl true | ||
def stop(_), do: :ok | ||
|
||
@impl true | ||
def available?(state), do: {:ok, state.current < state.max, state} | ||
|
||
@impl true | ||
def dispatched(state), do: {:ok, %{state | current: state.current + 1}} | ||
|
||
@impl true | ||
def processed(state), do: {:ok, %{state | current: state.current - 1}} | ||
|
||
@impl true | ||
def failed(state), do: {:ok, %{state | current: state.current - 1}} | ||
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
Oops, something went wrong.