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

Adds Crawly.Engine.stop_all_spiders/0 #106

Closed
wants to merge 8 commits into from
Closed
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
29 changes: 23 additions & 6 deletions lib/crawly/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ defmodule Crawly.Engine do
known_spiders: [module()]
}
@type started_spiders() :: %{optional(module()) => identifier()}
@type list_spiders() :: [
%{name: module(), status: :stopped | :started, pid: identifier()}
]

@type spider_info() :: %{
name: module(),
Expand Down Expand Up @@ -55,12 +58,16 @@ defmodule Crawly.Engine do
result:
:ok | {:error, :spider_not_running} | {:error, :spider_not_found}
def stop_spider(spider_name, reason \\ :ignore) do
case Crawly.Utils.get_settings(:on_spider_closed_callback, spider_name) do
nil -> :ignore
fun -> apply(fun, [reason])
end
GenServer.call(__MODULE__, {:stop_spider, spider_name, reason})
end

GenServer.call(__MODULE__, {:stop_spider, spider_name})
@spec stop_all_spiders(atom()) :: :ok
@doc "Stops all spiders, regardless of their current state. Runs :on_spider_closed_callback if available only if spider was running."
def stop_all_spiders(reason \\ :ignore) do
Crawly.Utils.list_spiders()
|> Enum.each(fn name -> stop_spider(name, reason) end)

:ok
end

@spec list_known_spiders() :: [spider_info()]
Expand Down Expand Up @@ -154,15 +161,25 @@ defmodule Crawly.Engine do
{:reply, msg, %Crawly.Engine{state | started_spiders: new_started_spiders}}
end

def handle_call({:stop_spider, spider_name}, _form, state) do
def handle_call({:stop_spider, spider_name, reason}, _form, state) do
{msg, new_started_spiders} =
case Map.pop(state.started_spiders, spider_name) do
{nil, _} ->
{{:error, :spider_not_running}, state.started_spiders}

{{pid, _crawl_id}, new_started_spiders} ->
# stop the spider
Crawly.EngineSup.stop_spider(pid)

# run the callback
case Crawly.Utils.get_settings(
:on_spider_closed_callback,
spider_name
) do
nil -> :ignore
fun -> apply(fun, [reason])
end

{:ok, new_started_spiders}
end

Expand Down
11 changes: 11 additions & 0 deletions test/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ defmodule APITest do

@opts Crawly.API.Router.init([])

setup do
kill_spiders()
on_exit(&kill_spiders/0)
end

defp kill_spiders do
Crawly.Engine.running_spiders()
|> Map.keys()
|> Enum.each(&Crawly.Engine.stop_spider/1)
end

test "returns welcome" do
conn =
:get
Expand Down
21 changes: 16 additions & 5 deletions test/engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ defmodule EngineTest do
Crawly.Engine.list_known_spiders()
|> Enum.find(fn s -> s.name == TestSpider end)

assert :started = started_status.status
assert started_status.status == :started
assert started_status.pid
end

# stop spider
Crawly.Engine.stop_spider(TestSpider)
spiders = Crawly.Engine.list_known_spiders()
assert Enum.all?(spiders, fn s -> s.status == :stopped end)
test "stop_all_spiders/0 stops all spiders" do
Crawly.Engine.list_known_spiders()
|> Enum.each(fn %{name: name} ->
Crawly.Engine.start_spider(name)
end)

Crawly.Engine.stop_all_spiders()

statuses = Crawly.Engine.list_known_spiders()

assert Enum.all?(statuses, fn status ->
assert status.status == :stopped
assert status.pid == nil
end)
end
end