21
21
import random
22
22
import shutil
23
23
import string
24
- import subprocess
25
24
import sys
26
25
import tempfile
27
26
import textwrap
@@ -87,19 +86,6 @@ def _get_hash(def_, package_requirements=None):
87
86
return hashlib .md5 (def_repr ).hexdigest ()
88
87
89
88
90
- def _run_system_command (command ):
91
- program = subprocess .Popen (
92
- [command ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , shell = True
93
- )
94
- stdout , stderr = program .communicate ()
95
- exit_code = program .wait ()
96
- if exit_code :
97
- raise RuntimeError (
98
- f"Command: { command } \n Output: { stdout .decode ()} \n Error: { stderr .decode ()} "
99
- f"{ constants .FEEDBACK_LINK } "
100
- )
101
-
102
-
103
89
def routine_ref_to_string_for_query (routine_ref : bigquery .RoutineReference ) -> str :
104
90
return f"`{ routine_ref .project } .{ routine_ref .dataset_id } `.{ routine_ref .routine_id } "
105
91
@@ -281,6 +267,8 @@ def generate_cloud_function_main_code(self, def_, dir):
281
267
code_template = textwrap .dedent (
282
268
"""\
283
269
import cloudpickle
270
+ import functions_framework
271
+ from flask import jsonify
284
272
import json
285
273
286
274
# original udf code is in {udf_code_file}
@@ -289,14 +277,17 @@ def generate_cloud_function_main_code(self, def_, dir):
289
277
udf = cloudpickle.load(f)
290
278
291
279
def {handler_func_name}(request):
292
- request_json = request.get_json(silent=True)
293
- calls = request_json["calls"]
294
- replies = []
295
- for call in calls:
296
- reply = udf(*call)
297
- replies.append(reply)
298
- return_json = json.dumps({{"replies" : replies}})
299
- return return_json
280
+ try:
281
+ request_json = request.get_json(silent=True)
282
+ calls = request_json["calls"]
283
+ replies = []
284
+ for call in calls:
285
+ reply = udf(*call)
286
+ replies.append(reply)
287
+ return_json = json.dumps({{"replies" : replies}})
288
+ return return_json
289
+ except Exception as e:
290
+ return jsonify( {{ "errorMessage": str(e) }} ), 400
300
291
"""
301
292
)
302
293
0 commit comments