Skip to content

Commit

Permalink
remove license-expire-sender-config and opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCXC committed Oct 20, 2023
1 parent 3c72505 commit 4c8a553
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dtable_events/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, config, task_mode):
self._data_syncr = DataSyncer(config)
self._workflow_schedule_scanner = WorkflowSchedulesScanner(config)
self._dtable_asset_trash_cleaner = DTableAssetTrashCleaner(config)
self._license_expiring_notices_sender = LicenseExpiringNoticesSender(config)
self._license_expiring_notices_sender = LicenseExpiringNoticesSender()

def serve_forever(self):
if self._enable_foreground_tasks:
Expand Down
43 changes: 9 additions & 34 deletions dtable_events/tasks/license_expiring_notices_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,28 @@
from seaserv import ccnet_api

from dtable_events.app.config import LICENSE_PATH, DTABLE_WEB_SERVICE_URL
from dtable_events.db import init_db_session_class
from dtable_events.utils import get_opt_from_conf_or_env
from dtable_events.utils.dtable_web_api import DTableWebAPI


class LicenseExpiringNoticesSender:

def __init__(self, config):
self._db_session_class = init_db_session_class(config)
self.days = [35, 15, 7, 6, 5, 4, 3, 2, 1]
def __init__(self):
self.days = [30, 15, 7, 6, 5, 4, 3, 2, 1]
self.license_path = LICENSE_PATH
self._parse_config(config)
logdir = os.path.join(os.environ.get('LOG_DIR', ''))
self._logfile = os.path.join(logdir, 'license_expiring_notices_sender.log')

def _parse_config(self, config):
section_name = 'LICENSE-EXPIRING-NOTICES-SENDER'
key_days = 'days'
if not config.has_section(section_name):
return

days_str = get_opt_from_conf_or_env(config, section_name, key_days, default='30,15,7,6,5,4,3,2,1')
days = []
for day_str in days_str.split(','):
try:
day = int(day_str.strip())
if day > 0 and day not in days:
days.append(day)
except:
pass
if days:
self.days = days

def start(self):
timer = LicenseExpiringNoticesSenderTimer(self._db_session_class, self.days, self.license_path, self._logfile)
timer = LicenseExpiringNoticesSenderTimer(self.days, self.license_path)
logging.info('Start license notices sender...')
timer.start()


class LicenseExpiringNoticesSenderTimer(Thread):

def __init__(self, db_session_class, days, license_path, _log_file):
def __init__(self, days, license_path):
super(LicenseExpiringNoticesSenderTimer, self).__init__()
self.daemon = True
self.db_session_class = db_session_class
self.days = days
self.license_path = license_path
self._logfile = _log_file

def run(self):
sched = BlockingScheduler()
Expand All @@ -72,7 +46,7 @@ def check():
with open(self.license_path, 'r') as f:
for line in f.readlines():
line = line.strip()
logging.info('line: %s', line)
logging.debug('line: %s', line)
if line.startswith('Expiration'):
expire_str = line
break
Expand All @@ -89,12 +63,13 @@ def check():
logging.warning('No expire date found: %s error: %s', expire_str, e)
return
days = (expire_date - date.today()).days
logging.info('license will expire in %s days', days)
if days not in self.days:
return
admin_users = ccnet_api.get_superusers()
dtable_web_api = DTableWebAPI(DTABLE_WEB_SERVICE_URL)
to_users = [user.email for user in admin_users]
try:
admin_users = ccnet_api.get_superusers()
dtable_web_api = DTableWebAPI(DTABLE_WEB_SERVICE_URL)
to_users = [user.email for user in admin_users]
dtable_web_api.internal_add_notification(to_users, 'license_expiring', {'days': days})
except Exception as e:
logging.exception('send license expiring days: %s to users: %s error: %s', days, to_users, e)
Expand Down

0 comments on commit 4c8a553

Please sign in to comment.