Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions lib/faktory_worker/batch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule FaktoryWorker.Batch do

"""
alias FaktoryWorker.Job
alias FaktoryWorker.Sandbox

@type bid :: String.t()

Expand Down Expand Up @@ -99,21 +100,26 @@ defmodule FaktoryWorker.Batch do
"""
@spec new!(Keyword.t()) :: {:ok, bid()} | {:error, any()}
def new!(opts \\ []) do
success = Keyword.get(opts, :on_success)
complete = Keyword.get(opts, :on_complete)
bid = Keyword.get(opts, :parent_bid)
description = Keyword.get(opts, :description)

payload =
%{}
|> maybe_put_description(description)
|> maybe_put_parent_bid(bid)
|> maybe_put_callback(:success, success)
|> maybe_put_callback(:complete, complete)
|> validate!()

opts = Keyword.take(opts, [:faktory_name, :timeout])
FaktoryWorker.send_command({:batch_new, payload}, opts)
if Sandbox.active?() do
{:ok, Sandbox.batch_id}
else
success = Keyword.get(opts, :on_success)
complete = Keyword.get(opts, :on_complete)
bid = Keyword.get(opts, :parent_bid)
description = Keyword.get(opts, :description)

payload =
%{}
|> maybe_put_description(description)
|> maybe_put_parent_bid(bid)
|> maybe_put_callback(:success, success)
|> maybe_put_callback(:complete, complete)
|> validate!()

opts = Keyword.take(opts, [:faktory_name, :timeout])
FaktoryWorker.send_command({:batch_new, payload}, opts)

end
end

@doc """
Expand All @@ -123,7 +129,11 @@ defmodule FaktoryWorker.Batch do
is committed, but
"""
def commit(bid, opts \\ []) do
FaktoryWorker.send_command({:batch_commit, bid}, opts)
if Sandbox.active?() do
{:ok, :no_content}
else
FaktoryWorker.send_command({:batch_commit, bid}, opts)
end
end

@doc """
Expand All @@ -135,7 +145,11 @@ defmodule FaktoryWorker.Batch do
After opening the batch, it must be committed again using `commit/2`.
"""
def open(bid, opts \\ []) do
FaktoryWorker.send_command({:batch_open, bid}, opts)
if Sandbox.active?() do
{:ok, :no_content}
else
FaktoryWorker.send_command({:batch_open, bid}, opts)
end
end

@doc """
Expand All @@ -144,7 +158,12 @@ defmodule FaktoryWorker.Batch do
Returns a map representing the status
"""
def status(bid, opts \\ []) do
FaktoryWorker.send_command({:batch_status, bid}, opts)
if Sandbox.active?() do
{:ok, Sandbox.batch_status()}
else
FaktoryWorker.send_command({:batch_status, bid}, opts)
end

end

defp maybe_put_description(payload, nil), do: payload
Expand Down
11 changes: 11 additions & 0 deletions lib/faktory_worker/sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,15 @@ defmodule FaktoryWorker.Sandbox do
jobs = Map.update(state.jobs, worker_mod, %{}, fn _ -> %{} end)
{:reply, :ok, %{state | jobs: jobs}}
end

def batch_id, do: "sandbox-batch-id"

def batch_status, do: %{
"bid" => batch_id(),
"total" => 10,
"pending" => 14,
"failed" => 3,
"created_at" => "2019-11-18T13:48:25Z",
"description" => "This is a sandbox status"
}
end