Broaden HTTPoison status code inspection

Rather than enforcing a 200 status code, the instance query is deemed a
success if the status code is <400. Various services return 200-399
status codes that don't necessarily indicate an error, but may have to
do with how the instance was configured.
This commit is contained in:
Ben Busby 2021-12-28 12:33:04 -07:00
parent 71febe3d3b
commit 1fb9051ae1
No known key found for this signature in database
GPG key ID: B9B7231E01D924A1
2 changed files with 15 additions and 8 deletions

View file

@ -37,8 +37,9 @@ For example:
The app runs with an internally scheduled cron task that queries all instances The app runs with an internally scheduled cron task that queries all instances
for services defined in [services.json](services.json) every 5 minutes. For for services defined in [services.json](services.json) every 5 minutes. For
each instance, as long as the instance takes <5 seconds to respond and returns each instance, as long as the instance takes <5 seconds to respond and returns
a 200 status code, the instance is added to a list of available instances for a successful response code, the instance is added to a list of available
that particular service. If not, it is discarded until the next update period. instances for that particular service. If not, it is discarded until the next
update period.
Farside's routing is very minimal, with only the following routes: Farside's routing is very minimal, with only the following routes:

View file

@ -5,6 +5,8 @@ defmodule Farside.Instances do
@service_prefix Application.fetch_env!(:farside, :service_prefix) @service_prefix Application.fetch_env!(:farside, :service_prefix)
@headers Application.fetch_env!(:farside, :headers) @headers Application.fetch_env!(:farside, :headers)
@queries Application.fetch_env!(:farside, :queries) @queries Application.fetch_env!(:farside, :queries)
@debug_header "======== "
@debug_spacer " "
def sync() do def sync() do
File.rename(@update_file, "#{@update_file}-prev") File.rename(@update_file, "#{@update_file}-prev")
@ -24,12 +26,16 @@ defmodule Farside.Instances do
:good :good
true -> true ->
case HTTPoison.get(url, @headers) do HTTPoison.get(url, @headers)
{:ok, %HTTPoison.Response{status_code: 200}} -> |> then(&elem(&1, 1))
# TODO: Add validation of results, not just status code |> Map.get(:status_code)
|> case do
n when n < 400 ->
IO.puts("#{@debug_spacer}✓ [#{n}]")
:good :good
_ -> n ->
IO.puts("#{@debug_spacer}x [#{(n && n) || "error"}]")
:bad :bad
end end
end end
@ -41,7 +47,7 @@ defmodule Farside.Instances do
# Loop through all instances and check each for availability # Loop through all instances and check each for availability
for service <- json do for service <- json do
IO.puts("======== " <> service.type) IO.puts("#{@debug_header}#{service.type}")
result = result =
Enum.filter(service.instances, fn instance_url -> Enum.filter(service.instances, fn instance_url ->
@ -52,7 +58,7 @@ defmodule Farside.Instances do
query: Enum.random(@queries) query: Enum.random(@queries)
) )
IO.puts(" " <> request_url) IO.puts("#{@debug_spacer}#{request_url}")
request(request_url) == :good request(request_url) == :good
end) end)