Add the SyncWorkerManual to run sync manually and notify when done.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -89,6 +89,10 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Phoenix.PubSub.subscribe(DecentralisedBookIndex.PubSub, DecentralisedBookIndex.SyncWorkerManual.topic())
|
||||
end
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign_new(:current_user, fn -> nil end)}
|
||||
@@ -198,23 +202,25 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
||||
|
||||
@impl true
|
||||
def handle_event("sync", _params, socket) do
|
||||
Task.async(fn ->
|
||||
SyncServerTask.sync_all()
|
||||
send(self(), :sync_completed)
|
||||
end)
|
||||
case DecentralisedBookIndex.SyncWorkerManual.new(%{}) |> Oban.insert() do
|
||||
{:ok, _job} ->
|
||||
{:noreply, put_flash(socket, :info, "Task started")}
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> put_flash(:info, "The sync is started")
|
||||
|
||||
{:noreply, socket}
|
||||
{:error, reason} ->
|
||||
{:noreply, put_flash(socket, :error, "Failed to start task: #{inspect(reason)}")}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({_pid, :sync_completed}, socket) do
|
||||
def handle_info({:manual_sync_completed, result}, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> put_flash(:info, "The sync is done")
|
||||
if result == :ok do
|
||||
socket
|
||||
|> put_flash(:info, "Task completed")
|
||||
else
|
||||
socket
|
||||
|> put_flash(:error, "Task failed")
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user