v10.0.0-rc24 - add loggers in `DiryWorker` module
parent
10695dcc98
commit
6a07b8539f
|
@ -6,6 +6,8 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
import API.View, only: [render: 2]
|
||||
|
||||
require Logger
|
||||
require FarmbotCore.Logger
|
||||
|
||||
use GenServer
|
||||
@timeout 10000
|
||||
|
||||
|
@ -34,7 +36,7 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
@impl GenServer
|
||||
def init(args) do
|
||||
# Logger.disable(self())
|
||||
# FarmbotCore.Logger.disable(self())
|
||||
module = Keyword.fetch!(args, :module)
|
||||
timeout = Keyword.get(args, :timeout, @timeout)
|
||||
{:ok, %{module: module, timeout: timeout}, timeout}
|
||||
|
@ -53,22 +55,27 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
end
|
||||
|
||||
def handle_continue([dirty | rest], %{module: module} = state) do
|
||||
# Logger.info("[#{module} #{dirty.local_id} #{inspect(self())}] Handling dirty data")
|
||||
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 ->
|
||||
# Logger.debug(
|
||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} ok"
|
||||
# )
|
||||
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 ->
|
||||
# Logger.debug(
|
||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error+body"
|
||||
# )
|
||||
FarmbotCore.Logger.debug(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error+body"
|
||||
)
|
||||
|
||||
changeset = module.changeset(dirty)
|
||||
|
||||
|
@ -79,9 +86,10 @@ 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 ->
|
||||
# Logger.debug(
|
||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error"
|
||||
# )
|
||||
FarmbotCore.Logger.debug(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error"
|
||||
)
|
||||
|
||||
module.changeset(dirty)
|
||||
|> Map.put(:valid?, false)
|
||||
|
@ -89,7 +97,8 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
# HTTP Error. (500, network error, timeout etc.)
|
||||
error ->
|
||||
Logger.error(
|
||||
FarmbotCore.Logger.error(
|
||||
3,
|
||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP Error: #{state.module} #{
|
||||
inspect(error)
|
||||
}"
|
||||
|
@ -101,7 +110,7 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
|
||||
# If the changeset was valid, update the record.
|
||||
def handle_changeset(%{valid?: true} = changeset, rest, state) do
|
||||
# Logger.info("Successfully synced: #{state.module}")
|
||||
# FarmbotCore.Logger.info("Successfully synced: #{state.module}")
|
||||
|
||||
Repo.update!(changeset)
|
||||
|> Private.mark_clean!()
|
||||
|
@ -119,27 +128,27 @@ defmodule FarmbotExt.API.DirtyWorker do
|
|||
end)
|
||||
|> Enum.join("\n")
|
||||
|
||||
Logger.error("Failed to sync: #{state.module} \n #{message}")
|
||||
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
|
||||
# Logger.debug("#{state.module} clean request (post)")
|
||||
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
|
||||
# Logger.debug("#{state.module} dirty request (patch)")
|
||||
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
|
||||
# Logger.debug("#{state.module} dirty request (patch)")
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue