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]
|
import API.View, only: [render: 2]
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
require FarmbotCore.Logger
|
||||||
|
|
||||||
use GenServer
|
use GenServer
|
||||||
@timeout 10000
|
@timeout 10000
|
||||||
|
|
||||||
|
@ -34,7 +36,7 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
|
|
||||||
@impl GenServer
|
@impl GenServer
|
||||||
def init(args) do
|
def init(args) do
|
||||||
# Logger.disable(self())
|
# FarmbotCore.Logger.disable(self())
|
||||||
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}
|
{:ok, %{module: module, timeout: timeout}, timeout}
|
||||||
|
@ -53,22 +55,27 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_continue([dirty | rest], %{module: module} = state) do
|
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
|
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 ->
|
||||||
# Logger.debug(
|
FarmbotCore.Logger.debug(
|
||||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} ok"
|
3,
|
||||||
# )
|
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} ok"
|
||||||
|
)
|
||||||
|
|
||||||
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 ->
|
||||||
# Logger.debug(
|
FarmbotCore.Logger.debug(
|
||||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error+body"
|
3,
|
||||||
# )
|
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error+body"
|
||||||
|
)
|
||||||
|
|
||||||
changeset = module.changeset(dirty)
|
changeset = module.changeset(dirty)
|
||||||
|
|
||||||
|
@ -79,9 +86,10 @@ 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 ->
|
||||||
# Logger.debug(
|
FarmbotCore.Logger.debug(
|
||||||
# "[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error"
|
3,
|
||||||
# )
|
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP request complete: #{s} error"
|
||||||
|
)
|
||||||
|
|
||||||
module.changeset(dirty)
|
module.changeset(dirty)
|
||||||
|> Map.put(:valid?, false)
|
|> Map.put(:valid?, false)
|
||||||
|
@ -89,7 +97,8 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
|
|
||||||
# HTTP Error. (500, network error, timeout etc.)
|
# HTTP Error. (500, network error, timeout etc.)
|
||||||
error ->
|
error ->
|
||||||
Logger.error(
|
FarmbotCore.Logger.error(
|
||||||
|
3,
|
||||||
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP Error: #{state.module} #{
|
"[#{module} #{dirty.local_id} #{inspect(self())}] HTTP Error: #{state.module} #{
|
||||||
inspect(error)
|
inspect(error)
|
||||||
}"
|
}"
|
||||||
|
@ -101,7 +110,7 @@ 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
|
||||||
# Logger.info("Successfully synced: #{state.module}")
|
# FarmbotCore.Logger.info("Successfully synced: #{state.module}")
|
||||||
|
|
||||||
Repo.update!(changeset)
|
Repo.update!(changeset)
|
||||||
|> Private.mark_clean!()
|
|> Private.mark_clean!()
|
||||||
|
@ -119,27 +128,27 @@ defmodule FarmbotExt.API.DirtyWorker do
|
||||||
end)
|
end)
|
||||||
|> Enum.join("\n")
|
|> 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)
|
_ = Repo.delete!(data)
|
||||||
{:noreply, state, {:continue, rest}}
|
{:noreply, state, {:continue, rest}}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp http_request(%{id: nil} = dirty, state) do
|
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()
|
path = state.module.path()
|
||||||
data = render(state.module, dirty)
|
data = render(state.module, dirty)
|
||||||
API.post(API.client(), path, data)
|
API.post(API.client(), path, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp http_request(dirty, %{module: module} = state) when module in @singular do
|
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()
|
path = path = state.module.path()
|
||||||
data = render(state.module, dirty)
|
data = render(state.module, dirty)
|
||||||
API.patch(API.client(), path, data)
|
API.patch(API.client(), path, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp http_request(dirty, state) do
|
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))
|
path = Path.join(state.module.path(), to_string(dirty.id))
|
||||||
data = render(state.module, dirty)
|
data = render(state.module, dirty)
|
||||||
API.patch(API.client(), path, data)
|
API.patch(API.client(), path, data)
|
||||||
|
|
Loading…
Reference in New Issue