Skip to content

Commit

Permalink
adjusted on_spider_closed_callback execution position to be in the ge…
Browse files Browse the repository at this point in the history
…nserver call handler
  • Loading branch information
Ziinc committed Nov 2, 2020
1 parent a4108ea commit 48af237
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/crawly/engine.ex
Expand Up @@ -14,7 +14,7 @@ defmodule Crawly.Engine do
}
@type started_spiders() :: %{optional(module()) => identifier()}
@type list_spiders() :: [
%{name: module(), state: :stopped | :started, pid: identifier()}
%{name: module(), status: :stopped | :started, pid: identifier()}
]

@type spider_info() :: %{
Expand Down Expand Up @@ -58,26 +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})
GenServer.call(__MODULE__, {:stop_spider, spider_name, reason})
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
@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 ->
case Crawly.Utils.get_settings(:on_spider_closed_callback, name) do
nil -> :ignore
fun -> apply(fun, [:stop_all])
end
|> Enum.each(fn name -> stop_spider(name, reason) end)

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

@spec list_known_spiders() :: [spider_info()]
Expand Down Expand Up @@ -171,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

0 comments on commit 48af237

Please sign in to comment.