def task_delete(task_id): """ Remove task from schedule :param task_id: :return: """ import re import json from akrr import akrrrestclient results = akrrrestclient.delete('/scheduled_tasks/{}'.format(task_id)) if results.status_code == 200 and hasattr(results, "text"): log.debug('Message from AKRR server: %s', results.text) response = json.loads(results.text) if "data" in response and "success" in response[ "data"] and "message" in response["data"]: if response["data"]["success"]: log.info('Successfully deleted task with id: %s' % task_id) else: log.error('Can not deleted task with id: %s because %s' % (task_id, response["data"]["message"])) if re.search("is not in queue", response["data"]["message"]) is None: raise AkrrRestAPIException() else: raise AkrrRestAPIException( 'Can not deleted task with id: %s, got following message: %s' % (task_id, results.text)) else: if hasattr(results, "text"): msg = "Can not delete task. Message from AKRR server: " + results.text log.error(msg) raise AkrrRestAPIException(msg) else: raise AkrrRestAPIException()
def task_delete_selection(resource: str = None, appkernel: str = None, nodes: str = None, group_id: str = None, active_tasks=False, scheduled_tasks=False): """ delete tasks from schedule """ from akrr import akrrrestclient import json from akrr.db import get_akrr_db from akrr.daemon import delete_task import time if not (resource or appkernel or nodes or group_id): raise AkrrValueException("Something out of resource/appkernel/nodes/group id should be set!") db, cur = get_akrr_db(dict_cursor=True) # ask scheduler not to start new tasks if akrrrestclient.post('/scheduler/no_new_tasks').status_code != 200: raise AkrrRestAPIException("Can not post scheduler/no_new_tasks") if active_tasks: # Now we need to wait till scheduler will be done checking active tasks while True: sql = "SELECT task_id FROM active_tasks WHERE task_lock > 0" log.debug(sql) cur.execute(sql) n_active_checking_task = len(cur.fetchall()) if n_active_checking_task==0: break log.info("There are %d task which daemon is actively working on, waiting for it to pause.", n_active_checking_task) time.sleep(5) # now daemon is not working on any tasks # now we can work with db where = [] if resource: where.append("resource='%s'" % resource) if appkernel: appkernel_list = ["'" + ak.strip() + "'" for ak in appkernel.split(',')] if ',' in appkernel else ["'" + appkernel + "'"] where.append("app IN (" + ",".join(appkernel_list) + ")") if group_id: where.append("group_id='%s'" % group_id) active_tasks_ids = [] if nodes: node_list = [int(node.strip()) for node in nodes.split(',')] if ',' in nodes else [int(nodes)] for node in node_list: where_node1 = where + ["resource_param LIKE \"%'nnodes':"+str(node)+"}%\""] where_node2 = where + ["resource_param LIKE \"%'nnodes':"+str(node)+",%\""] for where_node in [where_node1, where_node2]: if scheduled_tasks: sql = "DELETE FROM scheduled_tasks WHERE " + " AND ".join(where_node) log.debug(sql) cur.execute(sql) if active_tasks: sql = "SELECT task_id FROM active_tasks WHERE " + " AND ".join(where_node) log.debug(sql) cur.execute(sql) active_tasks_ids += [int(t['task_id']) for t in cur.fetchall()] else: if scheduled_tasks: sql = "DELETE FROM scheduled_tasks WHERE " + " AND ".join(where) log.debug(sql) cur.execute(sql) if active_tasks: sql = "SELECT task_id FROM active_tasks WHERE " + " AND ".join(where) log.debug(sql) cur.execute(sql) active_tasks_ids += [int(t['task_id']) for t in cur.fetchall()] if active_tasks: if len(active_tasks_ids)==0: log.info("No active tasks to delete") else: for task_id in active_tasks_ids: log.info("Deleting task_id %d", task_id) delete_task(task_id, remove_from_scheduled_queue=False, remove_from_active_queue=True, remove_derived_task=False) if scheduled_tasks or active_tasks: db.commit() # ask scheduler can start new tasks now if akrrrestclient.post('/scheduler/new_tasks_on').status_code != 200: raise AkrrRestAPIException("Can not post scheduler/new_tasks_on") log.info("Done")
def task_list(resource=None, appkernel=None, scheduled=True, active=True): """ Retrieve the list of currently scheduled tasks ( resource / application pairings ) from mod_akrr. :param resource: filter the results by the provided resource :param appkernel: filter the results by the provided application :param scheduled: show only scheduled for future execution tasks :param active: show only currently running tasks :type resource str or None :type appkernel str or None :return: None """ import json from . import akrrrestclient scheduled_table_parameters = OrderedDict( (("Res. Param", ("resource_param", "l", lambda s: wrap_str_dict(s, width=48))), ("App Param", ("app_param", "l", lambda s: wrap_str_dict(s, width=48))), ("Task Param", ("task_param", "l", lambda s: wrap_str_dict(s, width=48))), ('Repeat In', ('repeat_in', "c", str)))) scheduled_table_title_key = OrderedDict(( ("Task Id", ('task_id', "r", str)), ('Resource', ('resource', "l", lambda s: wrap_text(s, width=24))), ('App', ('app', "l", str)), ("Parameters", (scheduled_table_parameters, "l", pack_details)), ("Time to Start", ('time_to_start', "l", str)), )) active_table_details = OrderedDict( (('Resource', ('resource', "l", lambda s: wrap_text(s, width=48))), ('App', ('app', "l", str)), ("Resource Param", ("resource_param", "l", lambda s: wrap_str_dict(s, width=48))), ("App Param", ("app_param", "l", lambda s: wrap_str_dict(s, width=48))), ("Task Param", ("task_param", "l", lambda s: wrap_str_dict(s, width=48))), ("Time to Start", ('time_to_start', "l", str)), ('Repeat In', ('repeat_in', "c", str)))) active_table_title_key = OrderedDict(( ("Task Id", ('task_id', "r", str)), ("Details", (active_table_details, "l", pack_details)), ('Status', ('status', "l", lambda s: wrap_text(s, width=48))), )) log.debug("List all tasks") data = {} if resource is not None: data["resource"] = resource if appkernel is not None: data["app"] = appkernel if scheduled: results = akrrrestclient.get('/scheduled_tasks', data=data) if results.status_code == 200: log.debug('Successfully Completed Task Retrieval.\n%s', results.text) else: if hasattr(results, "text"): response = json.loads(results.text) if "error" in response and "message" in response["error"]: msg = "Message from AKRR server: " + response["error"][ "message"] log.error(msg) raise AkrrRestAPIException(msg) else: raise AkrrRestAPIException() results = json.loads(results.text)['data'] if len(results) > 0: table = get_task_list_table(results, scheduled_table_title_key) log.info('Scheduled tasks:\n' + str(table)) else: log.info('There is no scheduled tasks') if active: results = akrrrestclient.get('/active_tasks', data=data) if results.status_code == 200: log.debug('Successfully Completed Task Retrieval.\n%s', results.text) else: if hasattr(results, "text"): response = json.loads(results.text) if "error" in response and "message" in response["error"]: msg = "Message from AKRR server: " + response["error"][ "message"] log.error(msg) raise AkrrRestAPIException(msg) else: raise AkrrRestAPIException() results = json.loads(results.text)['data'] if len(results) > 0: table = get_task_list_table(results, active_table_title_key) log.info('Active tasks:\n' + str(table)) else: log.info('There is no active tasks')
def task_list(resource=None, appkernel=None, scheduled=True, active=True): """ Retrieve the list of currently scheduled tasks ( resource / application pairings ) from mod_akrr. :param resource: filter the results by the provided resource :param appkernel: filter the results by the provided application :param scheduled: show only scheduled for future execution tasks :param active: show only currently running tasks :type resource str or None :type appkernel str or None :return: None """ import json from . import akrrrestclient log.debug("List all tasks") data = {} if resource is not None: data["resource"] = resource if appkernel is not None: data["app"] = appkernel if scheduled: results = akrrrestclient.get('/scheduled_tasks', data=data) if results.status_code == 200: log.debug('Successfully Completed Task Retrieval.\n%s', results.text) else: if hasattr(results, "text"): response = json.loads(results.text) if "error" in response and "message" in response["error"]: msg = "Message from AKRR server: " + response["error"][ "message"] log.error(msg) raise AkrrRestAPIException(msg) else: raise AkrrRestAPIException() results = json.loads(results.text)['data'] if len(results) > 0: msg = 'Scheduled tasks:\n' msg = msg + "%10s%16s%32s%8s%20s%20s\n" % ( 'task_id', 'resource', 'app', 'nnodes', 'time_to_start', 'repeat_in') for r in results: if r is None: r = {} nodes = eval(r.get('resource_param', "{}")).get("nnodes", "NA") msg = msg + "%10s%16s%32s%8s%20s%20s\n" % ( r.get('task_id', "NA"), r.get('resource', "NA"), r.get('app', "NA"), nodes, r.get( 'time_to_start', "NA"), r.get('repeat_in', "NA")) log.info(msg) else: log.info('There is no scheduled tasks') if active: results = akrrrestclient.get('/active_tasks', data=data) if results.status_code == 200: log.debug('Successfully Completed Task Retrieval.\n%s', results.text) else: if hasattr(results, "text"): response = json.loads(results.text) if "error" in response and "message" in response["error"]: msg = "Message from AKRR server: " + response["error"][ "message"] log.error(msg) raise AkrrRestAPIException(msg) else: raise AkrrRestAPIException() results = json.loads(results.text)['data'] if len(results) > 0: msg = 'Active tasks:\n' msg = msg + "%10s%16s%32s%8s%20s%20s%22s\n" % ( 'task_id', 'resource', 'app', 'nnodes', 'time_to_start', 'repeat_in', 'status') for r in results: if r is None: r = {} nodes = eval(r.get('resource_param', "{}")).get("nnodes", "NA") if r.get('status', "NA") is None: status = "NA" else: status = r.get('status', "NA") msg = msg + "%10s%16s%32s%8s%20s%20s%22s\n" % ( r.get('task_id', "NA"), r.get('resource', "NA"), r.get('app', "NA"), nodes, r.get('time_to_start', "NA"), r.get('repeat_in', "NA"), status[:19]) log.info(msg) else: log.info('There is no active tasks')