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:
@@ -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
|
||||
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