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

Start TaskSupervisor by default #133

Merged
merged 1 commit into from
Apr 26, 2016
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
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ config :my_app, MyApp.Mailer,
end
```

3. Add the the `Bamboo.TaskSupervisor` as a child to your supervisor. This is necessary for `deliver_later` to work.

```elixir
# Usually in lib/my_app_name.ex
children = [
# This is where you add the supervisor that handles deliver_later calls
Bamboo.TaskSupervisorStrategy.child_spec
]
```

## Getting Started

Bamboo breaks email creation and email sending into two separate modules. This
Expand Down
2 changes: 2 additions & 0 deletions lib/bamboo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ defmodule Bamboo do

children = [
worker(Bamboo.SentEmail, []),
supervisor(Task.Supervisor, [[name: Bamboo.TaskSupervisorStrategy.supervisor_name]])
]

opts = [strategy: :one_for_one, name: Bamboo.Supervisor]
Supervisor.start_link(children, opts)
end
Expand Down
32 changes: 11 additions & 21 deletions lib/bamboo/strategies/task_supervisor_strategy.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
defmodule Bamboo.TaskSupervisorStrategy do
@behaviour Bamboo.DeliverLaterStrategy
@supervisor_name Bamboo.TaskSupervior

@moduledoc """
Default strategy. Sends an email in the background using Task.Supervisor

This is the default strategy when calling `deliver_later` because it is the
simplest to get started with. This strategy uses a Task.Supervisor to monitor
simplest to get started with. This strategy uses a `Task.Supervisor` to monitor
the delivery. Deliveries that fail will raise, but will not be retried, and
will not bring down the calling process.

To use this strategy, the `Bamboo.TaskSupervisor` must be added to your
supervisor. See the docs for `child_spec/0` or check out the installation
section of the README.

## Why use it?

Sending emails can often take time and may fail. If you are sending email
Expand All @@ -25,26 +20,21 @@ defmodule Bamboo.TaskSupervisorStrategy do

@doc false
def deliver_later(adapter, email, config) do
Task.Supervisor.start_child @supervisor_name, fn ->
Task.Supervisor.start_child supervisor_name, fn ->
adapter.deliver(email, config)
end
end

@doc """
Child spec for use in your supervisor

## Example
def supervisor_name do
Bamboo.TaskSupervisor
end

# Usually in lib/my_app_name.ex
children = [
# Add the supervisor that handles deliver_later calls
Bamboo.TaskSupervisorStrategy.child_spec
]
"""
@doc false
def child_spec do
Supervisor.Spec.supervisor(
Task.Supervisor,
[[name: @supervisor_name]]
)
raise """
the TaskSupervisorStrategy is now started automatically by the :bamboo application.

You can safely remove Bamboo.TaskSupervisorStrategy.child_spec
"""
end
end
11 changes: 4 additions & 7 deletions test/lib/bamboo/strategies/task_supervisor_strategy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ defmodule Bamboo.TaskSupervisorStrategyTest do
assert_receive :delivered
end

test "child_spec" do
spec = Bamboo.TaskSupervisorStrategy.child_spec

assert spec == Supervisor.Spec.supervisor(
Task.Supervisor,
[[name: Bamboo.TaskSupervior]]
)
test "child_spec raises error about removal" do
assert_raise RuntimeError, ~r/remove Bamboo.TaskSupervisorStrategy.child_spec/, fn ->
Bamboo.TaskSupervisorStrategy.child_spec
end
end
end
5 changes: 0 additions & 5 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
ExUnit.start()

Supervisor.start_child(
Bamboo.Supervisor,
Bamboo.TaskSupervisorStrategy.child_spec
)

Application.ensure_all_started(:phoenix)
Application.ensure_all_started(:cowboy)
Application.ensure_all_started(:ex_machina)