from crontab import CronTab import logging def post(user=None, task_id=None): """ Stop a task. Parameters ---------- user: str User id task_id: str Task id Returns ------- result: result[0]: message result[1]: bool if it is stopped or not Notes ----- % in any of string make script falls. >>> import requests >>> import json >>> params = {"user": 'test', task_id:'88657y!'} >>> requests.post(url='/MG/update/stop', json=params) ['Your task has successfully stopped', True] """ 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 removed', False] elif len(jobs) == 0: result = ['Wrong task id. This task does not exists.', False] else: jobs[0].enable(False) user_cron.write() result = ['Your task has successfully stopped', True] return result