Skip to content

Commit 41590ea

Browse files
update InactiveExpiredSubAccountsWorkerTimer
1 parent d2d3609 commit 41590ea

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import logging
3-
from threading import Thread, Event
3+
from threading import Thread
4+
from apscheduler.schedulers.blocking import BlockingScheduler
45

56
from dtable_events.utils import get_python_executable, run
67
from dtable_events.app.config import dtable_web_dir
@@ -21,42 +22,39 @@ class InactiveExpiredSubAccountsWorker(object):
2122

2223
def __init__(self, config):
2324
self._enabled = ENABLE_SUB_ACCOUNT
24-
self._interval = 12 * 3600 # 12h
2525

2626
def start(self):
2727
if not self._enabled:
28-
logging.warning('Can not start inactive expired sub accounts: it is not enabled!')
28+
logging.info('Can not start inactive expired sub accounts: it is not enabled!')
2929
return
3030
logging.info('Start inactive expired sub accounts.')
3131

32-
InactiveExpiredSubAccountsWorkerTimer(interval=self._interval).start()
32+
InactiveExpiredSubAccountsWorkerTimer().start()
3333

3434

3535
class InactiveExpiredSubAccountsWorkerTimer(Thread):
3636

37-
def __init__(self, interval):
37+
def __init__(self):
3838
Thread.__init__(self)
39-
self._interval = interval
40-
self.finished = Event()
4139

4240
def run(self):
43-
while not self.finished.is_set():
44-
self.finished.wait(self._interval)
45-
if not self.finished.is_set():
46-
logging.info('Starts inactive expired sub accounts...')
47-
try:
48-
python_exec = get_python_executable()
49-
manage_py = os.path.join(dtable_web_dir, 'manage.py')
50-
51-
cmd = [
52-
python_exec,
53-
manage_py,
54-
'inactive_expired_sub_accounts',
55-
]
56-
with open(self._logfile, 'a') as fp:
57-
run(cmd, cwd=dtable_web_dir, output=fp)
58-
except Exception as e:
59-
logging.exception('error when inactive expired sub accounts: %s', e)
60-
61-
def cancel(self):
62-
self.finished.set()
41+
sched = BlockingScheduler()
42+
# fire at 0 o'clock in every day of week
43+
@sched.scheduled_job('cron', day_of_week='*', hour='0', minute='1', misfire_grace_time=600)
44+
def timed_job():
45+
logging.info('Starts inactive expired sub accounts...')
46+
try:
47+
python_exec = get_python_executable()
48+
manage_py = os.path.join(dtable_web_dir, 'manage.py')
49+
50+
cmd = [
51+
python_exec,
52+
manage_py,
53+
'inactive_expired_sub_accounts',
54+
]
55+
with open(self._logfile, 'a') as fp:
56+
run(cmd, cwd=dtable_web_dir, output=fp)
57+
except Exception as e:
58+
logging.exception('error when inactive expired sub accounts: %s', e)
59+
60+
sched.start()

0 commit comments

Comments
 (0)