Skip to content

Commit

Permalink
Handle shutdown process (#45)
Browse files Browse the repository at this point in the history
* Test: workers stop before the pool

* Stop supervisor on pool terminating

* Destroy monitoring table on pool terminating

* Update CHANGELOG
  • Loading branch information
general-CbIC committed Jun 3, 2023
1 parent 7ef07ce commit a87a511
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix the shutdown process: stop workers before the pool ([issue](https://github.com/general-CbIC/poolex/issues/44)).

## [0.7.0] - 2023-04-13

### Added
Expand Down
8 changes: 8 additions & 0 deletions lib/poolex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,12 @@ defmodule Poolex do
{:noreply, %{state | waiting_callers_state: new_waiting_callers_state}}
end
end

@impl GenServer
def terminate(reason, %State{} = state) do
DynamicSupervisor.stop(state.supervisor, reason)
Monitoring.stop(state.monitor_id)

:ok
end
end
8 changes: 8 additions & 0 deletions lib/poolex/monitoring.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ defmodule Poolex.Monitoring do
{:ok, monitor_id}
end

@spec stop(monitor_id()) :: :ok
@doc false
def stop(monitor_id) do
:ets.delete(monitor_id)

:ok
end

@spec add(monitor_id(), pid(), kind_of_process()) :: :ok
@doc false
def add(monitor_id, process_pid, kind_of_process) do
Expand Down
24 changes: 24 additions & 0 deletions test/poolex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,30 @@ defmodule PoolexTest do
end
end

describe "shutdown process" do
test "workers stop before the pool", %{pool_name: pool_name} do
{:ok, pool_pid} =
Poolex.start_link(pool_id: pool_name, worker_module: SomeWorker, workers_count: 1)

state = Poolex.get_state(pool_name)

supervisor_pid = state.supervisor
worker_pid = Poolex.run!(pool_name, fn pid -> pid end)

pool_monitor_ref = Process.monitor(pool_pid)
supervisor_monitor_ref = Process.monitor(supervisor_pid)
worker_monitor_ref = Process.monitor(worker_pid)

GenServer.stop(pool_name)

{:messages, [message_1, message_2, message_3]} = Process.info(self(), :messages)

assert message_1 == {:DOWN, worker_monitor_ref, :process, worker_pid, :shutdown}
assert message_2 == {:DOWN, supervisor_monitor_ref, :process, supervisor_pid, :normal}
assert message_3 == {:DOWN, pool_monitor_ref, :process, pool_pid, :normal}
end
end

defp pool_name do
1..10
|> Enum.map(fn _ -> Enum.random(?a..?z) end)
Expand Down

0 comments on commit a87a511

Please sign in to comment.