Add the SyncWorkerManual to run sync manually and notify when done.
continuous-integration/drone/push Build is passing Details

dev
KKlochko 2 months ago
parent b616c0d5a0
commit 44213069c9

@ -0,0 +1,31 @@
defmodule DecentralisedBookIndex.SyncWorkerManual do
use Oban.Worker,
queue: :default,
max_attempts: 2,
tags: ["sync", "manual"]
require Logger
alias DecentralisedBookIndex.SyncTasks.SyncServerTask
@topic "task:events"
@impl Oban.Worker
def perform(%Oban.Job{args: args} = job) do
Logger.info("The manual job (#{job.id}) run SyncServerTask at #{DateTime.utc_now()} with args: #{inspect(args)}")
result = SyncServerTask.sync_all()
Phoenix.PubSub.broadcast!(
DecentralisedBookIndex.PubSub,
@topic,
{:manual_sync_completed, result}
)
Logger.info("The manual job (#{job.id}) is done.")
result
end
def topic, do: @topic
end

@ -89,6 +89,10 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
if connected?(socket) do
Phoenix.PubSub.subscribe(DecentralisedBookIndex.PubSub, DecentralisedBookIndex.SyncWorkerManual.topic())
end
{:ok, {:ok,
socket socket
|> assign_new(:current_user, fn -> nil end)} |> assign_new(:current_user, fn -> nil end)}
@ -198,23 +202,25 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
@impl true @impl true
def handle_event("sync", _params, socket) do def handle_event("sync", _params, socket) do
Task.async(fn -> case DecentralisedBookIndex.SyncWorkerManual.new(%{}) |> Oban.insert() do
SyncServerTask.sync_all() {:ok, _job} ->
send(self(), :sync_completed) {:noreply, put_flash(socket, :info, "Task started")}
end)
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 end
@impl true @impl true
def handle_info({_pid, :sync_completed}, socket) do def handle_info({:manual_sync_completed, result}, socket) do
socket = socket =
socket if result == :ok do
|> put_flash(:info, "The sync is done") socket
|> put_flash(:info, "Task completed")
else
socket
|> put_flash(:error, "Task failed")
end
{:noreply, socket} {:noreply, socket}
end end

Loading…
Cancel
Save