From feae3062708c6853c812d9a35603d717d37864cc Mon Sep 17 00:00:00 2001 From: Guannan Ma Date: Thu, 18 Jun 2020 15:08:54 +0800 Subject: [PATCH] add timeplus and CronTask in services.executor --- cup/services/executor.py | 27 ++++++++++++++++-- cup/timeplus.py | 60 +++++++++++++++++++++++++++++++++++++++- cup/version.py | 2 +- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/cup/services/executor.py b/cup/services/executor.py index 751bb2d..0c76a10 100644 --- a/cup/services/executor.py +++ b/cup/services/executor.py @@ -241,7 +241,8 @@ class CronTask(object): _GEN = generator.CachedUUID() def __init__( - self, name, pytz_timezone, timer_dict, function, *args, **kwargs + self, name, pytz_timezone, timer_dict, md5uuid, + function, *args, **kwargs ): """ :param pytz_timezone: @@ -284,7 +285,11 @@ def __init__( self._timer_params = self._generate_timer_params(self._timer_dict) self._check_param_valids(self._timer_params) self._lastsched_time = None - self._md5_id = self._GEN.get_uuid()[0] + if md5uuid is None: + self._md5_id = self._GEN.get_uuid()[0] + else: + self._md5_id = md5uuid + self._timer = None def get_funcargs(self): """return (function, args, kwargs)""" @@ -521,6 +526,14 @@ def __repr__(self): """return info of the crontask""" return 'CronTask(ID:{0} Name:{1})'.format(self.taskid(), self._name) + def set_timer(self, timer): + """set timer to this crontask""" + self._timer = timer + + def get_timer(self): + """return timer of the crontask""" + return self._timer + class CronExecution(ExecutionService): """ @@ -613,6 +626,7 @@ def delay_exec(self, delay_time_insec, self._do_delay_exe, [task_data] ) + crontask.set_timer(timer) timer.start() def schedule(self, crontask): @@ -656,4 +670,13 @@ def schedule(self, crontask): ) self._task_dict[crontask.taskid()] = crontask + def calcel_delay_exec(self, taskid): + """calcel delayexec by taskid""" + task = self._task_dict.get(taskid, None) + if task is None: + log.warn('delay exec task id {0} not found'.format(taskid)) + return + timer = task.get_timer() + timer.cancel() + # vi:set tw=0 ts=4 sw=4 nowrap fdm=indent diff --git a/cup/timeplus.py b/cup/timeplus.py index e033904..b8dd75b 100644 --- a/cup/timeplus.py +++ b/cup/timeplus.py @@ -8,8 +8,12 @@ """ from __future__ import print_function import time +import datetime -__all__ = ['get_str_now'] +import pytz + + +__all__ = ['get_str_now', 'TimePlus'] def get_str_now(fmt='%Y-%m-%d-%H-%M-%S'): @@ -22,5 +26,59 @@ def get_str_now(fmt='%Y-%m-%d-%H-%M-%S'): return str(time.strftime(fmt, time.localtime())) +class TimePlus(object): + """arrow time""" + def __init__(self, timezone): + """ + initialize with timezone setup + """ + if not isinstance(timezone, pytz.BaseTzInfo): + raise ValueError('not a object of pytz.timezone("xxx/xxx")') + self._timezone = timezone + self._utc_tz = pytz.timezone('UTC') + + def get_timezone(self): + """ + return current pytz timezone object + """ + return self._timezone + + def set_newtimezone(self, pytz_timezone): + """ + refresh timezone + + :return: + True if refreshing is done. False otherwise + """ + self._timezone = pytz_timezone + + @classmethod + def utc_now(cls): + """return utc_now""" + return datetime.datetime.now(pytz.UTC) + + def local2utc(self, dateobj): + """ + local timezone to utc conversion + + :return: + a datetime.datetime object with utc timezone enabled + + :raise: + ValueError if dateobj is not a datetime.datetime object + """ + if not isinstance(dateobj, datetime.datetime): + raise ValueError('dateobj is not a datetime.datetime') + return dateobj.astimezone(self._utc_tz) + + def utc2local(self, dateobj): + """ + utc datetime to local timezone datetime.datetime + """ + if not isinstance(dateobj, datetime.datetime): + raise ValueError('dateobj is not a datetime.datetime') + return dateobj.astimezone(self._timezone) + + if __name__ == '__main__': print(get_str_now()) diff --git a/cup/version.py b/cup/version.py index 2a1d6b7..3eb6d79 100644 --- a/cup/version.py +++ b/cup/version.py @@ -13,7 +13,7 @@ ] -VERSION = '3.2.16' +VERSION = '3.2.18' AUTHOR = 'CUP-DEV Team' # vi:set tw=0 ts=4 sw=4 nowrap fdm=indent