@@ -19,7 +19,6 @@ defmodule Realtime.Tenants.Connect do
19
19
alias Realtime.Tenants.Connect.GetTenant
20
20
alias Realtime.Tenants.Connect.Piper
21
21
alias Realtime.Tenants.Connect.RegisterProcess
22
- alias Realtime.Tenants.Connect.StartCounters
23
22
alias Realtime.Tenants.Migrations
24
23
alias Realtime.Tenants.ReplicationConnection
25
24
alias Realtime.UsersCounter
@@ -83,14 +82,13 @@ defmodule Realtime.Tenants.Connect do
83
82
| { :error , :tenant_database_connection_initializing }
84
83
def get_status ( tenant_id ) do
85
84
case :syn . lookup ( __MODULE__ , tenant_id ) do
86
- { _pid , % { conn: nil } } ->
87
- wait_for_connection ( tenant_id )
85
+ { pid , % { conn: nil } } ->
86
+ wait_for_connection ( pid , tenant_id )
88
87
89
88
{ _ , % { conn: conn } } ->
90
89
{ :ok , conn }
91
90
92
91
:undefined ->
93
- Logger . warning ( "Connection process starting up" )
94
92
{ :error , :tenant_database_connection_initializing }
95
93
96
94
error ->
@@ -101,7 +99,7 @@ defmodule Realtime.Tenants.Connect do
101
99
102
100
def syn_topic ( tenant_id ) , do: "connect:#{ tenant_id } "
103
101
104
- defp wait_for_connection ( tenant_id ) do
102
+ defp wait_for_connection ( pid , tenant_id ) do
105
103
RealtimeWeb.Endpoint . subscribe ( syn_topic ( tenant_id ) )
106
104
107
105
# We do a lookup after subscribing because we could've missed a message while subscribing
@@ -112,9 +110,18 @@ defmodule Realtime.Tenants.Connect do
112
110
_ ->
113
111
# Wait for up to 5 seconds for the ready event
114
112
receive do
115
- % { event: "ready" , payload: % { conn: conn } } -> { :ok , conn }
113
+ % { event: "ready" , payload: % { pid: ^ pid , conn: conn } } ->
114
+ { :ok , conn }
115
+
116
+ % { event: "connect_down" , payload: % { pid: ^ pid , reason: { :shutdown , :tenant_db_too_many_connections } } } ->
117
+ { :error , :tenant_db_too_many_connections }
118
+
119
+ % { event: "connect_down" , payload: % { pid: ^ pid , reason: _reason } } ->
120
+ metadata = [ external_id: tenant_id , project: tenant_id ]
121
+ log_error ( "UnableToConnectToTenantDatabase" , "Unable to connect to tenant database" , metadata )
122
+ { :error , :tenant_database_unavailable }
116
123
after
117
- 5_000 -> { :error , :initializing }
124
+ 15_000 -> { :error , :initializing }
118
125
end
119
126
end
120
127
after
@@ -139,16 +146,6 @@ defmodule Realtime.Tenants.Connect do
139
146
{ :error , { :already_started , _ } } ->
140
147
get_status ( tenant_id )
141
148
142
- { :error , { :shutdown , :tenant_db_too_many_connections } } ->
143
- { :error , :tenant_db_too_many_connections }
144
-
145
- { :error , { :shutdown , :tenant_not_found } } ->
146
- { :error , :tenant_not_found }
147
-
148
- { :error , :shutdown } ->
149
- log_error ( "UnableToConnectToTenantDatabase" , "Unable to connect to tenant database" , metadata )
150
- { :error , :tenant_database_unavailable }
151
-
152
149
{ :error , error } ->
153
150
log_error ( "UnableToConnectToTenantDatabase" , error , metadata )
154
151
{ :error , :tenant_database_unavailable }
@@ -209,30 +206,33 @@ defmodule Realtime.Tenants.Connect do
209
206
def init ( % { tenant_id: tenant_id } = state ) do
210
207
Logger . metadata ( external_id: tenant_id , project: tenant_id )
211
208
209
+ { :ok , state , { :continue , :db_connect } }
210
+ end
211
+
212
+ @ impl true
213
+ def handle_continue ( :db_connect , state ) do
212
214
pipes = [
213
215
GetTenant ,
214
216
CheckConnection ,
215
- StartCounters ,
216
217
RegisterProcess
217
218
]
218
219
219
220
case Piper . run ( pipes , state ) do
220
221
{ :ok , acc } ->
221
- { :ok , acc , { :continue , :run_migrations } }
222
+ { :noreply , acc , { :continue , :run_migrations } }
222
223
223
224
{ :error , :tenant_not_found } ->
224
- { :stop , { :shutdown , :tenant_not_found } }
225
+ { :stop , { :shutdown , :tenant_not_found } , state }
225
226
226
227
{ :error , :tenant_db_too_many_connections } ->
227
- { :stop , { :shutdown , :tenant_db_too_many_connections } }
228
+ { :stop , { :shutdown , :tenant_db_too_many_connections } , state }
228
229
229
230
{ :error , error } ->
230
231
log_error ( "UnableToConnectToTenantDatabase" , error )
231
- { :stop , :shutdown }
232
+ { :stop , :shutdown , state }
232
233
end
233
234
end
234
235
235
- @ impl true
236
236
def handle_continue ( :run_migrations , state ) do
237
237
% { tenant: tenant , db_conn_pid: db_conn_pid } = state
238
238
Logger . warning ( "Tenant #{ tenant . external_id } is initializing: #{ inspect ( node ( ) ) } " )
@@ -375,6 +375,7 @@ defmodule Realtime.Tenants.Connect do
375
375
376
376
## Private functions
377
377
defp call_external_node ( tenant_id , opts ) do
378
+ Logger . warning ( "Connection process starting up" )
378
379
rpc_timeout = Keyword . get ( opts , :rpc_timeout , @ rpc_timeout_default )
379
380
380
381
with tenant <- Tenants.Cache . get_tenant_by_external_id ( tenant_id ) ,
0 commit comments