2021-11-08 17:08:19 -07:00
|
|
|
defmodule Farside do
|
|
|
|
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
2021-11-10 12:19:37 -07:00
|
|
|
@fallback_suffix Application.fetch_env!(:farside, :fallback_suffix)
|
|
|
|
@previous_suffix Application.fetch_env!(:farside, :previous_suffix)
|
2021-11-08 17:08:19 -07:00
|
|
|
|
|
|
|
def get_services_map do
|
2021-11-10 10:47:04 -07:00
|
|
|
{:ok, service_list} = Redix.command(:redix, ["KEYS", "#{@service_prefix}*"])
|
2021-11-08 17:08:19 -07:00
|
|
|
|
|
|
|
# Match service name to list of available instances
|
|
|
|
Enum.reduce(service_list, %{}, fn service, acc ->
|
|
|
|
{:ok, instance_list} =
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
|
|
|
["LRANGE", service, "0", "-1"]
|
|
|
|
)
|
|
|
|
|
|
|
|
Map.put(
|
|
|
|
acc,
|
|
|
|
String.replace_prefix(
|
|
|
|
service,
|
|
|
|
@service_prefix,
|
|
|
|
""
|
|
|
|
),
|
|
|
|
instance_list
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-11-10 11:50:19 -07:00
|
|
|
def pick_instance(service) do
|
|
|
|
{:ok, instances} =
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
|
|
|
[
|
|
|
|
"LRANGE",
|
|
|
|
"#{@service_prefix}#{service}",
|
|
|
|
"0",
|
|
|
|
"-1"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Either pick a random available instance,
|
|
|
|
# or fall back to the default one
|
|
|
|
instance =
|
|
|
|
if Enum.count(instances) > 0 do
|
2021-11-10 12:19:37 -07:00
|
|
|
if Enum.count(instances) == 1 do
|
|
|
|
# If there's only one instance, just return that one...
|
|
|
|
List.first(instances)
|
|
|
|
else
|
|
|
|
# ...otherwise pick a random one from the list, ensuring
|
|
|
|
# that the same instance is never picked twice in a row.
|
|
|
|
{:ok, previous} =
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
|
|
|
["GET", "#{service}#{@previous_suffix}"]
|
|
|
|
)
|
|
|
|
|
|
|
|
instance =
|
|
|
|
Enum.filter(instances, &(&1 != previous))
|
|
|
|
|> Enum.random()
|
|
|
|
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
|
|
|
["SET", "#{service}#{@previous_suffix}", instance]
|
|
|
|
)
|
|
|
|
|
|
|
|
instance
|
|
|
|
end
|
2021-11-10 11:50:19 -07:00
|
|
|
else
|
|
|
|
{:ok, result} =
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
2021-11-10 12:19:37 -07:00
|
|
|
["GET", "#{service}#{@fallback_suffix}"]
|
2021-11-10 11:50:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
2021-11-08 17:08:19 -07:00
|
|
|
def get_last_updated do
|
|
|
|
{:ok, last_updated} =
|
|
|
|
Redix.command(
|
|
|
|
:redix,
|
|
|
|
["GET", "last_updated"]
|
|
|
|
)
|
|
|
|
|
|
|
|
last_updated
|
|
|
|
end
|
|
|
|
end
|