from crontab import CronTab import logging def post(user=None, task_id=None): """ Start a stopped task Parameters ---------- user: str User id task_id: str Task id Returns ------- result: list of str and bool result[0]: message result[1]: bool if it is started or not Notes ----- % in any of string make script falls. Examples -------- >>> import requests >>> import json >>> params = {"user": 'test', task_id:'88657y!'} >>> requests.post(url='/MG/update/start', json=params) ['Your task has successfully started', True] """ check_user = True # user = data['user'] user = 'roberto' user_taskid = "{}:{}".format(user, task_id) user_cron = CronTab(user=True) jobs = [job for job in user_cron.find_comment(user_taskid)] if len(jobs) > 1: logging.warning('There are two task with the same id, {}.'.format(task_id)) result = ['Your task could not be started', False] elif len(jobs) == 0: result = ['Wrong task id. This task does not exists.', False] else: jobs[0].enable(True) user_cron.write() result = ['Your task has successfully enabled', True] return result