|
| 1 | +import os |
| 2 | +import logging |
| 3 | +from threading import Thread, Event |
| 4 | + |
| 5 | +from dtable_events.utils import get_python_executable, run |
| 6 | +from dtable_events.app.config import dtable_web_dir |
| 7 | + |
| 8 | +try: |
| 9 | + from seahub.settings import ENABLE_SUB_ACCOUNT |
| 10 | +except ImportError as err: |
| 11 | + ENABLE_SUB_ACCOUNT = False |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +__all__ = [ |
| 16 | + 'InactiveExpiredSubAccountsWorker', |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +class InactiveExpiredSubAccountsWorker(object): |
| 21 | + |
| 22 | + def __init__(self, config): |
| 23 | + self._enabled = ENABLE_SUB_ACCOUNT |
| 24 | + self._interval = 12 * 3600 # 12h |
| 25 | + |
| 26 | + def start(self): |
| 27 | + if not self._enabled: |
| 28 | + logging.warning('Can not start inactive expired sub accounts: it is not enabled!') |
| 29 | + return |
| 30 | + logging.info('Start inactive expired sub accounts.') |
| 31 | + |
| 32 | + InactiveExpiredSubAccountsWorkerTimer(interval=self._interval).start() |
| 33 | + |
| 34 | + |
| 35 | +class InactiveExpiredSubAccountsWorkerTimer(Thread): |
| 36 | + |
| 37 | + def __init__(self, interval): |
| 38 | + Thread.__init__(self) |
| 39 | + self._interval = interval |
| 40 | + self.finished = Event() |
| 41 | + |
| 42 | + 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() |
0 commit comments