Creating Background Job a In Controller

Senario: User clicks deploy button. In the DeployController we create a background job that will run some deploy code. The controller finishes and renders the show page to the user before this job has finished. Once the background job is finished, the UI on the show page updates notifiying the user that their deploy has finished.

My Idea: In the controller use Task.start so the request will move past the controller and into the render logic of the controller. Then once the deploy has finished there is something that will broadcast something out to a deploy channel.

Questions: Does this seem like the right approach. Is Task.start correct to use here?

Example Code

defmodule MyApp.DeployController do

  def deploy(conn, _params) do

  Task.start fn ->
       case do_some_deploy_logic do
        {:ok, msg} -> broadcast_deploy_success
        {:error, msg} -> broadcast_deploy_failure
       end
     end)

  conn
    |> put_flash(:info, "Your deployment has started!")
    |> render("show.html")
  end

Is there some sort of synchronization needed around deployment? As in, one a deploy job is started you can’t start a second one? If so, you’ll need a registry or a queue or something.

In this case deploys can happen simultaneously.