farmbot_os/farmbot_ext/lib/farmbot_ext/api/dirty_worker.ex

122 lines
3.3 KiB
Elixir
Raw Normal View History

2019-03-05 11:38:25 -07:00
defmodule FarmbotExt.API.DirtyWorker do
@moduledoc "Handles uploading/downloading of data from the API."
2019-03-05 11:38:25 -07:00
alias FarmbotCore.Asset.{Private, Repo}
alias FarmbotExt.{API, API.DirtyWorker}
import API.View, only: [render: 2]
require Logger
use GenServer
2020-05-15 17:24:57 -06:00
@timeout 1500
# these resources can't be accessed by `id`.
@singular [
FarmbotCore.Asset.Device,
FarmbotCore.Asset.FirmwareConfig,
FarmbotCore.Asset.FbosConfig
]
@doc false
def child_spec(module) when is_atom(module) do
%{
2019-03-05 11:38:25 -07:00
id: {DirtyWorker, module},
start: {__MODULE__, :start_link, [[module: module, timeout: @timeout]]},
type: :worker,
restart: :permanent,
shutdown: 500
}
end
@doc "Start an instance of a DirtyWorker"
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
@impl GenServer
def init(args) do
module = Keyword.fetch!(args, :module)
2020-05-15 17:24:57 -06:00
Process.send_after(self(), :do_work, 100)
{:ok, %{module: module}}
end
@impl GenServer
2020-05-15 17:24:57 -06:00
def handle_info(:do_work, %{module: module} = state) do
(Private.list_dirty(module) ++ Private.list_local(module))
|> Enum.uniq()
|> Enum.map(fn dirty -> work(dirty, module) end)
2020-05-15 17:24:57 -06:00
Process.send_after(self(), :do_work, @timeout)
{:noreply, state}
end
2020-05-15 17:24:57 -06:00
def work(dirty, module) do
case http_request(dirty, module) do
# Valid data
{:ok, %{status: s, body: body}} when s > 199 and s < 300 ->
2020-05-15 17:24:57 -06:00
dirty |> module.changeset(body) |> handle_changeset(module)
# Invalid data
{:ok, %{status: s, body: %{} = body}} when s > 399 and s < 500 ->
changeset = module.changeset(dirty)
Enum.reduce(body, changeset, fn {key, val}, changeset ->
Ecto.Changeset.add_error(changeset, key, val)
end)
2020-05-15 17:24:57 -06:00
|> handle_changeset(module)
# Invalid data, but the API didn't say why
{:ok, %{status: s, body: _body}} when s > 399 and s < 500 ->
module.changeset(dirty)
|> Map.put(:valid?, false)
2020-05-15 17:24:57 -06:00
|> handle_changeset(module)
# HTTP Error. (500, network error, timeout etc.)
2019-03-25 10:41:58 -06:00
error ->
2020-05-14 13:31:46 -06:00
Logger.error(
2020-05-15 17:24:57 -06:00
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP Error: #{module} #{
2019-04-18 10:32:34 -06:00
inspect(error)
}"
)
2020-05-15 17:24:57 -06:00
module
end
end
# If the changeset was valid, update the record.
2020-05-15 17:24:57 -06:00
def handle_changeset(%{valid?: true} = changeset, _module) do
Private.mark_clean!(Repo.update!(changeset))
:ok
end
2020-05-15 17:24:57 -06:00
def handle_changeset(%{valid?: false, data: data} = changeset, module) do
message =
2019-04-18 10:32:34 -06:00
Enum.map(changeset.errors, fn
{key, {msg, _meta}} when is_binary(key) -> "\t#{key}: #{msg}"
{key, msg} when is_binary(key) -> "\t#{key}: #{msg}"
end)
|> Enum.join("\n")
2020-05-15 17:24:57 -06:00
Logger.error("Failed to sync: #{module} \n #{message}")
_ = Repo.delete!(data)
2020-05-15 17:24:57 -06:00
:ok
end
2020-05-15 17:24:57 -06:00
defp http_request(%{id: nil} = dirty, module) do
path = module.path()
data = render(module, dirty)
API.post(API.client(), path, data)
end
2020-05-15 17:24:57 -06:00
defp http_request(dirty, module) when module in @singular do
path = path = module.path()
data = render(module, dirty)
API.patch(API.client(), path, data)
end
2020-05-15 17:24:57 -06:00
defp http_request(dirty, module) do
path = Path.join(module.path(), to_string(dirty.id))
data = render(module, dirty)
API.patch(API.client(), path, data)
end
end