v10.0.0-rc27 - Thanks, Connor.
parent
6d92a11ebd
commit
b9fca35731
|
@ -36,7 +36,8 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
def init(args) do
|
def init(args) do
|
||||||
module = Keyword.fetch!(args, :module)
|
module = Keyword.fetch!(args, :module)
|
||||||
timeout = Keyword.get(args, :timeout, @timeout)
|
timeout = Keyword.get(args, :timeout, @timeout)
|
||||||
{:ok, %{module: module, timeout: timeout}, timeout}
|
timer = Process.send_after(self(), :timeout, timeout)
|
||||||
|
{:ok, %{module: module, timeout: timeout, timer: timer}}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl GenServer
|
@impl GenServer
|
||||||
|
@ -48,20 +49,18 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
|
|
||||||
@impl GenServer
|
@impl GenServer
|
||||||
def handle_continue([], state) do
|
def handle_continue([], state) do
|
||||||
{:noreply, state, state.timeout}
|
timer = Process.send_after(self(), :timeout, state.timeout)
|
||||||
|
{:noreply, %{state | timer: timer}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_continue([dirty | rest], %{module: module} = state) do
|
def handle_continue([dirty | rest], %{module: module} = state) do
|
||||||
|
|
||||||
case http_request(dirty, state) do
|
case http_request(dirty, state) do
|
||||||
# Valid data
|
# Valid data
|
||||||
{:ok, %{status: s, body: body}} when s > 199 and s < 300 ->
|
{:ok, %{status: s, body: body}} when s > 199 and s < 300 ->
|
||||||
|
|
||||||
dirty |> module.changeset(body) |> handle_changeset(rest, state)
|
dirty |> module.changeset(body) |> handle_changeset(rest, state)
|
||||||
|
|
||||||
# Invalid data
|
# Invalid data
|
||||||
{:ok, %{status: s, body: %{} = body}} when s > 399 and s < 500 ->
|
{:ok, %{status: s, body: %{} = body}} when s > 399 and s < 500 ->
|
||||||
|
|
||||||
changeset = module.changeset(dirty)
|
changeset = module.changeset(dirty)
|
||||||
|
|
||||||
Enum.reduce(body, changeset, fn {key, val}, changeset ->
|
Enum.reduce(body, changeset, fn {key, val}, changeset ->
|
||||||
|
@ -71,7 +70,6 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
|
|
||||||
# Invalid data, but the API didn't say why
|
# Invalid data, but the API didn't say why
|
||||||
{:ok, %{status: s, body: _body}} when s > 399 and s < 500 ->
|
{:ok, %{status: s, body: _body}} when s > 399 and s < 500 ->
|
||||||
|
|
||||||
module.changeset(dirty)
|
module.changeset(dirty)
|
||||||
|> Map.put(:valid?, false)
|
|> Map.put(:valid?, false)
|
||||||
|> handle_changeset(rest, state)
|
|> handle_changeset(rest, state)
|
||||||
|
@ -90,15 +88,12 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
|
|
||||||
# If the changeset was valid, update the record.
|
# If the changeset was valid, update the record.
|
||||||
def handle_changeset(%{valid?: true} = changeset, rest, state) do
|
def handle_changeset(%{valid?: true} = changeset, rest, state) do
|
||||||
|
|
||||||
Repo.update!(changeset)
|
Repo.update!(changeset)
|
||||||
|> Private.mark_clean!()
|
|> Private.mark_clean!()
|
||||||
|
|
||||||
{:noreply, state, {:continue, rest}}
|
{:noreply, state, {:continue, rest}}
|
||||||
end
|
end
|
||||||
|
|
||||||
# If the changeset was invalid, delete the record.
|
|
||||||
# TODO(Connor) - Update the dirty field here, upload to rollbar?
|
|
||||||
def handle_changeset(%{valid?: false, data: data} = changeset, rest, state) do
|
def handle_changeset(%{valid?: false, data: data} = changeset, rest, state) do
|
||||||
message =
|
message =
|
||||||
Enum.map(changeset.errors, fn
|
Enum.map(changeset.errors, fn
|
||||||
|
@ -107,6 +102,7 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
end)
|
end)
|
||||||
|> Enum.join("\n")
|
|> Enum.join("\n")
|
||||||
|
|
||||||
|
Logger.error("Failed to sync: #{state.module} \n #{message}")
|
||||||
_ = Repo.delete!(data)
|
_ = Repo.delete!(data)
|
||||||
{:noreply, state, {:continue, rest}}
|
{:noreply, state, {:continue, rest}}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue