from crontab import CronTab import re from ..calls.create import Task import logging def post(user=None, task_id=None): """ Task information Parameters ---------- user: str Task user task_id: str Task id Returns ------- result: str or dict If task does not exist return error. If task exists return a dict with some info. Dict keys 'is_enabled', 'user_taskid', 'command', 'description', 'last_run' Notes ----- % in any of string make script falls. Examples -------- >>> import requests >>> import json >>> params = {"user": 'test', "task_id": '8485#&'} >>> requests.post(url='/MG/routines/info', json=params) {'is_enabled': True, 'user_taskid: 'test#8485#&', 'command': geomicroservice, 'description': description, 'last_run': last_run_date} """ user_cron = CronTab(user=True) result = {} jobs = [job for job in user_cron.find_comment(r'{}:{}'.format(user, task_id))] if len(jobs) > 1: logging.warning('There are two task with the same id, {}.'.format(task_id)) result = 'There are two task with the same id.' elif len(jobs) == 0: result = 'Wrong task id. This task does not exists.' else: job = jobs[0] result['is_enabled'] = job.is_enabled() result['user_taskid'] = job.comment result['command'] = job.command result['description'] = job.description() result['last_run'] = Task.get_last_run(user, task_id) return result