@@ -125,13 +125,13 @@ defmodule Realtime.Tenants.Connect do
125125 end
126126
127127 @ doc """
128- Returns the pid of the tenant Connection process
128+ Returns the pid of the tenant Connection process and db_conn pid
129129 """
130- @ spec whereis ( binary ( ) ) :: pid | nil
130+ @ spec whereis ( binary ( ) ) :: pid ( ) | nil
131131 def whereis ( tenant_id ) do
132132 case :syn . lookup ( __MODULE__ , tenant_id ) do
133- { pid , _ } -> pid
134- :undefined -> nil
133+ { pid , _ } when is_pid ( pid ) -> pid
134+ _ -> nil
135135 end
136136 end
137137
@@ -141,8 +141,12 @@ defmodule Realtime.Tenants.Connect do
141141 @ spec shutdown ( binary ( ) ) :: :ok | nil
142142 def shutdown ( tenant_id ) do
143143 case whereis ( tenant_id ) do
144- pid when is_pid ( pid ) -> GenServer . stop ( pid )
145- _ -> :ok
144+ pid when is_pid ( pid ) ->
145+ send ( pid , :shutdown_connect )
146+ :ok
147+
148+ _ ->
149+ :ok
146150 end
147151 end
148152
@@ -196,7 +200,7 @@ defmodule Realtime.Tenants.Connect do
196200 def handle_continue ( :run_migrations , state ) do
197201 % { tenant: tenant , db_conn_pid: db_conn_pid } = state
198202
199- with :ok <- Migrations . run_migrations ( tenant ) ,
203+ with res when res in [ :ok , :noop ] <- Migrations . run_migrations ( tenant ) ,
200204 :ok <- Migrations . create_partitions ( db_conn_pid ) do
201205 { :noreply , state , { :continue , :start_listen_and_replication } }
202206 else
@@ -273,42 +277,19 @@ defmodule Realtime.Tenants.Connect do
273277 { :noreply , % { state | connected_users_bucket: connected_users_bucket } }
274278 end
275279
276- def handle_info ( :shutdown , state ) do
277- % {
278- db_conn_pid: db_conn_pid ,
279- replication_connection_pid: replication_connection_pid ,
280- listen_pid: listen_pid
281- } = state
282-
280+ def handle_info ( :shutdown_no_connected_users , state ) do
283281 Logger . info ( "Tenant has no connected users, database connection will be terminated" )
284- :ok = GenServer . stop ( db_conn_pid , :normal , 500 )
285-
286- replication_connection_pid && Process . alive? ( replication_connection_pid ) &&
287- GenServer . stop ( replication_connection_pid , :normal , 500 )
288-
289- listen_pid && Process . alive? ( listen_pid ) &&
290- GenServer . stop ( listen_pid , :normal , 500 )
291-
292- { :stop , :normal , state }
282+ shutdown_connect_process ( state )
293283 end
294284
295285 def handle_info ( :suspend_tenant , state ) do
296- % {
297- db_conn_pid: db_conn_pid ,
298- replication_connection_pid: replication_connection_pid ,
299- listen_pid: listen_pid
300- } = state
301-
302286 Logger . warning ( "Tenant was suspended, database connection will be terminated" )
303- :ok = GenServer . stop ( db_conn_pid , :normal , 500 )
304-
305- replication_connection_pid && Process . alive? ( replication_connection_pid ) &&
306- GenServer . stop ( replication_connection_pid , :normal , 500 )
307-
308- listen_pid && Process . alive? ( listen_pid ) &&
309- GenServer . stop ( listen_pid , :normal , 500 )
287+ shutdown_connect_process ( state )
288+ end
310289
311- { :stop , :normal , state }
290+ def handle_info ( :shutdown_connect , state ) do
291+ Logger . warning ( "Shutdowning tenant connection" )
292+ shutdown_connect_process ( state )
312293 end
313294
314295 # Handle database connection termination
@@ -371,7 +352,7 @@ defmodule Realtime.Tenants.Connect do
371352 @ connected_users_bucket_shutdown ,
372353 check_connected_user_interval
373354 ) do
374- Process . send_after ( self ( ) , :shutdown , check_connected_user_interval )
355+ Process . send_after ( self ( ) , :shutdown_no_connected_users , check_connected_user_interval )
375356 end
376357
377358 defp send_connected_user_check_message ( connected_users_bucket , check_connected_user_interval ) do
@@ -381,4 +362,22 @@ defmodule Realtime.Tenants.Connect do
381362
382363 defp tenant_suspended? ( % Tenant { suspend: true } ) , do: { :error , :tenant_suspended }
383364 defp tenant_suspended? ( _ ) , do: :ok
365+
366+ defp shutdown_connect_process ( state ) do
367+ % {
368+ db_conn_pid: db_conn_pid ,
369+ replication_connection_pid: replication_connection_pid ,
370+ listen_pid: listen_pid
371+ } = state
372+
373+ :ok = GenServer . stop ( db_conn_pid , :shutdown , 500 )
374+
375+ replication_connection_pid && Process . alive? ( replication_connection_pid ) &&
376+ GenServer . stop ( replication_connection_pid , :normal , 500 )
377+
378+ listen_pid && Process . alive? ( listen_pid ) &&
379+ GenServer . stop ( listen_pid , :normal , 500 )
380+
381+ { :stop , :normal , state }
382+ end
384383end
0 commit comments