def post(self, actor_id): """Ensure a certain number of workers are running for an actor""" id = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[id]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) args = self.validate_post() num = args.get('num') if not num or num == 0: num = 1 dbid = Actor.get_dbid(g.tenant, actor_id) workers = Worker.get_workers(dbid) if len(workers.items()) < num: worker_ids = [] num_to_add = int(num) - len(workers.items()) for idx in range(num_to_add): worker_ids.append(Worker.request_worker(actor_id)) ch = CommandChannel() ch.put_cmd(actor_id=actor.db_id, worker_ids=worker_ids, image=actor.image, tenant=g.tenant, num=num_to_add, stop_existing=False) return ok( result=None, msg="Scheduled {} new worker(s) to start. There were only". format(num_to_add)) else: return ok(result=None, msg="Actor {} already had {} worker(s).".format( actor_id, num))
def post(self, actor_id): def get_hypermedia(actor, exc): return { '_links': { 'self': '{}/actors/v2/{}/executions/{}'.format( actor.api_server, actor.id, exc), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), 'messages': '{}/actors/v2/{}/messages'.format(actor.api_server, actor.id) }, } args = self.validate_post() d = {} # build a dictionary of k:v pairs from the query parameters, and pass a single # additional object 'message' from within the post payload. Note that 'message' # need not be JSON data. for k, v in request.args.items(): if k == 'message': continue d[k] = v if hasattr(g, 'user'): d['_abaco_username'] = g.user if hasattr(g, 'api_server'): d['_abaco_api_server'] = g.api_server # if hasattr(g, 'jwt'): # d['_abaco_jwt'] = g.jwt # if hasattr(g, 'jwt_server'): # d['_abaco_jwt_server'] = g.jwt_server if hasattr(g, 'jwt_header_name'): d['_abaco_jwt_header_name'] = g.jwt_header_name dbid = Actor.get_dbid(g.tenant, actor_id) # create an execution exc = Execution.add_execution( dbid, { 'cpu': 0, 'io': 0, 'runtime': 0, 'status': SUBMITTED, 'executor': g.user }) d['_abaco_execution_id'] = exc d['_abaco_Content-Type'] = args.get('_abaco_Content-Type', '') ch = ActorMsgChannel(actor_id=dbid) ch.put_msg(message=args['message'], d=d) # make sure at least one worker is available actor = Actor.from_db(actors_store[dbid]) actor.ensure_one_worker() result = {'execution_id': exc, 'msg': args['message']} result.update(get_hypermedia(actor, exc)) case = Config.get('web', 'case') if not case == 'camel': return ok(result) else: return ok(dict_to_camel(result))
def post(self, actor_id): def get_hypermedia(actor, exc): return {'_links': {'self': '{}/actors/v2/{}/executions/{}'.format(actor.api_server, actor.id, exc), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), 'messages': '{}/actors/v2/{}/messages'.format(actor.api_server, actor.id)},} args = self.validate_post() d = {} # build a dictionary of k:v pairs from the query parameters, and pass a single # additional object 'message' from within the post payload. Note that 'message' # need not be JSON data. for k, v in request.args.items(): if k == 'message': continue d[k] = v if hasattr(g, 'user'): d['_abaco_username'] = g.user if hasattr(g, 'api_server'): d['_abaco_api_server'] = g.api_server # if hasattr(g, 'jwt'): # d['_abaco_jwt'] = g.jwt # if hasattr(g, 'jwt_server'): # d['_abaco_jwt_server'] = g.jwt_server if hasattr(g, 'jwt_header_name'): d['_abaco_jwt_header_name'] = g.jwt_header_name dbid = Actor.get_dbid(g.tenant, actor_id) # create an execution exc = Execution.add_execution(dbid, {'cpu': 0, 'io': 0, 'runtime': 0, 'status': SUBMITTED, 'executor': g.user}) d['_abaco_execution_id'] = exc d['_abaco_Content-Type'] = args.get('_abaco_Content-Type', '') ch = ActorMsgChannel(actor_id=dbid) ch.put_msg(message=args['message'], d=d) # make sure at least one worker is available workers = Worker.get_workers(dbid) actor = Actor.from_db(actors_store[dbid]) if len(workers.items()) < 1: ch = CommandChannel() ch.put_cmd(actor_id=dbid, image=actor.image, tenant=g.tenant, num=1, stop_existing=False) result={'execution_id': exc, 'msg': args['message']} result.update(get_hypermedia(actor, exc)) case = Config.get('web', 'case') if not case == 'camel': return ok(result) else: return ok(dict_to_camel(result))
def get(self, actor_id): try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) return ok(result=actor, msg="Actor retrieved successfully.")
def put(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) previous_image = actor.image args = self.validate_put(actor) args['tenant'] = g.tenant update_image = False if args['image'] == previous_image: args['status'] = actor.status else: update_image = True args['status'] = SUBMITTED args['api_server'] = g.api_server args['owner'] = g.user actor = Actor(**args) actors_store[actor.db_id] = actor.to_db() if update_image: ch = CommandChannel() ch.put_cmd(actor_id=actor.db_id, image=actor.image, tenant=args['tenant']) # return ok(result={'update_image': str(update_image)}, # msg="Actor updated successfully.") return ok(result=actor.display(), msg="Actor updated successfully.")
def put(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) previous_image = actor.image args = self.validate_put(actor) args['tenant'] = g.tenant update_image = False if args['image'] == previous_image: args['status'] = actor.status else: update_image = True args['status'] = SUBMITTED args['api_server'] = g.api_server args['owner'] = g.user actor = Actor(**args) actors_store[actor.db_id] = actor.to_db() worker_ids = Worker.request_worker(actor.db_id) if update_image: ch = CommandChannel() ch.put_cmd(actor_id=actor.db_id, worker_ids=worker_ids, image=actor.image, tenant=args['tenant']) # return ok(result={'update_image': str(update_image)}, # msg="Actor updated successfully.") return ok(result=actor.display(), msg="Actor updated successfully.")
def get(self, actor_id, execution_id): def get_hypermedia(actor, exc): return {'_links': {'self': '{}/actors/v2/{}/executions/{}/logs'.format(actor.api_server, actor.id, exc.id), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), 'execution': '{}/actors/v2/{}/executions/{}'.format(actor.api_server, actor.id, exc.id)}, } dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) try: excs = executions_store[dbid] except KeyError: raise APIException("No executions found for actor {}.".format(actor_id)) try: exc = Execution.from_db(excs[execution_id]) except KeyError: raise APIException("Execution not found {}.".format(execution_id)) try: logs = logs_store[execution_id] except KeyError: logs = "" result={'logs': logs} result.update(get_hypermedia(actor, exc)) return ok(result, msg="Logs retrieved successfully.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: summary = ExecutionsSummary(db_id=dbid) except DAOError as e: raise APIException("actor not found: {}. DAOError: {}'".format(actor_id, e), 404) return ok(result=summary.display(), msg="Actor executions retrieved successfully.")
def delete(self, actor_id, worker_id): id = Actor.get_dbid(g.tenant, actor_id) try: worker = Worker.get_worker(id, worker_id) except WorkerException as e: raise ResourceError(e.msg, 404) shutdown_worker(worker['ch_name']) return ok(result=None, msg="Worker scheduled to be stopped.")
def delete(self, actor_id, ch_name): id = Actor.get_dbid(g.tenant, actor_id) try: worker = Worker.get_worker(id, ch_name) except WorkerException as e: raise APIException(e.msg, 404) shutdown_worker(ch_name) return ok(result=None, msg="Worker scheduled to be stopped.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise APIException( "actor not found: {}. db_id:{}'".format(actor_id, dbid), 404) return ok(result=actor.display(), msg="Actor retrieved successfully.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise ResourceError( "actor not found: {}. db_id:{}'".format(actor_id, dbid), 404) return ok(result=actor.display(), msg="Actor retrieved successfully.")
def delete(self, actor_id, ch_name): try: worker = get_worker(actor_id, ch_name) except WorkerException as e: raise APIException(e.message, 404) ch = WorkerChannel(name=ch_name) ch.put("stop") return ok(result=worker, msg="Worker scheduled to be stopped.")
def get(self, actor_id): try: actor = Actor.from_db(actors_store[actor_id]) subscriptions = actor.get('subscriptions') or {'subscriptions': None} except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) return ok(result=subscriptions, msg="Subscriptions retrieved successfully.")
def post(self, actor_id): try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) args = self.validate_post() Execution.add_execution(actor_id, args) return ok(result=actor, msg="Actor execution added successfully.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: summary = ExecutionsSummary(db_id=dbid) except DAOError as e: raise ResourceError( "actor not found: {}. DAOError: {}'".format(actor_id, e), 404) return ok(result=summary.display(), msg="Actor executions retrieved successfully.")
def post(self, actor_id): id = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[id]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) args = self.validate_post() Execution.add_execution(id, args) return ok(result=actor.display(), msg="Actor execution added successfully.")
def get(self, actor_id): # check that actor exists try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) # TODO # retrieve pending messages from the queue return ok(result={'messages': []})
def post(self, actor_id): args = self.validate_post() d = {} for k, v in request.args.items(): if k == 'message': continue d[k] = v ch = ActorMsgChannel(actor_id=actor_id) ch.put_msg(msg=args['message'], d=d) return ok(result={'msg': args['message']})
def get(self, actor_id, ch_name): try: Actor.from_db(actors_store[actor_id]) except KeyError: raise WorkerException("actor not found: {}'".format(actor_id)) try: worker = get_worker(actor_id, ch_name) except WorkerException as e: raise APIException(e.message, 404) return ok(result=worker, msg="Worker retrieved successfully.")
def post(self, actor_id): args = self.validate_post() state = args['state'] try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) actor.state = state actors_store[actor_id] = actor.to_db() return ok(result=actor, msg="State updated successfully.")
def get(self, actor_id, worker_id): id = Actor.get_dbid(g.tenant, actor_id) try: Actor.from_db(actors_store[id]) except KeyError: raise WorkerException("actor not found: {}'".format(actor_id)) try: worker = Worker.get_worker(id, worker_id) except WorkerException as e: raise ResourceError(e.msg, 404) return ok(result=worker, msg="Worker retrieved successfully.")
def post(self): args = self.validate_post() args['executions'] = {} args['state'] = '' args['subscriptions'] = [] args['status'] = SUBMITTED actor = Actor(args) actors_store[actor.id] = actor.to_db() ch = CommandChannel() ch.put_cmd(actor_id=actor.id, image=actor.image) return ok(result=actor, msg="Actor created successfully.")
def post(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) args = self.validate_post() state = args['state'] try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) actors_store.update(dbid, 'state', state) return ok(result=actor.display(), msg="State updated successfully.")
def post(self, actor_id): """Add new permissions for an actor""" try: Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) args = self.validate_post() add_permission(args['user'], actor_id, args['level']) permissions = get_permissions(actor_id) return ok(result=permissions, msg="Permission added successfully.")
def get(self, actor_id): try: Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) try: permissions = get_permissions(actor_id) except PermissionsException as e: raise APIException(e.message, 404) return ok(result=permissions, msg="Permissions retrieved successfully.")
def post(self): args = self.validate_post() args['tenant'] = g.tenant args['api_server'] = g.api_server args['owner'] = g.user actor = Actor(**args) actors_store[actor.db_id] = actor.to_db() ch = CommandChannel() ch.put_cmd(actor_id=actor.db_id, image=actor.image, tenant=args['tenant']) add_permission(g.user, actor.db_id, 'UPDATE') return ok(result=actor.display(), msg="Actor created successfully.", request=request)
def post(self, actor_id): """Add new permissions for an actor""" id = Actor.get_dbid(g.tenant, actor_id) try: Actor.from_db(actors_store[id]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) args = self.validate_post() add_permission(args['user'], id, args['level']) permissions = get_permissions(id) return ok(result=permissions, msg="Permission added successfully.")
def get(self, actor_id): id = Actor.get_dbid(g.tenant, actor_id) try: Actor.from_db(actors_store[id]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) try: permissions = get_permissions(id) except PermissionsException as e: raise ResourceError(e.msg, 404) return ok(result=permissions, msg="Permissions retrieved successfully.")
def delete(self, actor_id): id = Actor.get_dbid(g.tenant, actor_id) shutdown_workers(id) try: actor = Actor.from_db(actors_store[id]) executions = actor.get('executions') or {} for ex_id, val in executions.items(): del logs_store[ex_id] except KeyError: print("Did not find actor with id: {}".format(id)) del actors_store[id] del permissions_store[id] return ok(result=None, msg='Actor deleted successfully.')
def post(self, actor_id): """Start new workers for an actor""" try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException("actor not found: {}'".format(actor_id), 404) args = self.validate_post() num = args.get("num") if not num or num == 0: num = 1 ch = CommandChannel() ch.put_cmd(actor_id=actor.id, image=actor.image, num=num, stop_existing=False) return ok(result=None, msg="Scheduled {} new worker(s) to start.".format(str(num)))
def post(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) if actor.stateless: raise ResourceError("actor is stateless.", 404) args = self.validate_post() state = args['state'] actors_store.update(dbid, 'state', state) actor = Actor.from_db(actors_store[dbid]) return ok(result=actor.display(), msg="State updated successfully.")
def get(self, actor_id): try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) tot = {'total_executions': 0, 'total_cpu': 0, 'total_io':0, 'total_runtime': 0, 'ids':[]} executions = actor.get('executions') or {} for id, val in executions.items(): tot['ids'].append(id) tot['total_executions'] += 1 tot['total_cpu'] += int(val['cpu']) tot['total_io'] += int(val['io']) tot['total_runtime'] += int(val['runtime']) return ok(result=tot, msg="Actor executions retrieved successfully.")
def get(self, actor_id, execution_id): try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) try: excs = actor.executions except KeyError: raise APIException("No executions found for actor {}.".format(actor_id)) try: logs = logs_store[execution_id] except KeyError: logs = "" return ok(result=logs, msg="Logs retrieved successfully.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: Actor.from_db(actors_store[dbid]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 400) try: workers = Worker.get_workers(dbid) except WorkerException as e: raise ResourceError(e.msg, 404) result = [] for id, worker in workers.items(): worker.update({'id': id}) result.append(worker) return ok(result=result, msg="Workers retrieved successfully.")
def get(self, actor_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: Actor.from_db(actors_store[dbid]) except KeyError: raise APIException("actor not found: {}'".format(actor_id), 400) try: workers = Worker.get_workers(dbid) except WorkerException as e: raise APIException(e.msg, 404) result = [] for id, worker in workers.items(): worker.update({'id': id}) result.append(worker) return ok(result=result, msg="Workers retrieved successfully.")
def get(self, actor_id): def get_hypermedia(actor): return {'_links': {'self': '{}/actors/v2/{}/messages'.format(actor.api_server, actor.id), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), }, } # check that actor exists id = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) result={'messages': len(ActorMsgChannel(actor_id=id)._queue._queue)} result.update(get_hypermedia(actor)) return ok(result)
def get(self, actor_id, execution_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actors_store[dbid] except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) try: excs = executions_store[dbid] except KeyError: raise APIException("No executions found for actor {}.".format(actor_id)) try: exc = Execution.from_db(excs[execution_id]) except KeyError: raise APIException("Execution not found {}.".format(execution_id)) return ok(result=exc.display(), msg="Actor execution retrieved successfully.")
def get(self, actor_id, execution_id): dbid = Actor.get_dbid(g.tenant, actor_id) try: actors_store[dbid] except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) try: excs = executions_store[dbid] except KeyError: raise ResourceError( "No executions found for actor {}.".format(actor_id)) try: exc = Execution.from_db(excs[execution_id]) except KeyError: raise ResourceError("Execution not found {}.".format(execution_id)) return ok(result=exc.display(), msg="Actor execution retrieved successfully.")
def post(self): args = self.validate_post() args['tenant'] = g.tenant args['api_server'] = g.api_server args['owner'] = g.user actor = Actor(**args) actors_store[actor.db_id] = actor.to_db() actor.ensure_one_worker() # worker_id = Worker.request_worker(actor_id=actor.db_id) # ch = CommandChannel() # ch.put_cmd(actor_id=actor.db_id, # worker_ids=[worker_id], # image=actor.image, # tenant=args['tenant'], # stop_existing=False) add_permission(g.user, actor.db_id, 'UPDATE') return ok(result=actor.display(), msg="Actor created successfully.", request=request)
def post(self, actor_id): args = self.validate_post() try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) event_ids = args['event_id'] event_patterns = args['event_pattern'] subs = {} for id in event_ids: s = Subscription(actor, {'event_id': id}) subs[s.id] = s actor.subscriptions = subs for pat in event_patterns: s = Subscription(actor, {'event_pattern': pat}) subs[s.id] = s actor.subscriptions = subs actors_store[actor_id] = actor.to_db() return ok(result=actor, msg="Subscriptions updated successfully.")
def get(self, actor_id): def get_hypermedia(actor): return { '_links': { 'self': '{}/actors/v2/{}/messages'.format(actor.api_server, actor.id), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), }, } # check that actor exists id = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[id]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) result = {'messages': len(ActorMsgChannel(actor_id=id)._queue._queue)} result.update(get_hypermedia(actor)) return ok(result)
def put(self, actor_id): try: actor = Actor.from_db(actors_store[actor_id]) except KeyError: raise APIException( "actor not found: {}'".format(actor_id), 404) args = self.validate_put() update_image = False args['name'] = actor['name'] args['id'] = actor['id'] args['executions'] = actor['executions'] args['state'] = actor['state'] if args['image'] == actor.image: args['status'] = actor.status else: update_image = True args['status'] = SUBMITTED actor = Actor(args) actors_store[actor.id] = actor.to_db() if update_image: ch = CommandChannel() ch.put_cmd(actor_id=actor.id, image=actor.image) return ok(result=actor, msg="Actor updated successfully.")
def get(self, actor_id, execution_id): def get_hypermedia(actor, exc): return { '_links': { 'self': '{}/actors/v2/{}/executions/{}/logs'.format( actor.api_server, actor.id, exc.id), 'owner': '{}/profiles/v2/{}'.format(actor.api_server, actor.owner), 'execution': '{}/actors/v2/{}/executions/{}'.format( actor.api_server, actor.id, exc.id) }, } dbid = Actor.get_dbid(g.tenant, actor_id) try: actor = Actor.from_db(actors_store[dbid]) except KeyError: raise ResourceError("actor not found: {}'".format(actor_id), 404) try: excs = executions_store[dbid] except KeyError: raise ResourceError( "No executions found for actor {}.".format(actor_id)) try: exc = Execution.from_db(excs[execution_id]) except KeyError: raise ResourceError("Execution not found {}.".format(execution_id)) try: logs = logs_store[execution_id] except KeyError: logs = "" result = {'logs': logs} result.update(get_hypermedia(actor, exc)) return ok(result, msg="Logs retrieved successfully.")
def get(self): actors = [] for k, v in actors_store.items(): if v['tenant'] == g.tenant: actors.append(Actor.from_db(v).display()) return ok(result=actors, msg="Actors retrieved successfully.")
def delete(self, actor_id): shutdown_workers(actor_id) del actors_store[actor_id] return ok(result=None, msg='Actor delete successfully.')
def get(self): return ok(result=[json.loads(actor[1]) for actor in actors_store.items()], msg="Actors retrieved successfully.")