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
This commit is contained in:
Tim Sherratt 2022-04-25 00:03:08 +10:00 committed by GitHub
commit 3001e1e394
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 13 deletions

View file

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

View file

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