1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-06-08 10:26:36 +00:00
This commit is contained in:
mithereal 2022-07-30 15:08:54 -07:00
parent d69b859910
commit a76046a41b
9 changed files with 85 additions and 95 deletions

View file

@ -1,6 +1,4 @@
defmodule Farside.Application do
# @farside_port Application.fetch_env!(:farside, :port)
# @redis_conn Application.fetch_env!(:farside, :redis_conn)
@moduledoc false
use Application
@ -9,6 +7,7 @@ defmodule Farside.Application do
alias Farside.LastUpdated
alias Farside.Sync
alias Farside.Http
@impl true
def start(_type, _args) do
@ -48,11 +47,21 @@ defmodule Farside.Application do
end
def load(response) do
services_json = Application.fetch_env!(:farside, :services_json)
services_json_data = Application.fetch_env!(:farside, :services_json_data)
queries = Application.fetch_env!(:farside, :queries)
{:ok, file} = File.read(services_json)
{:ok, json} = Jason.decode(file)
reply =
case String.length(services_json_data) < 10 do
true ->
file = Application.fetch_env!(:farside, :services_json)
{:ok, data} = File.read(file)
data
false ->
services_json_data
end
{:ok, json} = Jason.decode(reply)
for service_json <- json do
service_atom =
@ -60,48 +69,9 @@ defmodule Farside.Application do
{String.to_existing_atom(key), val}
end
service = struct(%Service{}, service_atom)
test_urls =
Enum.map(service.instances, fn x ->
test_url =
x <>
EEx.eval_string(
service.test_url,
query: Enum.random(queries)
)
{test_url, x}
end)
tasks =
for {test_url, instance} <- test_urls do
Task.async(fn ->
reply = Farside.Http.request(test_url, service.type)
{test_url, reply, instance}
end)
end
tasks_with_results = Task.yield_many(tasks, 5000)
instances =
Enum.map(tasks_with_results, fn {task, res} ->
# Shut down the tasks that did not reply nor exit
res || Task.shutdown(task, :brutal_kill)
end)
|> Enum.reject(fn x -> x == nil end)
|> Enum.filter(fn {_, data} ->
{_test_url, value, _instance} = data
value == :good
end)
|> Enum.map(fn {_, data} ->
{_test_url, _value, instance} = data
instance
end)
service = %{service | instances: instances}
Farside.Instance.Supervisor.start(service)
struct(%Service{}, service_atom)
|> Http.fetch_instances()
|> Farside.Instance.Supervisor.start()
end
LastUpdated.value(DateTime.utc_now())

View file

@ -2,6 +2,7 @@ defmodule Farside.Http do
require Logger
@headers Application.fetch_env!(:farside, :headers)
@queries Application.fetch_env!(:farside, :queries)
def request(url) do
cond do
@ -44,4 +45,37 @@ defmodule Farside.Http do
end
end
end
def fetch_instances(service) do
instances =
Enum.map(service.instances, fn instance ->
test_url =
instance <>
EEx.eval_string(
service.test_url,
query: Enum.random(@queries)
)
Task.async(fn ->
reply = request(test_url, service.type)
{test_url, reply, instance}
end)
end)
|> Task.yield_many(5000)
|> Enum.map(fn {task, res} ->
# Shut down the tasks that did not reply nor exit
res || Task.shutdown(task, :brutal_kill)
end)
|> Enum.reject(fn x -> x == nil end)
|> Enum.filter(fn {_, data} ->
{_test_url, value, _instance} = data
value == :good
end)
|> Enum.map(fn {_, data} ->
{_test_url, _value, instance} = data
instance
end)
%{service | instances: instances}
end
end

View file

@ -3,7 +3,10 @@ defmodule Farside.Instance do
require Logger
alias Farside.Http
@registry_name :servers
@update_file Application.fetch_env!(:farside, :update_file)
def child_spec(args) do
%{
@ -60,54 +63,13 @@ defmodule Farside.Instance do
{_, service} = List.first(service)
queries = Application.fetch_env!(:farside, :queries)
test_urls =
Enum.map(service.instances, fn x ->
test_url =
x <>
EEx.eval_string(
service.test_url,
query: Enum.random(queries)
)
{test_url, x}
end)
tasks =
for {test_url, instance} <- test_urls do
Task.async(fn ->
reply = Farside.Http.request(test_url, service.type)
{test_url, reply, instance}
end)
end
tasks_with_results = Task.yield_many(tasks, 5000)
instances =
Enum.map(tasks_with_results, fn {task, res} ->
# Shut down the tasks that did not reply nor exit
res || Task.shutdown(task, :brutal_kill)
end)
|> Enum.reject(fn x -> x == nil end)
|> Enum.filter(fn {_, data} ->
{_test_url, value, _instance} = data
value == :good
end)
|> Enum.map(fn {_, data} ->
{_test_url, _value, instance} = data
instance
end)
values = %{service | instances: instances}
service = Http.fetch_instances(service)
:ets.delete_all_objects(String.to_atom(state.type))
:ets.insert(state.ref, {:data, values})
:ets.insert(state.ref, {:data, service})
update_file = Application.fetch_env!(:farside, :update_file)
File.write(update_file, values.fallback)
File.write(@update_file, service.fallback)
{:noreply, state}
end