defmodule Pollin.CallbackInterface do @moduledoc """ Behaviour for Callback workers and services """ use Behaviour @callback push(atom, Pollin.Resource.Callback.t, charlist) :: :ok @callback pop(atom) :: Pollin.Resource.Callback.t | nil @callback pop(atom, charlist) :: Pollin.Resource.Callback.t | nil @callback pop(atom, %{phase: charlist, offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback pop(atom, %{offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback fetch(atom) :: Pollin.Resource.Callback.t | nil @callback fetch(atom, charlist) :: Pollin.Resource.Callback.t | nil @callback fetch(atom, %{phase: charlist, offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback fetch(atom, %{offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback fetch_reverse(atom, %{phase: charlist, offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback fetch_reverse(atom, %{offset: integer, limit: integer}) :: list(Pollin.Resource.Callback.t) @callback count(atom) :: integer @callback count(atom, %{phase: charlist}) :: integer @callback dump(atom) :: list(Pollin.Resource.Callback.t) @callback reset(atom) :: :ok @callback delete(atom, charlist) :: :ok @callback update_phase(atom, charlist, charlist) :: :ok defmacro __using__(_) do quote do @behaviour Pollin.CallbackInterface ## Public api @doc """ Push a callback resource to an endpoint ## Example ``` CallbackWorker.push(id, %Pollin.Resource.Callback{}) ``` """ def push(id, %Pollin.Resource.Callback{} = callback, secret \\ ""), do: GenServer.cast(id, {:push, callback, secret}) @doc """ Pop a callback from endpoint ## Example ``` callback = CallbackWorker.pop(id) ``` """ def pop(id), do: GenServer.call(id, {:pop}) @doc """ Pop a callback by an exact key from endpoint ## Example ``` callback = CallbackWorker.pop(id, key) ``` """ def pop(id, key) when is_binary(key), do: GenServer.call(id, {:pop, key}) @doc """ Pop list of callbacks from an endpoint queue by given phase, offset and limit ## Example ``` callbacks = CallbackWorker.pop(id, opts) ``` """ def pop(id, %{phase: phase, offset: offset, limit: limit} = opts) when is_binary(phase) and is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:pop, opts}) @doc """ Pop list of callbacks from endpoint queue by given options offset and limit ## Example ``` callbacks = CallbackWorker.pop(id, opts) ``` """ def pop(id, %{offset: offset, limit: limit} = opts) when is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:pop, opts}) @doc """ Pop a callback from endpoint ## Example ``` callback = CallbackWorker.fetch(id) ``` """ def fetch(id), do: GenServer.call(id, {:fetch}) @doc """ Pop a callback by an exact key from endpoint ## Example ``` callback = CallbackWorker.fetch(id, key) ``` """ def fetch(id, key) when is_binary(key), do: GenServer.call(id, {:fetch, key}) @doc """ Fetch list of callbacks from an endpoint queue by given phase, offset and limit ## Example ``` callbacks = CallbackWorker.fetch(id, opts) ``` """ def fetch(id, %{phase: phase, offset: offset, limit: limit} = opts) when is_binary(phase) and is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:fetch, opts}) @doc """ Fetch list of callbacks from endpoint queue by given offset and limit ## Example ``` callbacks = CallbackWorker.fetch(id, opts) ``` """ def fetch(id, %{offset: offset, limit: limit} = opts) when is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:fetch, opts}) @doc """ Fetch reverse list of callbacks from an endpoint queue by given phase, offset and limit ## Example ``` callbacks = CallbackWorker.fetch(id, opts) ``` """ def fetch_reverse(id, %{phase: phase, offset: offset, limit: limit} = opts) when is_binary(phase) and is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:fetch_reverse, opts}) @doc """ Fetch reverse list of callbacks from endpoint queue by given offset and limit ## Example ``` callbacks = CallbackWorker.fetch(id, opts) ``` """ def fetch_reverse(id, %{offset: offset, limit: limit} = opts) when is_integer(offset) and is_integer(limit), do: GenServer.call(id, {:fetch_reverse, opts}) @doc """ Count all callbacks of an endpoint ## Example ``` count = CallbackWorker.count(id) ``` """ def count(id), do: GenServer.call(id, {:count}) @doc """ Count all callbacks of an endpoint with filter options ## Example ``` count = CallbackWorker.count(id, %{phase: "unprocessed"}) ``` """ def count(id, %{phase: phase} = opts) when is_binary(phase), do: GenServer.call(id, {:count, opts}) @doc """ Dump all callbacks of an endpoint ## Example ``` callbacks = CallbackWorker.count(id) ``` """ def dump(id), do: GenServer.call(id, {:dump}) @doc """ Reset all callbacks of an endpoint ## Example ``` CallbackWorker.reset(id) ``` """ def reset(id), do: GenServer.cast(id, {:reset}) @doc """ Remove a callback ## Example ``` CallbackWorker.delete(id, key) ``` """ def delete(id, key) when is_binary(key), do: GenServer.cast(id, {:delete, key}) @doc """ Update phase of a callback ## Example ``` CallbackWorker.update_phase(id, key, phase) ``` """ def update_phase(id, key, phase) when is_binary(key) and is_binary(phase), do: GenServer.cast(id, {:update_phase, key, phase}) defoverridable [ {:push, 2}, {:pop, 1}, {:pop, 2}, {:fetch, 1}, {:fetch, 2}, {:count, 1}, {:count, 2}, {:dump, 1}, {:reset, 1}, {:delete, 2}, {:update_phase, 3} ] end end end