Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setup and teardown callbacks #14

Merged
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
3 changes: 3 additions & 0 deletions lib/que/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule Que.Job do

{:ok, pid} =
Que.Helpers.do_task(fn ->
job.worker.on_setup(job)
job.worker.perform(job.arguments)
end)

Expand All @@ -78,6 +79,7 @@ defmodule Que.Job do

Que.Helpers.do_task(fn ->
job.worker.on_success(job.arguments)
job.worker.on_teardown(job)
end)

%{ job | status: :completed, pid: nil, ref: nil }
Expand All @@ -96,6 +98,7 @@ defmodule Que.Job do

Que.Helpers.do_task(fn ->
job.worker.on_failure(job.arguments, error)
job.worker.on_teardown(job)
end)

%{ job | status: :failed, pid: nil, ref: nil }
Expand Down
26 changes: 25 additions & 1 deletion lib/que/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ defmodule Que.Worker do
end


defoverridable [on_success: 1, on_failure: 2]
def on_setup(_job) do
end


def on_teardown(_job) do
end


defoverridable [on_success: 1, on_failure: 2, on_setup: 1, on_teardown: 1]



Expand Down Expand Up @@ -232,4 +240,20 @@ defmodule Que.Worker do
"""
@callback on_failure(arguments :: term, error :: tuple) :: term




@doc """
Optional callback that is executed before the job is started.
"""
@callback on_setup(job :: term) :: term




@doc """
Optional callback that is executed after the job finishes,
both on success and failure.
"""
@callback on_teardown(job :: term) :: term
end
49 changes: 46 additions & 3 deletions test/que/job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Que.Test.Job do
alias Que.Test.Meta.TestWorker
alias Que.Test.Meta.SuccessWorker
alias Que.Test.Meta.FailureWorker
alias Que.Test.Meta.SetupAndTeardownWorker


test "#new builds a new Job struct with defaults" do
Expand Down Expand Up @@ -56,14 +57,56 @@ defmodule Que.Test.Job do
refute job.pid == nil
refute job.ref == nil

Helpers.wait
Helpers.wait_for_children
end)

assert capture =~ ~r/Starting/
assert capture =~ ~r/perform: nil/
end


test "#perform triggers the worker's on_setup and on_teardown callbacks on success" do
capture = Helpers.capture_log(fn ->
job =
SetupAndTeardownWorker
|> Job.new
|> Job.perform
|> Job.handle_success

assert job.status == :completed
assert job.pid == nil
assert job.ref == nil

Helpers.wait_for_children
end)

assert capture =~ ~r/Completed/
assert capture =~ ~r/on_setup: %Que.Job/
assert capture =~ ~r/on_teardown: %Que.Job/
end


test "#perform triggers the worker's on_setup and on_teardown callbacks on failure" do
capture = Helpers.capture_log(fn ->
job =
SetupAndTeardownWorker
|> Job.new
|> Job.perform
|> Job.handle_failure("some error")

assert job.status == :failed
assert job.pid == nil
assert job.ref == nil

Helpers.wait_for_children
end)

assert capture =~ ~r/Failed/
assert capture =~ ~r/on_setup: %Que.Job/
assert capture =~ ~r/on_teardown: %Que.Job/
end


test "#handle_success works as expected" do
capture = Helpers.capture_log(fn ->
job =
Expand All @@ -75,7 +118,7 @@ defmodule Que.Test.Job do
assert job.pid == nil
assert job.ref == nil

Helpers.wait
Helpers.wait_for_children
end)

assert capture =~ ~r/Completed/
Expand All @@ -94,7 +137,7 @@ defmodule Que.Test.Job do
assert job.pid == nil
assert job.ref == nil

Helpers.wait
Helpers.wait_for_children
end)

assert capture =~ ~r/Failed/
Expand Down
19 changes: 19 additions & 0 deletions test/support.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ defmodule Que.Test.Meta do
end


defmodule SetupAndTeardownWorker do
use Que.Worker

def perform(args), do: Logger.debug("#{__MODULE__} - perform: #{inspect(args)}")
def on_setup(job), do: Logger.debug("#{__MODULE__} - on_setup: #{inspect(job)}")
def on_teardown(job), do: Logger.debug("#{__MODULE__} - on_teardown: #{inspect(job)}")
end




# Helper Module for Tests
Expand All @@ -68,6 +77,16 @@ defmodule Que.Test.Meta do
:timer.sleep(ms)
end

def wait_for_children do
Task.Supervisor.children(Que.TaskSupervisor)
|> Enum.map(&Process.monitor/1)
|> Enum.each(fn ref ->
receive do
{:DOWN, ^ref, _, _, _} -> nil
end
end)
end

# Captures IO output
def capture_io(fun) do
ExUnit.CaptureIO.capture_io(fun)
Expand Down