mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-04 12:04:20 +08:00
Minor improvements to Server task
This commit is contained in:
parent
a99b687f2c
commit
ccc07af9d9
1 changed files with 11 additions and 26 deletions
|
@ -1,4 +1,5 @@
|
|||
defmodule LivebookCLI.Server do
|
||||
import LivebookCLI.Utils
|
||||
@behaviour LivebookCLI.Task
|
||||
|
||||
@external_resource "README.md"
|
||||
|
@ -63,8 +64,7 @@ defmodule LivebookCLI.Server do
|
|||
|
||||
Starts a server and imports the notebook at the given URL:
|
||||
|
||||
livebook server https://example.com/my-notebook.livemd
|
||||
|
||||
livebook server https://example.com/my-notebook.livemd\
|
||||
"""
|
||||
end
|
||||
|
||||
|
@ -84,14 +84,12 @@ defmodule LivebookCLI.Server do
|
|||
|
||||
case check_endpoint_availability(base_url) do
|
||||
:livebook_running ->
|
||||
IO.puts("Livebook already running on #{base_url}")
|
||||
log_info("Livebook already running on #{base_url}")
|
||||
open_from_args(base_url, extra_args)
|
||||
|
||||
:taken ->
|
||||
print_error(
|
||||
"Another application is already running on port #{port}." <>
|
||||
" Either ensure this port is free or specify a different port using the --port option"
|
||||
)
|
||||
raise "Another application is already running on port #{port}." <>
|
||||
" Either ensure this port is free or specify a different port using the --port option"
|
||||
|
||||
:available ->
|
||||
start_server(extra_args)
|
||||
|
@ -108,7 +106,7 @@ defmodule LivebookCLI.Server do
|
|||
Process.sleep(:infinity)
|
||||
|
||||
{:error, error} ->
|
||||
print_error("Livebook failed to start with reason: #{inspect(error)}")
|
||||
raise "Livebook failed to start with reason: #{inspect(error)}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -126,20 +124,13 @@ defmodule LivebookCLI.Server do
|
|||
|
||||
defp check_endpoint_availability(base_url) do
|
||||
Application.ensure_all_started(:req)
|
||||
|
||||
health_url = set_path(base_url, "/public/health")
|
||||
|
||||
req = Req.new() |> Livebook.Utils.req_attach_defaults()
|
||||
|
||||
case Req.get(req, url: health_url, retry: false) do
|
||||
{:ok, %{status: 200, body: %{"application" => "livebook"}}} ->
|
||||
:livebook_running
|
||||
|
||||
{:ok, _other} ->
|
||||
:taken
|
||||
|
||||
{:error, _exception} ->
|
||||
:available
|
||||
{:ok, %{status: 200, body: %{"application" => "livebook"}}} -> :livebook_running
|
||||
{:ok, _other} -> :taken
|
||||
{:error, _exception} -> :available
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -158,7 +149,7 @@ defmodule LivebookCLI.Server do
|
|||
end
|
||||
|
||||
defp open_from_args(base_url, ["new"]) do
|
||||
IO.warn(~s/passing "new" as an argument is deprecated, use "@new" instead/, [])
|
||||
log_warning(~s/passing "new" as an argument is deprecated, use "@new" instead/)
|
||||
open_from_args(base_url, ["@new"])
|
||||
end
|
||||
|
||||
|
@ -183,9 +174,7 @@ defmodule LivebookCLI.Server do
|
|||
end
|
||||
|
||||
defp open_from_args(_base_url, _extra_args) do
|
||||
print_error(
|
||||
"Too many arguments entered. Ensure only one argument is used to specify the file path and all other arguments are preceded by the relevant switch"
|
||||
)
|
||||
raise "Too many arguments entered. Ensure only one argument is used to specify the file path and all other arguments are preceded by the relevant switch"
|
||||
end
|
||||
|
||||
@switches [
|
||||
|
@ -237,8 +226,4 @@ defmodule LivebookCLI.Server do
|
|||
|> Map.put(:path, path)
|
||||
|> URI.to_string()
|
||||
end
|
||||
|
||||
defp print_error(message) do
|
||||
IO.ANSI.format([:red, message]) |> IO.puts()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue