Skip to content

Commit

Permalink
Do not redeploy apps from dir on every node (#2291)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Oct 22, 2023
1 parent 06b5532 commit ccf34ba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/livebook/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ defmodule Livebook.Application do

Livebook.Apps.deploy_apps_in_dir(apps_path,
password: Livebook.Config.apps_path_password(),
warmup: warmup
warmup: warmup,
start_only: true
)
end
end
Expand Down
39 changes: 27 additions & 12 deletions lib/livebook/apps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ defmodule Livebook.Apps do
* `:files_source` - a location to fetch notebook files from, see
`Livebook.Session.start_link/1` for more details
* `:start_only` - when `true`, deploys only if the app does not
exist already. Defaults to `false`
"""
@spec deploy(Livebook.Notebook.t(), keyword()) :: {:ok, pid()} | {:error, term()}
@spec deploy(Livebook.Notebook.t(), keyword()) ::
{:ok, pid()} | {:error, :already_started} | {:error, term()}
def deploy(notebook, opts \\ []) do
opts = Keyword.validate!(opts, warnings: [], files_source: nil)
opts = Keyword.validate!(opts, warnings: [], files_source: nil, start_only: false)

slug = notebook.app_settings.slug
name = name(slug)
Expand All @@ -41,18 +45,12 @@ defmodule Livebook.Apps do
end

pid ->
App.deploy(pid, notebook,
warnings: opts[:warnings],
files_source: opts[:files_source]
)

{:ok, pid}
redeploy_app(pid, notebook, opts)
end
end)

pid ->
App.deploy(pid, notebook, warnings: opts[:warnings], files_source: opts[:files_source])
{:ok, pid}
redeploy_app(pid, notebook, opts)
end
end

Expand All @@ -77,6 +75,15 @@ defmodule Livebook.Apps do
end
end

defp redeploy_app(pid, notebook, opts) do
if opts[:start_only] do
{:error, :already_started}
else
App.deploy(pid, notebook, warnings: opts[:warnings], files_source: opts[:files_source])
{:ok, pid}
end
end

@doc """
Returns app process pid for the given slug.
"""
Expand Down Expand Up @@ -166,10 +173,14 @@ defmodule Livebook.Apps do
This can be used to warmup apps without deployment. Defaults
to `false`
* `:start_only` - when `true`, deploys only if the app does not
exist already. Defaults to `false`
"""
@spec deploy_apps_in_dir(String.t(), keyword()) :: :ok
def deploy_apps_in_dir(path, opts \\ []) do
opts = Keyword.validate!(opts, [:password, warmup: true, skip_deploy: false])
opts =
Keyword.validate!(opts, [:password, warmup: true, skip_deploy: false, start_only: false])

infos = import_app_notebooks(path)

Expand Down Expand Up @@ -226,7 +237,11 @@ defmodule Livebook.Apps do

warnings = Enum.map(info.import_warnings, &("Import: " <> &1))

{:ok, _} = deploy(notebook, warnings: warnings, files_source: info.files_source)
deploy(notebook,
warnings: warnings,
files_source: info.files_source,
start_only: opts[:start_only]
)
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/livebook/apps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,29 @@ defmodule Livebook.AppsTest do

Livebook.App.close(app.pid)
end

@tag :tmp_dir
test "skips existing apps when :start_only is enabled", %{tmp_dir: tmp_dir} do
app_path = Path.join(tmp_dir, "app.livemd")

File.write!(app_path, """
<!-- livebook:{"app_settings":{"access_type":"public","slug":"app"}} -->
# App
""")

Livebook.Apps.subscribe()

Livebook.Apps.deploy_apps_in_dir(tmp_dir)
assert_receive {:app_updated, app}

Livebook.Apps.deploy_apps_in_dir(tmp_dir)
assert %{version: 2} = Livebook.App.get_by_pid(app.pid)

Livebook.Apps.deploy_apps_in_dir(tmp_dir, start_only: true)
assert %{version: 2} = Livebook.App.get_by_pid(app.pid)

Livebook.App.close(app.pid)
end
end
end

0 comments on commit ccf34ba

Please sign in to comment.