1# Copyright (c) 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import json 5import logging 6import os 7import uuid 8import webapp2 9 10from google.appengine.api import taskqueue 11from perf_insights.endpoints.cloud_mapper import job_info 12 13MAX_JOB_TIMEOUT_IN_SECONDS = 600 14DEFAULT_JOB_TIMEOUT_IN_SECONDS = 120 15 16MAX_FUNCTION_TIMEOUT_IN_SECONDS = 120 17DEFAULT_FUNCTION_TIMEOUT_IN_SECONDS = 30 18 19class CreatePage(webapp2.RequestHandler): 20 21 def post(self): 22 self.response.headers['Content-Type'] = 'text/plain' 23 24 mapper = self.request.get('mapper') 25 reducer = self.request.get('reducer') 26 mapper_function = self.request.get('mapper_function') 27 reducer_function = self.request.get('reducer_function') 28 query = self.request.get('query') 29 corpus = self.request.get('corpus') 30 revision = self.request.get('revision') 31 if not revision: 32 revision = 'HEAD' 33 timeout = self.request.get('timeout') 34 if not timeout: 35 timeout = DEFAULT_JOB_TIMEOUT_IN_SECONDS 36 else: 37 timeout = int(timeout) 38 function_timeout = self.request.get('function_timeout') 39 if not function_timeout: 40 function_timeout = DEFAULT_FUNCTION_TIMEOUT_IN_SECONDS 41 else: 42 function_timeout = int(function_timeout) 43 timeout = max(0, min( 44 timeout, MAX_JOB_TIMEOUT_IN_SECONDS)) 45 function_timeout = max(0, min( 46 function_timeout, MAX_FUNCTION_TIMEOUT_IN_SECONDS)) 47 48 job_uuid = str(uuid.uuid4()) 49 logging.info('Creating new job %s' % job_uuid) 50 job = job_info.JobInfo(id=job_uuid) 51 job.remote_addr = os.environ["REMOTE_ADDR"] 52 job.status = 'QUEUED' 53 job.mapper = mapper 54 job.reducer = reducer 55 job.mapper_function = mapper_function 56 job.reducer_function = reducer_function 57 job.query = query 58 job.corpus = corpus 59 job.revision = revision 60 job.running_tasks = [] 61 job.timeout = timeout 62 job.function_timeout = function_timeout 63 job.put() 64 65 response = { 66 'status': True, 67 'jobid': job_uuid 68 } 69 70 self.response.out.write(json.dumps(response)) 71 72 payload = {'jobid': job_uuid, 'type': 'create'} 73 taskqueue.add(url='/cloud_mapper/task', name=job_uuid, params=payload) 74 75 76app = webapp2.WSGIApplication([('/cloud_mapper/create', CreatePage)]) 77