Skip to content

Commit

Permalink
add timeplus and CronTask in services.executor
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmgn committed Jun 18, 2020
1 parent 90ef7dc commit feae306
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
27 changes: 25 additions & 2 deletions cup/services/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
60 changes: 59 additions & 1 deletion cup/timeplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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())
2 changes: 1 addition & 1 deletion cup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit feae306

Please sign in to comment.