From 1fb9051ae12be292519821623dc3eef0e3541c24 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 28 Dec 2021 12:33:04 -0700 Subject: [PATCH] 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. --- README.md | 5 +++-- lib/farside/instances.ex | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c8db668..43443a8 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ For example: 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 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 -that particular service. If not, it is discarded until the next update period. +a successful response code, the instance is added to a list of available +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: diff --git a/lib/farside/instances.ex b/lib/farside/instances.ex index 0e1b9b4..7a07827 100644 --- a/lib/farside/instances.ex +++ b/lib/farside/instances.ex @@ -5,6 +5,8 @@ defmodule Farside.Instances do @service_prefix Application.fetch_env!(:farside, :service_prefix) @headers Application.fetch_env!(:farside, :headers) @queries Application.fetch_env!(:farside, :queries) + @debug_header "======== " + @debug_spacer " " def sync() do File.rename(@update_file, "#{@update_file}-prev") @@ -24,12 +26,16 @@ defmodule Farside.Instances do :good true -> - case HTTPoison.get(url, @headers) do - {:ok, %HTTPoison.Response{status_code: 200}} -> - # TODO: Add validation of results, not just status code + HTTPoison.get(url, @headers) + |> then(&elem(&1, 1)) + |> Map.get(:status_code) + |> case do + n when n < 400 -> + IO.puts("#{@debug_spacer}✓ [#{n}]") :good - _ -> + n -> + IO.puts("#{@debug_spacer}x [#{(n && n) || "error"}]") :bad end end @@ -41,7 +47,7 @@ defmodule Farside.Instances do # Loop through all instances and check each for availability for service <- json do - IO.puts("======== " <> service.type) + IO.puts("#{@debug_header}#{service.type}") result = Enum.filter(service.instances, fn instance_url -> @@ -52,7 +58,7 @@ defmodule Farside.Instances do query: Enum.random(@queries) ) - IO.puts(" " <> request_url) + IO.puts("#{@debug_spacer}#{request_url}") request(request_url) == :good end)