1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-06-08 02:16:36 +00:00

config option update_file now contains updated json of working instances after sync

This commit is contained in:
mithereal 2022-07-30 18:40:07 -07:00
parent a6e0c34abe
commit ca1e96470d
5 changed files with 22 additions and 9 deletions

View file

@ -35,7 +35,7 @@
<div id="child-div"> <div id="child-div">
<h1>Farside | <a href="https://github.com/benbusby/farside">View on GitHub</a></h1> <h1>Farside | <a href="https://github.com/benbusby/farside">View on GitHub</a></h1>
<hr> <hr>
<h3>Last synced <%= last_updated %> UTC</h2> <h3>Last synced <%= DateTime.truncate(last_updated, :second) %> UTC</h2>
<div> <div>
<ul> <ul>
<%= for {service, instance_list} <- services do %> <%= for {service, instance_list} <- services do %>

View file

@ -92,4 +92,17 @@ defmodule Farside do
instance instance
end end
end end
def save_results(file, data) do
{:ok, file} = File.open(file, [:append])
bin = :erlang.term_to_binary(data)
IO.binwrite(file, bin)
File.close(file)
end
def restore_term(file) do
{:ok,bin} = File.read(file)
:erlang.binary_to_term(bin)
end
end end

View file

@ -13,7 +13,7 @@ defmodule Farside.Application do
def start(_type, _args) do def start(_type, _args) do
port = port =
case Application.fetch_env!(:farside, :port) do case Application.fetch_env!(:farside, :port) do
nil -> System.get_env("PORT") nil -> System.get_env("PORT", "4001")
port -> port port -> port
end end

View file

@ -6,7 +6,7 @@ defmodule Farside.Instance do
alias Farside.Http alias Farside.Http
@registry_name :servers @registry_name :servers
@update_file Application.fetch_env!(:farside, :update_file) @update_file Application.fetch_env!(:farside, :update_file) <> ".json"
def child_spec(args) do def child_spec(args) do
%{ %{
@ -69,7 +69,9 @@ defmodule Farside.Instance do
:ets.insert(state.ref, {:data, service}) :ets.insert(state.ref, {:data, service})
File.write(@update_file, service.fallback) encoded = service |> Map.from_struct() |> Jason.encode!()
Farside.save_results(@update_file, encoded)
{:noreply, state} {:noreply, state}
end end

View file

@ -3,15 +3,13 @@ defmodule Farside.Instances do
def sync() do def sync() do
update_file = Application.fetch_env!(:farside, :update_file) update_file = Application.fetch_env!(:farside, :update_file)
update_json = update_file <> ".json"
File.rm("#{update_file}-prev") File.rename(update_json, "#{update_file}-#{to_string(DateTime.utc_now()) <> ".json"}")
File.rename(update_file, "#{update_file}-prev") File.write(update_json, "")
File.write(update_file, "")
LastUpdated.value(DateTime.utc_now()) LastUpdated.value(DateTime.utc_now())
Farside.Instance.Supervisor.update_children() Farside.Instance.Supervisor.update_children()
end end
end end