add a force sync mech to repo manager

This commit is contained in:
connor rigby 2017-11-13 13:22:08 -08:00
parent 2981e4ef72
commit f595f9e38a
4 changed files with 39 additions and 13 deletions

View file

@ -20,8 +20,8 @@ config :farmbot, :init, [
# Transports.
config :farmbot, :transport, [
# Farmbot.BotState.Transport.GenMQTT,
Farmbot.BotState.Transport.AMQP,
Farmbot.BotState.Transport.GenMQTT,
# Farmbot.BotState.Transport.AMQP,
Farmbot.BotState.Transport.HTTP,
]

View file

@ -58,7 +58,7 @@ defmodule Farmbot.BotState.Transport.GenMQTT.Client do
Logger.info(3, "Connected to real time services.")
if state.cache do
GenMQTT.publish(self(), status_topic(state.device), state.cache, 0, false)
GenMQTT.publish(self(), status_topic(state.device), Poison.encode!(state.cache), 0, false)
end
{:ok, %{state | connected: true}}
@ -110,10 +110,11 @@ defmodule Farmbot.BotState.Transport.GenMQTT.Client do
end
def handle_cast({:bot_state, bs}, state) do
json = Poison.encode!(bs)
GenMQTT.publish(self(), status_topic(state.device), json, 0, false)
# IO.puts "push state"
{:noreply, %{state | cache: json}}
if bs != state.cache do
json = Poison.encode!(bs)
GenMQTT.publish(self(), status_topic(state.device), json, 0, false)
end
{:noreply, %{state | cache: bs}}
end
def handle_cast({:bot_log, log}, state) do

View file

@ -42,20 +42,26 @@ defmodule Farmbot.Repo do
end
def init([repo_a, repo_b]) do
if Farmbot.System.ConfigStorage.get_config_value(:bool, "settings", "first_sync") do
needs_hard_sync = if Farmbot.System.ConfigStorage.get_config_value(:bool, "settings", "first_sync") || Farmbot.System.ConfigStorage.get_config_value(:bool, "settings", "auto_sync") do
do_sync_all_resources(repo_a)
do_sync_all_resources(repo_b)
Farmbot.System.ConfigStorage.update_config_value(:bool, "settings", "first_sync", false)
Farmbot.BotState.set_sync_status(:synced)
false
else
Farmbot.BotState.set_sync_status(:sync_now)
true
end
repos = case Farmbot.System.ConfigStorage.get_config_value(:string, "settings", "current_repo") do
"A" -> [repo_a, repo_b]
"B" -> [repo_b, repo_a]
end
# Copy configs
[current, _] = repos
copy_configs(current)
{:ok, %{repos: repos, sync_cmds: []}}
{:ok, %{repos: repos, sync_cmds: [], needs_hard_sync: needs_hard_sync}}
end
defp copy_configs(repo) do
@ -79,6 +85,22 @@ defmodule Farmbot.Repo do
{:reply, repo_b, state}
end
def handle_call(:flip, _, %{repos: [repo_a, repo_b], needs_hard_sync: true} = state) do
Farmbot.BotState.set_sync_status(:syncing)
do_sync_all_resources(repo_a)
do_sync_all_resources(repo_b)
Farmbot.BotState.set_sync_status(:synced)
copy_configs(repo_b)
case Farmbot.System.ConfigStorage.get_config_value(:string, "settings", "current_repo") do
"A" ->
Farmbot.System.ConfigStorage.update_config_value(:string, "settings", "current_repo", "B")
"B" ->
Farmbot.System.ConfigStorage.update_config_value(:string, "settings", "current_repo", "A")
end
{:reply, :ok, %{state | repos: [repo_b, repo_a], sync_cmds: [], needs_hard_sync: false}}
end
def handle_call(:flip, _, %{repos: [repo_a, repo_b]} = state) do
Farmbot.BotState.set_sync_status(:syncing)
Enum.reverse(state.sync_cmds) |> Enum.map(fn(sync_cmd) ->
@ -97,7 +119,10 @@ defmodule Farmbot.Repo do
end
def handle_call({:register_sync_cmd, sync_cmd}, _from, state) do
Farmbot.BotState.set_sync_status(:sync_now)
case Farmbot.System.ConfigStorage.get_config_value(:bool, "settings", "auto_sync") do
false -> Farmbot.BotState.set_sync_status(:sync_now)
true -> Farmbot.BotState.set_sync_status(:syncing)
end
[_current_repo, other_repo] = state.repos
apply_sync_cmd(other_repo, sync_cmd)
sync_cmds = if sync_cmd in state.sync_cmds do

View file

@ -36,7 +36,7 @@ defmodule Farmbot.System do
def factory_reset(reason) do
formatted = format_reason(reason)
Ecto.drop()
write_file(reason)
write_file(formatted)
@system_tasks.factory_reset(formatted)
end
@ -44,7 +44,7 @@ defmodule Farmbot.System do
@spec reboot(unparsed_reason) :: no_return
def reboot(reason) do
formatted = format_reason(reason)
write_file(reason)
write_file(formatted)
@system_tasks.reboot(formatted)
end
@ -52,7 +52,7 @@ defmodule Farmbot.System do
@spec shutdown(unparsed_reason) :: no_return
def shutdown(reason) do
formatted = format_reason(reason)
write_file(reason)
write_file(formatted)
@system_tasks.shutdown(formatted)
end