Skip to content

Commit

Permalink
added stop all spiders
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziinc committed May 18, 2020
1 parent 1f6c54b commit f1d5751
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/crawly/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ defmodule Crawly.Engine do
GenServer.call(__MODULE__, {:stop_spider, spider_name})
end

@spec stop_all_spiders() :: :ok
@doc "Stops all spiders, regardless of their current state. Runs :on_spider_closed_callback if available"
def stop_all_spiders() do
Crawly.Utils.list_spiders()
|> Enum.each(fn name ->
case Crawly.Utils.get_settings(:on_spider_closed_callback, name) do
nil -> :ignore
fun -> apply(fun, [:stop_all])
end

GenServer.call(__MODULE__, {:stop_spider, name})
end)
end

@spec list_spiders() :: list_spiders()
def list_spiders() do
GenServer.call(__MODULE__, :list_spiders)
Expand Down
16 changes: 16 additions & 0 deletions test/engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ defmodule EngineTest do
assert started_status.state == :started
assert started_status.pid
end

test "stop_all_spiders/0 stops all spiders" do
Crawly.Engine.list_spiders()
|> Enum.each(fn %{name: name} ->
Crawly.Engine.start_spider(name)
end)

Crawly.Engine.stop_all_spiders()

statuses = Crawly.Engine.list_spiders()

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

0 comments on commit f1d5751

Please sign in to comment.