def __init__(self, conf, logger=None): self.conf = conf self.logger = logger or get_logger(self.conf) self.backend = XcuteBackend(self.conf, logger=self.logger) url_map = Map([ Rule('/status', endpoint='status'), Submount('/v1.0/xcute', [ Rule('/job/list', endpoint='job_list', methods=['GET']), Rule('/job/create', endpoint='job_create', methods=['POST']), Rule('/job/show', endpoint='job_show', methods=['GET']), Rule('/job/pause', endpoint='job_pause', methods=['POST']), Rule('/job/resume', endpoint='job_resume', methods=['POST']), Rule('/job/delete', endpoint='job_delete', methods=['DELETE']), Rule('/lock/list', endpoint='lock_list', methods=['GET']), Rule('/lock/show', endpoint='lock_show', methods=['GET']), ]) ]) super(XcuteServer, self).__init__(url_map, logger)
def __init__(self, conf, logger=None): self.conf = conf self.logger = logger or get_logger(self.conf) self.backend = XcuteBackend(self.conf, logger=self.logger) self.conscience_client = ConscienceClient(self.conf) self.orchestrator_id = self.conf.get('orchestrator_id') if not self.orchestrator_id: raise ValueError('Missing orchestrator ID') self.logger.info('Using orchestrator ID: %s', self.orchestrator_id) self.beanstalkd_workers_tube = self.conf.get('beanstalkd_workers_tube') if not self.beanstalkd_workers_tube: raise ValueError('Missing beanstalkd workers tube') self.logger.info('Using beanstalkd workers tube: %s', self.beanstalkd_workers_tube) self.beanstalkd_reply_addr = self.conf.get('beanstalkd_reply_addr') if not self.beanstalkd_reply_addr: raise ValueError('Missing beanstalkd reply address') self.beanstalkd_reply_tube = self.conf.get( 'beanstalkd_reply_tube', self.beanstalkd_workers_tube + '.reply') self.logger.info('Using beanstalkd reply : %s %s', self.beanstalkd_reply_addr, self.beanstalkd_reply_tube) self.refresh_time_beanstalkd_workers = int_value( self.conf.get('refresh_time_beanstalkd_workers'), self.DEFAULT_REFRESH_TIME_BEANSTALKD_WORKERS) self.max_jobs_per_beanstalkd = int_value( self.conf.get('max_jobs_per_beanstalkd'), self.DEFAULT_MAX_JOBS_PER_BEANSTALKD) self.running = True self.beanstalkd_workers = dict() self.refresh_beanstalkd_workers_thread = None self.listen_beanstalkd_reply_thread = None self.dispatch_tasks_threads = dict() self.compute_total_tasks_threads = dict()
class XcuteServer(WerkzeugApp): def __init__(self, conf, logger=None): self.conf = conf self.logger = logger or get_logger(self.conf) self.backend = XcuteBackend(self.conf, logger=self.logger) url_map = Map([ Rule('/status', endpoint='status'), Submount('/v1.0/xcute', [ Rule('/job/list', endpoint='job_list', methods=['GET']), Rule('/job/create', endpoint='job_create', methods=['POST']), Rule('/job/show', endpoint='job_show', methods=['GET']), Rule('/job/pause', endpoint='job_pause', methods=['POST']), Rule('/job/resume', endpoint='job_resume', methods=['POST']), Rule('/job/delete', endpoint='job_delete', methods=['DELETE']), Rule('/lock/list', endpoint='lock_list', methods=['GET']), Rule('/lock/show', endpoint='lock_show', methods=['GET']), ]) ]) super(XcuteServer, self).__init__(url_map, logger) @handle_exceptions def on_status(self, req): status = self.backend.status() return Response(json.dumps(status), mimetype='application/json') @access_log @handle_exceptions def on_job_list(self, req): limit = int_value(req.args.get('limit'), None) marker = req.args.get('marker') job_infos = self.backend.list_jobs(limit=limit, marker=marker) return Response(json.dumps(job_infos), mimetype='application/json') @access_log @handle_exceptions def on_job_create(self, req): job_type = req.args.get('type') if not job_type: raise HTTPBadRequest('Missing job type') job_class = JOB_TYPES.get(job_type) if job_class is None: raise HTTPBadRequest('Unknown job type') job_config, lock = job_class.sanitize_config( json.loads(req.data or '{}')) job_id = self.backend.create(job_type, job_config, lock) job_info = self.backend.get_job_info(job_id) return Response(json.dumps(job_info), mimetype='application/json', status=202) def _get_job_id(self, req): """Fetch job ID from request query string.""" job_id = req.args.get('id') if not job_id: raise HTTPBadRequest('Missing job ID') return job_id @access_log @handle_exceptions def on_job_show(self, req): job_id = self._get_job_id(req) job_info = self.backend.get_job_info(job_id) return Response(json.dumps(job_info), mimetype='application/json') @access_log @handle_exceptions def on_job_pause(self, req): job_id = self._get_job_id(req) self.backend.request_pause(job_id) job_info = self.backend.get_job_info(job_id) return Response(json.dumps(job_info), mimetype='application/json', status=202) @access_log @handle_exceptions def on_job_resume(self, req): job_id = self._get_job_id(req) self.backend.resume(job_id) job_info = self.backend.get_job_info(job_id) return Response(json.dumps(job_info), mimetype='application/json', status=202) @access_log @handle_exceptions def on_job_delete(self, req): job_id = self._get_job_id(req) self.backend.delete(job_id) return Response(status=204) @access_log @handle_exceptions def on_lock_list(self, req): locks = self.backend.list_locks() return Response(json.dumps(locks), mimetype='application/json') @access_log @handle_exceptions def on_lock_show(self, req): lock = req.args.get('lock') if not lock: raise HTTPBadRequest('Missing lock') lock_info = self.backend.get_lock_info(lock) return Response(json.dumps(lock_info), mimetype='application/json')