diff --git a/dtable_events/app/app.py b/dtable_events/app/app.py index 3be75fb5..eab78db6 100644 --- a/dtable_events/app/app.py +++ b/dtable_events/app/app.py @@ -7,6 +7,7 @@ from dtable_events.dtable_io.dtable_io_server import DTableIOServer from dtable_events.tasks.instant_notices_sender import InstantNoticeSender from dtable_events.tasks.email_notices_sender import EmailNoticesSender +from dtable_events.tasks.inactive_expired_sub_account_worker import InactiveExpiredSubAccountsWorker from dtable_events.tasks.dtables_cleaner import DTablesCleaner from dtable_events.tasks.dtable_updates_sender import DTableUpdatesSender from dtable_events.tasks.dtable_real_time_rows_counter import DTableRealTimeRowsCounter @@ -60,6 +61,7 @@ def __init__(self, config, seafile_config, task_mode): # cron jobs self._instant_notices_sender = InstantNoticeSender(config) self._email_notices_sender = EmailNoticesSender(config) + self._inactive_expired_sub_accounts_worker = InactiveExpiredSubAccountsWorker(config) self._dtables_cleaner = DTablesCleaner(config) self._dtable_updates_sender = DTableUpdatesSender(config) self._dtable_notification_rules_scanner = DTableNofiticationRulesScanner(config) @@ -99,6 +101,7 @@ def serve_forever(self): # cron jobs self._instant_notices_sender.start() # default True self._email_notices_sender.start() # default True + self._inactive_expired_sub_accounts_worker.start()# default False self._dtables_cleaner.start() # default True self._dtable_updates_sender.start() # default True self._dtable_notification_rules_scanner.start() # default True diff --git a/dtable_events/tasks/inactive_expired_sub_account_worker.py b/dtable_events/tasks/inactive_expired_sub_account_worker.py new file mode 100644 index 00000000..dd0c3449 --- /dev/null +++ b/dtable_events/tasks/inactive_expired_sub_account_worker.py @@ -0,0 +1,67 @@ +import os +import logging +from threading import Thread +from apscheduler.schedulers.blocking import BlockingScheduler + +from dtable_events.utils import get_python_executable, run +from dtable_events.app.config import dtable_web_dir + +try: + from seahub.settings import ENABLE_SUB_ACCOUNT +except ImportError as err: + ENABLE_SUB_ACCOUNT = False + +logger = logging.getLogger(__name__) + +__all__ = [ + 'InactiveExpiredSubAccountsWorker', +] + + +class InactiveExpiredSubAccountsWorker(object): + + def __init__(self, config): + self._enabled = ENABLE_SUB_ACCOUNT + self._logfile = None + self._prepare_logfile() + + def _prepare_logfile(self): + logdir = os.path.join(os.environ.get('LOG_DIR', '')) + self._logfile = os.path.join(logdir, 'inactive_expired_sub_account.log') + + def start(self): + if not self._enabled: + logging.info('Can not start inactive expired sub accounts: it is not enabled!') + return + logging.info('Start inactive expired sub accounts.') + + InactiveExpiredSubAccountsWorkerTimer(self._logfile).start() + + +class InactiveExpiredSubAccountsWorkerTimer(Thread): + + def __init__(self, logfile): + Thread.__init__(self) + self._logfile = logfile + + def run(self): + sched = BlockingScheduler() + # fire at 0 o'clock in every day of week + @sched.scheduled_job('cron', day_of_week='*', hour='0', minute='1', misfire_grace_time=600) + def timed_job(): + logging.info('Starts inactive expired sub accounts...') + try: + python_exec = get_python_executable() + manage_py = os.path.join(dtable_web_dir, 'manage.py') + + cmd = [ + python_exec, + manage_py, + 'inactive_expired_sub_accounts', + ] + with open(self._logfile, 'a') as fp: + run(cmd, cwd=dtable_web_dir, output=fp) + except Exception as e: + logging.exception('error when inactive expired sub accounts: %s', e) + + sched.start()