TODO: Add description
If available in Hex, the package can be installed
by adding rak
to your list of dependencies in mix.exs
:
def deps do
[
{:rak, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/rak.
config :mnesia,
dir: 'mnesia/#{Mix.env()}/#{node()}'
Run the following mix task to create the Mnesia schema and required databases:
$ mix rak.setup
defmodule MyApp.Worker do
use Rak.Worker
def perform(arg) do
# do something
end
end
Enqueue a job to be processed by the worker:
iex> Rak.enqueue(MyApp.Worker, :hello)
%Rak.Job{...}
defmodule MyApp.Worker do
use Rak.Worker, concurrency: 5
def perform(data) do
MyApp.Service.perform(data)
end
end
defmodule MyApp.Worker do
use Rak.Worker, max_retries: 3
def perform do
raise "failed"
end
end
Failing job retries 3 times:
iex> job = Rak.enqueue(MyApp.Worker)
iex> ... delay determined by retry strategy
iex> job in Rak.jobs(:failed)
true
defmodule MyApp.Worker do
use Rak.Worker
def perform do
# do something
end
def on_completed(%Rak.Job{} = job) do
# do something when job is completed
:ok
end
def on_failed(%Rak.Job{} = job, reason, stacktrace) do
# do something when job failed
:ok
end
end
defmodule MyApp.EventHandler do
use GenServer
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts)
end
def init(state) do
:ok = Rak.subscribe()
{:ok, state}
end
def handle_info({:completed, %Rak.Job{} = job}, state) do
IO.inspect(job, label: "received `completed` event")
{:noreply, state}
end
def handle_info({:failed, %Rak.Job{} = job}, state) do
IO.inspect(job, label: "received `failed` event")
{:noreply, state}
end
end
Listen for events:
iex> {:ok, pid} = MyApp.EventHandler.start_link()
iex> Rak.enqueue(MyApp.Worker, :ok)
%Rak.Job{...}
... job processed
received `completed` event: %Rak.Job{...}
iex> Rak.enqueue(MyApp.Worker, :error)
%Rak.Job{...}
... job processed
received `failed` event: %Rak.Job{...}