Force farmbot to sync on first boot

feature/sequence-on-boot
Connor Rigby 2019-11-07 10:07:27 -08:00
parent fd7510fc94
commit 6704a40c5a
No known key found for this signature in database
GPG Key ID: 29A88B24B70456E0
5 changed files with 35 additions and 3 deletions

View File

@ -7,8 +7,15 @@ defmodule FarmbotCore.Asset.Query do
@callback auto_sync?() :: boolean()
@callback first_sync?() :: boolean()
@doc "Returns the configuration value for auto_sync"
def auto_sync?() do
Asset.fbos_config().auto_sync
end
@doc "Checks if initial syncing is still required"
def first_sync?() do
is_nil(Asset.fbos_config().id)
end
end

View File

@ -79,9 +79,13 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
def handle_info(:preload, state) do
_ = Leds.green(:really_fast_blink)
# this must be called __before__ preloading.
# if it's not, it will have been reset by the time the
# preload completes
first_sync? = Asset.Query.first_sync?()
with :ok <- Preloader.preload_all() do
if Asset.Query.auto_sync?() do
if Asset.Query.auto_sync?() || first_sync? do
_ = Leds.green(:solid)
BotState.set_sync_status("synced")
else

View File

@ -21,10 +21,19 @@ defmodule FarmbotExt.API.Preloader do
actually sync all resources. If it is not, preload all resources.
"""
def preload_all() do
# this must be called __before__ preloading.
# if it's not, it will have been reset by the time the
# preload completes
first_sync? = Query.first_sync?()
if first_sync? do
FarmbotCore.Logger.info(2, "Farmbot doing first sync")
end
with {:ok, sync_changeset} <- API.get_changeset(Sync),
sync_changeset <- Reconciler.sync_group(sync_changeset, SyncGroup.group_0()) do
FarmbotCore.Logger.success(3, "Successfully preloaded resources.")
maybe_auto_sync(sync_changeset, Query.auto_sync?())
maybe_auto_sync(sync_changeset, Query.auto_sync?() || first_sync?)
end
end

View File

@ -36,6 +36,7 @@ defmodule AutoSyncChannelTest do
test_pid = self()
expect(Query, :auto_sync?, 2, fn -> false end)
expect(Query, :first_sync?, 2, fn -> false end)
expect(API, :get_changeset, fn _module ->
send(test_pid, :preload_all_called)
@ -148,6 +149,8 @@ defmodule AutoSyncChannelTest do
false
end)
stub(Query, :first_sync?, fn -> false end)
send(pid, {:basic_deliver, payload, %{routing_key: key}})
assert_receive :called_auto_sync?
end
@ -158,6 +161,7 @@ defmodule AutoSyncChannelTest do
payload = '{"args":{"label":"foo"},"body":{}}'
key = "bot.device_15.sync.Device.999"
stub(Query, :auto_sync?, fn -> true end)
stub(Query, :first_sync?, fn -> false end)
stub(Command, :update, fn x, y, z ->
send(test_pid, {:update_called, x, y, z})
@ -175,6 +179,7 @@ defmodule AutoSyncChannelTest do
key = "bot.device_15.sync.#{module_name}.999"
stub(Query, :auto_sync?, fn -> true end)
stub(Query, :first_sync?, fn -> false end)
stub(Command, :update, fn x, y, z ->
send(test_pid, {:update_called, x, y, z})
@ -203,6 +208,8 @@ defmodule AutoSyncChannelTest do
false
end)
stub(Query, :first_sync?, fn -> false end)
stub(Command, :update, fn kind, id, params ->
send(test_pid, {:update_called, kind, id, params})
:ok
@ -225,6 +232,8 @@ defmodule AutoSyncChannelTest do
false
end)
stub(Query, :first_sync?, fn -> false end)
stub(Command, :new_changeset, fn kind, id, params ->
send(test_pid, {:new_changeset_called, kind, id, params})
:ok
@ -246,6 +255,7 @@ defmodule AutoSyncChannelTest do
key = "bot.device_15.sync.#{module_name}.999"
stub(Query, :auto_sync?, fn -> true end)
stub(Query, :first_sync?, fn -> false end)
stub(Command, :update, fn x, y, z ->
send(test_pid, {:update_called, x, y, z})

View File

@ -4,7 +4,7 @@ defmodule FarmbotExt.API.PreloaderTest do
alias FarmbotCore.{
# Asset,
# Asset.Query,
Asset.Query,
Asset.Sync
}
@ -17,6 +17,8 @@ defmodule FarmbotExt.API.PreloaderTest do
{:error, "some descriptive API error"}
end)
expect(Query, :first_sync?, 1, fn -> false end)
assert {:error, "some descriptive API error"} = Preloader.preload_all()
end
end