46 lines
1.2 KiB
Elixir
46 lines
1.2 KiB
Elixir
defmodule Prymn.Worker do
|
|
use Oban.Worker
|
|
|
|
alias Prymn.{Agents, Apps}
|
|
|
|
@impl true
|
|
def perform(%Oban.Job{args: %{"app_id" => app_id}}) do
|
|
deploy_app(app_id)
|
|
await_deploy("app:#{app_id}")
|
|
:ok
|
|
end
|
|
|
|
defp deploy_app(app_id) do
|
|
pid = self()
|
|
app = Apps.get_app!(app_id)
|
|
# agent = Agents.from_app(app)
|
|
|
|
Task.start_link(fn ->
|
|
notify_fun = fn
|
|
:progress, data, progress -> send(pid, {:progress, data, progress})
|
|
:complete, data, _progress -> send(pid, {:complete, data})
|
|
:error, data, _progress -> send(pid, {:error, data})
|
|
end
|
|
|
|
# Apps.Wordpress.deploy(app, agent, notify_fun)
|
|
end)
|
|
end
|
|
|
|
defp await_deploy(channel) do
|
|
receive do
|
|
{:progress, data, progress} ->
|
|
Phoenix.PubSub.broadcast!(Prymn.PubSub, channel, {:progress, data, progress})
|
|
await_deploy(channel)
|
|
|
|
{:complete, data} ->
|
|
Phoenix.PubSub.broadcast!(Prymn.PubSub, channel, {:complete, data})
|
|
|
|
{:error, data} ->
|
|
Phoenix.PubSub.broadcast!(Prymn.PubSub, channel, {:error, data})
|
|
# raise "Error occured during deployment: #{inspect(data)}"
|
|
after
|
|
60_000 ->
|
|
raise RuntimeError, "no progress after 1 minute"
|
|
end
|
|
end
|
|
end
|