Add timeout option to Cloudrun build (#1717)

* Add timeout option for build phase
* Make the --timeout setting optional
* Add test for --timeout setting

Thanks, @wragge
api-extras
Tim Sherratt 2022-04-25 00:03:08 +10:00 zatwierdzone przez GitHub
rodzic d57c347f35
commit 3001e1e394
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 30 dodań i 13 usunięć

Wyświetl plik

@ -41,6 +41,10 @@ def publish_subcommand(publish):
type=click.Choice(["1", "2", "4"]),
help="Number of vCPUs to allocate in Cloud Run",
)
@click.option(
"--timeout",
help="Build timeout in seconds",
)
@click.option(
"--apt-get-install",
"apt_get_extras",
@ -72,6 +76,7 @@ def publish_subcommand(publish):
show_files,
memory,
cpu,
timeout,
apt_get_extras,
):
"Publish databases to Datasette running on Cloud Run"
@ -156,7 +161,12 @@ def publish_subcommand(publish):
print("\n====================\n")
image_id = f"gcr.io/{project}/{name}"
check_call(f"gcloud builds submit --tag {image_id}", shell=True)
check_call(
"gcloud builds submit --tag {}{}".format(
image_id, " --timeout {}".format(timeout) if timeout else ""
),
shell=True,
)
check_call(
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}{}".format(
image_id,

Wyświetl plik

@ -105,18 +105,19 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which, tmp_path_factory):
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
@pytest.mark.parametrize(
"memory,cpu,expected_gcloud_args",
"memory,cpu,timeout,expected_gcloud_args",
[
["1Gi", None, "--memory 1Gi"],
["2G", None, "--memory 2G"],
["256Mi", None, "--memory 256Mi"],
["4", None, None],
["GB", None, None],
[None, 1, "--cpu 1"],
[None, 2, "--cpu 2"],
[None, 3, None],
[None, 4, "--cpu 4"],
["2G", 4, "--memory 2G --cpu 4"],
["1Gi", None, None, "--memory 1Gi"],
["2G", None, None, "--memory 2G"],
["256Mi", None, None, "--memory 256Mi"],
["4", None, None, None],
["GB", None, None, None],
[None, 1, None, "--cpu 1"],
[None, 2, None, "--cpu 2"],
[None, 3, None, None],
[None, 4, None, "--cpu 4"],
["2G", 4, None, "--memory 2G --cpu 4"],
[None, None, 1800, "--timeout 1800"],
],
)
def test_publish_cloudrun_memory_cpu(
@ -125,6 +126,7 @@ def test_publish_cloudrun_memory_cpu(
mock_which,
memory,
cpu,
timeout,
expected_gcloud_args,
tmp_path_factory,
):
@ -139,6 +141,8 @@ def test_publish_cloudrun_memory_cpu(
args.extend(["--memory", memory])
if cpu:
args.extend(["--cpu", str(cpu)])
if timeout:
args.extend(["--timeout", str(timeout)])
result = runner.invoke(cli.cli, args)
if expected_gcloud_args is None:
assert 2 == result.exit_code
@ -149,13 +153,16 @@ def test_publish_cloudrun_memory_cpu(
"gcloud run deploy --allow-unauthenticated --platform=managed"
" --image {} test".format(tag)
)
expected_build_call = f"gcloud builds submit --tag {tag}"
if memory:
expected_call += " --memory {}".format(memory)
if cpu:
expected_call += " --cpu {}".format(cpu)
if timeout:
expected_build_call += f" --timeout {timeout}"
mock_call.assert_has_calls(
[
mock.call(f"gcloud builds submit --tag {tag}", shell=True),
mock.call(expected_build_call, shell=True),
mock.call(
expected_call,
shell=True,