close
Skip to content

Commit feae306

Browse files
committed
add timeplus and CronTask in services.executor
1 parent 90ef7dc commit feae306

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎cup/services/executor.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class CronTask(object):
241241
_GEN = generator.CachedUUID()
242242

243243
def __init__(
244-
self, name, pytz_timezone, timer_dict, function, *args, **kwargs
244+
self, name, pytz_timezone, timer_dict, md5uuid,
245+
function, *args, **kwargs
245246
):
246247
"""
247248
:param pytz_timezone:
@@ -284,7 +285,11 @@ def __init__(
284285
self._timer_params = self._generate_timer_params(self._timer_dict)
285286
self._check_param_valids(self._timer_params)
286287
self._lastsched_time = None
287-
self._md5_id = self._GEN.get_uuid()[0]
288+
if md5uuid is None:
289+
self._md5_id = self._GEN.get_uuid()[0]
290+
else:
291+
self._md5_id = md5uuid
292+
self._timer = None
288293

289294
def get_funcargs(self):
290295
"""return (function, args, kwargs)"""
@@ -521,6 +526,14 @@ def __repr__(self):
521526
"""return info of the crontask"""
522527
return 'CronTask(ID:{0} Name:{1})'.format(self.taskid(), self._name)
523528

529+
def set_timer(self, timer):
530+
"""set timer to this crontask"""
531+
self._timer = timer
532+
533+
def get_timer(self):
534+
"""return timer of the crontask"""
535+
return self._timer
536+
524537

525538
class CronExecution(ExecutionService):
526539
"""
@@ -613,6 +626,7 @@ def delay_exec(self,
613626
delay_time_insec, self._do_delay_exe,
614627
[task_data]
615628
)
629+
crontask.set_timer(timer)
616630
timer.start()
617631

618632
def schedule(self, crontask):
@@ -656,4 +670,13 @@ def schedule(self, crontask):
656670
)
657671
self._task_dict[crontask.taskid()] = crontask
658672

673+
def calcel_delay_exec(self, taskid):
674+
"""calcel delayexec by taskid"""
675+
task = self._task_dict.get(taskid, None)
676+
if task is None:
677+
log.warn('delay exec task id {0} not found'.format(taskid))
678+
return
679+
timer = task.get_timer()
680+
timer.cancel()
681+
659682
# vi:set tw=0 ts=4 sw=4 nowrap fdm=indent

‎cup/timeplus.py‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
"""
99
from __future__ import print_function
1010
import time
11+
import datetime
1112

12-
__all__ = ['get_str_now']
13+
import pytz
14+
15+
16+
__all__ = ['get_str_now', 'TimePlus']
1317

1418

1519
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'):
2226
return str(time.strftime(fmt, time.localtime()))
2327

2428

29+
class TimePlus(object):
30+
"""arrow time"""
31+
def __init__(self, timezone):
32+
"""
33+
initialize with timezone setup
34+
"""
35+
if not isinstance(timezone, pytz.BaseTzInfo):
36+
raise ValueError('not a object of pytz.timezone("xxx/xxx")')
37+
self._timezone = timezone
38+
self._utc_tz = pytz.timezone('UTC')
39+
40+
def get_timezone(self):
41+
"""
42+
return current pytz timezone object
43+
"""
44+
return self._timezone
45+
46+
def set_newtimezone(self, pytz_timezone):
47+
"""
48+
refresh timezone
49+
50+
:return:
51+
True if refreshing is done. False otherwise
52+
"""
53+
self._timezone = pytz_timezone
54+
55+
@classmethod
56+
def utc_now(cls):
57+
"""return utc_now"""
58+
return datetime.datetime.now(pytz.UTC)
59+
60+
def local2utc(self, dateobj):
61+
"""
62+
local timezone to utc conversion
63+
64+
:return:
65+
a datetime.datetime object with utc timezone enabled
66+
67+
:raise:
68+
ValueError if dateobj is not a datetime.datetime object
69+
"""
70+
if not isinstance(dateobj, datetime.datetime):
71+
raise ValueError('dateobj is not a datetime.datetime')
72+
return dateobj.astimezone(self._utc_tz)
73+
74+
def utc2local(self, dateobj):
75+
"""
76+
utc datetime to local timezone datetime.datetime
77+
"""
78+
if not isinstance(dateobj, datetime.datetime):
79+
raise ValueError('dateobj is not a datetime.datetime')
80+
return dateobj.astimezone(self._timezone)
81+
82+
2583
if __name__ == '__main__':
2684
print(get_str_now())

‎cup/version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]
1414

1515

16-
VERSION = '3.2.16'
16+
VERSION = '3.2.18'
1717
AUTHOR = 'CUP-DEV Team'
1818

1919
# vi:set tw=0 ts=4 sw=4 nowrap fdm=indent

0 commit comments

Comments
 (0)