@@ -5,8 +5,7 @@ defmodule Realtime.Tenants do
5
5
6
6
require Logger
7
7
alias Realtime.Repo.Replica
8
- alias Realtime.Api
9
- alias Realtime . { Api.Tenant , PostgresCdc , UsersCounter }
8
+ alias Realtime . { Api.Tenant , UsersCounter , Tenants }
10
9
11
10
@ doc """
12
11
Gets a list of connected tenant `external_id` strings in the cluster or a node.
@@ -23,21 +22,20 @@ defmodule Realtime.Tenants do
23
22
end
24
23
25
24
@ doc """
26
- Gets the database connection pid of the SubscriptionManager `manager` and the
27
- Postgrex connection pool `subs_pool`.
28
-
29
- When this function returns `:wait` the database connection tree is starting and the
30
- caller should not try to start the database connection tree again.
25
+ Gets the database connection pid managed by the Tenants.Connect process.
31
26
32
27
## Examples
33
28
34
- iex> get_manager_conn(Extensions.PostgresCdcRls, "not_started_external_id" )
35
- {:error, nil }
29
+ iex> get_health_conn(%Tenant{external_id: "not_found_tenant"} )
30
+ {:error, :tenant_database_unavailable }
36
31
"""
37
32
38
- @ spec get_manager_conn ( :atom , % Tenant { } ) :: { :error , :wait | nil } | { :ok , pid ( ) , pid ( ) }
39
- def get_manager_conn ( module , % Tenant { external_id: external_id } ) do
40
- module . get_manager_conn ( external_id )
33
+ @ spec get_health_conn ( % Tenant { } ) :: { :error , term ( ) } | { :ok , pid ( ) }
34
+ def get_health_conn ( % Tenant { external_id: external_id } ) do
35
+ case Tenants.Connect . get_status ( external_id ) do
36
+ { :ok , conn } -> { :ok , conn }
37
+ { :error , reason } -> { :error , reason }
38
+ end
41
39
end
42
40
43
41
@ doc """
@@ -60,19 +58,15 @@ defmodule Realtime.Tenants do
60
58
| % { connected_cluster: pos_integer , db_connected: false , healthy: false } }
61
59
| { :ok , % { connected_cluster: non_neg_integer , db_connected: true , healthy: true } }
62
60
def health_check ( external_id ) when is_binary ( external_id ) do
63
- with % Tenant { } = tenant <- Api . get_tenant_by_external_id ( external_id ) ,
64
- { :ok , module } <- PostgresCdc . driver ( tenant . postgres_cdc_default ) ,
65
- { :error , _ } <- get_manager_conn ( module , tenant ) ,
61
+ with % Tenant { } = tenant <- Tenants.Cache . get_tenant_by_external_id ( external_id ) ,
62
+ { :error , _ } <- get_health_conn ( tenant ) ,
66
63
connected_cluster when connected_cluster > 0 <- UsersCounter . tenant_users ( external_id ) do
67
64
{ :error , % { healthy: false , db_connected: false , connected_cluster: connected_cluster } }
68
65
else
69
66
nil ->
70
67
{ :error , :tenant_not_found }
71
68
72
- { :error , _mod } ->
73
- { :error , "Bad value for tenant field `postgres_cdc_default`" }
74
-
75
- { :ok , _manager , _subs_pool } ->
69
+ { :ok , _health_conn } ->
76
70
connected_cluster = UsersCounter . tenant_users ( external_id )
77
71
78
72
{ :ok , % { healthy: true , db_connected: true , connected_cluster: connected_cluster } }
0 commit comments