v10.0.0-rc25 - More debug points
parent
6a07b8539f
commit
db1d6cf4f5
|
@ -40,17 +40,20 @@ defmodule FarmbotCore.Asset.Private do
|
|||
# Because sqlite can't test unique constraints before a transaction, if this function gets called for
|
||||
# the same asset more than once asyncronously, the asset can be marked dirty twice at the same time
|
||||
# causing the `unique constraint` error to happen in either `ecto` OR `sqlite`. I've
|
||||
# caught both errors here as they are both essentially the same thing, and can be safely
|
||||
# caught both errors here as they are both essentially the same thing, and can be safely
|
||||
# discarded. Doing an `insert_or_update/1` (without the bang) can still result in the sqlite
|
||||
# error being thrown.
|
||||
# error being thrown.
|
||||
changeset = LocalMeta.changeset(local_meta, Map.merge(params, %{table: table, status: "dirty"}))
|
||||
try do
|
||||
Repo.insert_or_update!(changeset)
|
||||
result = Repo.insert_or_update!(changeset)
|
||||
%FarmbotCore.Asset.Private.LocalMeta{} = result
|
||||
FarmbotCore.Logger.debug(3, "#mark_dirty!/2: #{result.table}##{result.id}")
|
||||
result
|
||||
catch
|
||||
:error, %Sqlite.DbConnection.Error{
|
||||
message: "UNIQUE constraint failed: local_metas.table, local_metas.asset_local_id",
|
||||
message: "UNIQUE constraint failed: local_metas.table, local_metas.asset_local_id",
|
||||
sqlite: %{code: :constraint}
|
||||
} ->
|
||||
} ->
|
||||
Logger.warn """
|
||||
Caught race condition marking data as dirty (sqlite)
|
||||
table: #{inspect(table)}
|
||||
|
@ -59,10 +62,10 @@ defmodule FarmbotCore.Asset.Private do
|
|||
Ecto.Changeset.apply_changes(changeset)
|
||||
:error, %Ecto.InvalidChangesetError{
|
||||
changeset: %{
|
||||
action: :insert,
|
||||
action: :insert,
|
||||
errors: [
|
||||
table: {"LocalMeta already exists.", [
|
||||
validation: :unsafe_unique,
|
||||
validation: :unsafe_unique,
|
||||
fields: [:table, :asset_local_id]
|
||||
]}
|
||||
]}
|
||||
|
@ -73,7 +76,7 @@ defmodule FarmbotCore.Asset.Private do
|
|||
id: #{inspect(asset.local_id)}
|
||||
"""
|
||||
Ecto.Changeset.apply_changes(changeset)
|
||||
type, reason ->
|
||||
type, reason ->
|
||||
FarmbotCore.Logger.error 1, """
|
||||
Caught unexpected error marking data as dirty
|
||||
table: #{inspect(table)}
|
||||
|
|
|
@ -6,8 +6,6 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
import API.View, only: [render: 2]
|
||||
|
||||
require Logger
|
||||
require FarmbotCore.Logger
|
||||
|
||||
use GenServer
|
||||
@timeout 10000
|
||||
|
||||
|
@ -36,7 +34,6 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
@impl GenServer
|
||||
def init(args) do
|
||||
# FarmbotCore.Logger.disable(self())
|
||||
module = Keyword.fetch!(args, :module)
|
||||
timeout = Keyword.get(args, :timeout, @timeout)
|
||||
{:ok, %{module: module, timeout: timeout}, timeout}
|
||||
|
@ -55,27 +52,15 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
end
|
||||
|
||||
def handle_continue([dirty | rest], %{module: module} = state) do
|
||||
FarmbotCore.Logger.info(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] Handling dirty data"
|
||||
)
|
||||
|
||||
case http_request(dirty, state) do
|
||||
# Valid data
|
||||
{:ok, %{status: s, body: body}} when s > 199 and s < 300 ->
|
||||
FarmbotCore.Logger.debug(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} ok"
|
||||
)
|
||||
|
||||
dirty |> module.changeset(body) |> handle_changeset(rest, state)
|
||||
|
||||
# Invalid data
|
||||
{:ok, %{status: s, body: %{} = body}} when s > 399 and s < 500 ->
|
||||
FarmbotCore.Logger.debug(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error+body"
|
||||
)
|
||||
|
||||
changeset = module.changeset(dirty)
|
||||
|
||||
|
@ -86,10 +71,6 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
# Invalid data, but the API didn't say why
|
||||
{:ok, %{status: s, body: _body}} when s > 399 and s < 500 ->
|
||||
FarmbotCore.Logger.debug(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error"
|
||||
)
|
||||
|
||||
module.changeset(dirty)
|
||||
|> Map.put(:valid?, false)
|
||||
|
@ -97,8 +78,7 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
# HTTP Error. (500, network error, timeout etc.)
|
||||
error ->
|
||||
FarmbotCore.Logger.error(
|
||||
3,
|
||||
Logger.error(
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP Error: #{state.module} #{
|
||||
inspect(error)
|
||||
}"
|
||||
|
@ -110,7 +90,6 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
# If the changeset was valid, update the record.
|
||||
def handle_changeset(%{valid?: true} = changeset, rest, state) do
|
||||
# FarmbotCore.Logger.info("Successfully synced: #{state.module}")
|
||||
|
||||
Repo.update!(changeset)
|
||||
|> Private.mark_clean!()
|
||||
|
@ -128,27 +107,23 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
end)
|
||||
|> Enum.join("\n")
|
||||
|
||||
FarmbotCore.Logger.error(3, "Failed to sync: #{state.module} \n #{message}")
|
||||
_ = Repo.delete!(data)
|
||||
{:noreply, state, {:continue, rest}}
|
||||
end
|
||||
|
||||
defp http_request(%{id: nil} = dirty, state) do
|
||||
FarmbotCore.Logger.debug(3, "#{state.module} clean request (post)")
|
||||
path = state.module.path()
|
||||
data = render(state.module, dirty)
|
||||
API.post(API.client(), path, data)
|
||||
end
|
||||
|
||||
defp http_request(dirty, %{module: module} = state) when module in @singular do
|
||||
FarmbotCore.Logger.debug(3, "#{state.module} dirty request (patch)")
|
||||
path = path = state.module.path()
|
||||
data = render(state.module, dirty)
|
||||
API.patch(API.client(), path, data)
|
||||
end
|
||||
|
||||
defp http_request(dirty, state) do
|
||||
FarmbotCore.Logger.debug(3, "#{state.module} dirty request (patch)")
|
||||
path = Path.join(state.module.path(), to_string(dirty.id))
|
||||
data = render(state.module, dirty)
|
||||
API.patch(API.client(), path, data)
|
||||
|
|
|
@ -70,7 +70,6 @@ defmodule FarmbotOS.SysCalls.ResourceUpdate do
|
|||
with %{} = point <- Asset.get_point(id: id),
|
||||
{:ok, point} <- Asset.update_point(point, params) do
|
||||
_ = Private.mark_dirty!(point)
|
||||
IO.puts("Updated point #{id}: #{inspect(params)}")
|
||||
:ok
|
||||
else
|
||||
nil ->
|
||||
|
@ -84,6 +83,9 @@ defmodule FarmbotOS.SysCalls.ResourceUpdate do
|
|||
|
||||
FarmbotCore.Logger.error(3, msg)
|
||||
{:error, msg}
|
||||
|
||||
err ->
|
||||
{:error, "Unknown error. Please notify support. #{inspect(err)}"}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue