Python runtime improvement

Change-Id: I8a77324df07c2d98773c20aa04eb81cc54152be3
This commit is contained in:
Lingxian Kong 2017-07-13 22:29:27 +12:00
parent f870ac6871
commit 0f7520b88c
5 changed files with 27 additions and 36 deletions

View File

@ -67,7 +67,7 @@ class ExecutionsController(rest.RestController):
pass pass
if func_url: if func_url:
r = requests.post(func_url, data=params.get('input')) r = requests.post(func_url, json=params.get('input'))
params.update( params.update(
{'status': 'success', 'output': {'result': r.json()}} {'status': 'success', 'output': {'result': r.json()}}
) )

View File

@ -79,8 +79,8 @@ class DefaultEngine(object):
input=None): input=None):
LOG.info( LOG.info(
'Creating execution. execution_id=%s, function_id=%s, ' 'Creating execution. execution_id=%s, function_id=%s, '
'runtime_id=%s', 'runtime_id=%s, input=%s',
execution_id, function_id, runtime_id execution_id, function_id, runtime_id, input
) )
with db_api.transaction(): with db_api.transaction():

View File

@ -298,7 +298,7 @@ class KubernetesManager(base.OrchestratorBase):
# new connection' error for some reason. Needs to find a better # new connection' error for some reason. Needs to find a better
# solution. # solution.
time.sleep(1) time.sleep(1)
r = requests.post(request_url, data=data) r = requests.post(request_url, json=data)
if r.status_code != requests.codes.ok: if r.status_code != requests.codes.ok:
raise exc.OrchestratorException( raise exc.OrchestratorException(
@ -355,7 +355,7 @@ class KubernetesManager(base.OrchestratorBase):
func_url = '%s/execute' % service_url func_url = '%s/execute' % service_url
LOG.info('Invoke function %s, url: %s', function_id, func_url) LOG.info('Invoke function %s, url: %s', function_id, func_url)
r = requests.post(func_url, data=input) r = requests.post(func_url, json=input)
return {'result': r.json()} return {'result': r.json()}
else: else:

View File

@ -1,9 +1,9 @@
FROM alpine:3.5 FROM phusion/baseimage:0.9.22
MAINTAINER lingxian.kong@gmail.com
RUN apk update RUN apt-get update
RUN apk add --no-cache linux-headers python2 python2-dev build-base py2-pip py2-pbr RUN apt-get -y install python-dev python-setuptools libffi-dev libxslt1-dev libxml2-dev libyaml-dev libssl-dev python-pip
RUN pip install --upgrade pip RUN pip install -U pip setuptools
RUN rm -r /root/.cache
COPY . /app COPY . /app
WORKDIR /app WORKDIR /app

View File

@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import importlib
import json import json
import logging import logging
import sys import sys
import time import time
import traceback
import zipfile import zipfile
import zipimport
from flask import abort from flask import abort
from flask import Flask from flask import Flask
@ -36,11 +37,12 @@ openstack_session = None
@app.route('/download', methods=['POST']) @app.route('/download', methods=['POST'])
def download(): def download():
download_url = request.form['download_url'] params = request.get_json() or {}
function_id = request.form['function_id'] download_url = params.get('download_url')
entry = request.form['entry'] function_id = params.get('function_id')
token = request.form.get('token') entry = params.get('entry')
auth_url = request.form.get('auth_url') token = params.get('token')
auth_url = params.get('auth_url')
headers = {} headers = {}
if token: if token:
@ -60,14 +62,12 @@ def download():
) )
r = requests.get(download_url, headers=headers, stream=True) r = requests.get(download_url, headers=headers, stream=True)
with open(zip_file, 'wb') as fd: with open(zip_file, 'wb') as fd:
for chunk in r.iter_content(chunk_size=65535): for chunk in r.iter_content(chunk_size=65535):
fd.write(chunk) fd.write(chunk)
if not zipfile.is_zipfile(zip_file): if not zipfile.is_zipfile(zip_file):
abort(500) abort(500)
app.logger.info('Code package downloaded to %s' % zip_file) app.logger.info('Code package downloaded to %s' % zip_file)
global function_module global function_module
@ -85,33 +85,24 @@ def execute():
global openstack_session global openstack_session
context = {'os_session': openstack_session} context = {'os_session': openstack_session}
input = request.get_json() or {}
try:
importer = zipimport.zipimporter(zip_file)
module = importer.load_module(function_module)
except Exception as e:
return Response(
response=json.dumps({'output': str(e), 'duration': 0}),
status=200,
mimetype='application/json'
)
input = {}
if request.form:
# Refer to:
# http://werkzeug.pocoo.org/docs/0.12/datastructures/#werkzeug.datastructures.MultiDict
input = request.form.to_dict()
app.logger.debug('Invoking function with input: %s' % input) app.logger.debug('Invoking function with input: %s' % input)
start = time.time() start = time.time()
try: try:
sys.path.insert(0, zip_file)
module = importlib.import_module(function_module)
func = getattr(module, function_method) func = getattr(module, function_method)
result = func(context=context, **input) result = func(context=context, **input)
except Exception as e: except Exception as e:
result = str(e) result = str(e)
duration = time.time() - start
# Print stacktrace
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
app.logger.debug(''.join(line for line in lines))
duration = time.time() - start
return Response( return Response(
response=json.dumps({'output': result, 'duration': duration}), response=json.dumps({'output': result, 'duration': duration}),
status=200, status=200,