Skip to content

Commit 18cbdf3

Browse files
lbristol88tswast
authored andcommitted
Set a timeout when waiting for query job to finish. (GoogleCloudPlatform#2064)
1 parent 1d4e988 commit 18cbdf3

File tree

5 files changed

+107
-25
lines changed

5 files changed

+107
-25
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# Python pycache:
17+
__pycache__/
18+
# Ignored by the build system
19+
/setup.cfg

appengine/standard_python37/bigquery/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
# limitations under the License.
1414

1515
# [START gae_python37_bigquery]
16+
import concurrent.futures
17+
1618
from flask import Flask, render_template
1719
from google.cloud import bigquery
1820

21+
1922
app = Flask(__name__)
2023
bigquery_client = bigquery.Client()
2124

@@ -34,7 +37,12 @@ def main():
3437
LIMIT 10
3538
""")
3639

37-
results = query_job.result()
40+
try:
41+
# Set a timeout because queries could take longer than one minute.
42+
results = query_job.result(timeout=30)
43+
except concurrent.futures.TimeoutError:
44+
return render_template('timeout.html', job_id=query_job.job_id)
45+
3846
return render_template('query_result.html', results=results)
3947

4048

appengine/standard_python37/bigquery/main_test.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,38 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import concurrent.futures
16+
from unittest import mock
1517

16-
def test_main():
18+
from google.cloud import bigquery
19+
import pytest
20+
21+
22+
@pytest.fixture
23+
def flask_client():
1724
import main
1825

1926
main.app.testing = True
20-
client = main.app.test_client()
27+
return main.app.test_client()
28+
2129

22-
r = client.get('/')
30+
def test_main(flask_client):
31+
r = flask_client.get('/')
2332
assert r.status_code == 200
2433
assert 'Query Result' in r.data.decode('utf-8')
34+
35+
36+
def test_main_timeout(flask_client, monkeypatch):
37+
import main
38+
39+
fake_job = mock.create_autospec(bigquery.QueryJob)
40+
fake_job.result.side_effect = concurrent.futures.TimeoutError()
41+
42+
def fake_query(query):
43+
return fake_job
44+
45+
monkeypatch.setattr(main.bigquery_client, 'query', fake_query)
46+
47+
r = flask_client.get('/')
48+
assert r.status_code == 200
49+
assert 'Query Timeout' in r.data.decode('utf-8')
Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
<!doctype <!DOCTYPE html>
2-
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>Query Result</title>
6-
</head>
7-
<body>
8-
<table>
9-
<tr>
10-
<th>URL</th>
11-
<th>View Count</th>
12-
</tr>
13-
{% for result in results %}
14-
<tr>
15-
<td>{{ result[0] }}</td>
16-
<td>{{ result[1] }}</td>
17-
</tr>
18-
{% endfor %}
19-
</table>
20-
</body>
21-
</html>
1+
<!DOCTYPE html>
2+
{#
3+
Copyright 2019 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
#}
17+
<meta charset="utf-8" />
18+
<title>Query Result</title>
19+
20+
<table>
21+
<tr>
22+
<th>URL</th>
23+
<th>View Count</th>
24+
</tr>
25+
{% for result in results %}
26+
<tr>
27+
<td>{{ result[0] }}</td>
28+
<td>{{ result[1] }}</td>
29+
</tr>
30+
{% endfor %}
31+
</table>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
{#
3+
Copyright 2019 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
#}
17+
<meta charset="utf-8" />
18+
<title>Query Timeout</title>
19+
20+
<p>Query job {{ job_id }} timed out.

0 commit comments

Comments
 (0)