diff --git a/fir_ser/api/utils/crontab/sync_cache.py b/fir_ser/api/utils/crontab/ctasks.py similarity index 75% rename from fir_ser/api/utils/crontab/sync_cache.py rename to fir_ser/api/utils/crontab/ctasks.py index e93a505..35f38b7 100644 --- a/fir_ser/api/utils/crontab/sync_cache.py +++ b/fir_ser/api/utils/crontab/ctasks.py @@ -4,8 +4,10 @@ # author: liuyu # date: 2020/4/7 -from api.models import Apps, UserInfo +from api.models import Apps, UserInfo, AppIOSDeveloperInfo from api.utils.storage.storage import Storage +from api.utils.app.supersignutils import IosUtils +from api.utils.utils import send_ios_developer_active_status from django.core.cache import cache from fir_ser.settings import CACHE_KEY_TEMPLATE, SYNC_CACHE_TO_DATABASE, SUPER_SIGN_ROOT import time, os @@ -25,18 +27,6 @@ def sync_download_times(): logger.info("sync_download_times app_id:%s count_hits:%s" % (app_id, count_hits)) -def sync_download_times_by_app_id(app_ids): - app_id_lists = [] - for app_id in app_ids: - down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"), app_id.get("app_id")]) - app_id_lists.append(down_tem_key) - down_times_lists = cache.get_many(app_id_lists) - for k, v in down_times_lists.items(): - app_id = k.split(CACHE_KEY_TEMPLATE.get("download_times_key"))[1].strip('_') - Apps.objects.filter(app_id=app_id).update(count_hits=v) - logger.info("sync_download_times_by_app_id app_id:%s count_hits:%s" % (app_id, v)) - - def auto_clean_upload_tmp_file(): upload_tem_key = CACHE_KEY_TEMPLATE.get("upload_file_tmp_name_key") key = "_".join([upload_tem_key, '*']) @@ -79,3 +69,20 @@ def auto_delete_tmp_file(): os.remove(file_path) except Exception as e: logger.error("auto_delete_tmp_file %s Failed . Exception %s" % (file_path, e)) + + +def auto_check_ios_developer_active(): + all_ios_developer = AppIOSDeveloperInfo.objects.filter(is_actived=True) + for ios_developer in all_ios_developer: + userinfo = ios_developer.user_id + if userinfo.supersign_active: + status, result = IosUtils.active_developer(ios_developer, userinfo) + msg = "auto_check_ios_developer_active user:%s ios.developer:%s status:%s result:%s" % ( + ios_developer.user_id, ios_developer, status, result) + if status: + logger.info(msg) + else: + ios_developer.is_actived = False + ios_developer.save() + logger.error(msg) + send_ios_developer_active_status(ios_developer, msg) diff --git a/fir_ser/api/utils/crontab/run.py b/fir_ser/api/utils/crontab/run.py index 89b8ee8..7815d7d 100644 --- a/fir_ser/api/utils/crontab/run.py +++ b/fir_ser/api/utils/crontab/run.py @@ -7,8 +7,8 @@ from apscheduler.schedulers.background import BackgroundScheduler from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job from fir_ser.settings import SYNC_CACHE_TO_DATABASE -from api.utils.crontab.sync_cache import sync_download_times, auto_clean_upload_tmp_file, auto_delete_job_log, \ - auto_delete_tmp_file +from api.utils.crontab.ctasks import sync_download_times, auto_clean_upload_tmp_file, auto_delete_job_log, \ + auto_delete_tmp_file, auto_check_ios_developer_active import logging logger = logging.getLogger(__file__) @@ -29,16 +29,15 @@ try: scheduler.add_jobstore(DjangoJobStore(), "default") - # 设置定时任务,选择方式为interval,时间间隔为10s # 另一种方式为每天固定时间执行任务,对应代码为: # @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='9', minute='30', second='10',id='task_time') + @register_job(scheduler, "interval", seconds=SYNC_CACHE_TO_DATABASE.get("download_times")) def sync_download_times_job(): # 这里写你要执行的任务 sync_download_times() - # @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='9', minute='30', second='10',id='task_time') @register_job(scheduler, "interval", seconds=SYNC_CACHE_TO_DATABASE.get("auto_clean_tmp_file_times")) def auto_clean_upload_tmp_file_job(): auto_clean_upload_tmp_file() @@ -50,6 +49,12 @@ try: auto_delete_tmp_file() + @register_job(scheduler, "interval", + seconds=SYNC_CACHE_TO_DATABASE.get("auto_check_ios_developer_active_times")) + def auto_check_ios_developer_active_job(): + auto_check_ios_developer_active() + + register_events(scheduler) scheduler.start() except Exception as e: diff --git a/fir_ser/api/utils/crontab/runWin.py b/fir_ser/api/utils/crontab/runWin.py index 960a1fd..ea8027a 100644 --- a/fir_ser/api/utils/crontab/runWin.py +++ b/fir_ser/api/utils/crontab/runWin.py @@ -7,7 +7,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job from fir_ser.settings import SYNC_CACHE_TO_DATABASE -from api.utils.crontab.sync_cache import sync_download_times +from api.utils.crontab.ctasks import sync_download_times # import atexit # import fcntl diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 8136a5f..9479525 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -10,7 +10,6 @@ import time, os from django.utils import timezone from fir_ser.settings import CACHE_KEY_TEMPLATE, SERVER_DOMAIN, SYNC_CACHE_TO_DATABASE, DEFAULT_MOBILEPROVISION from api.utils.storage.storage import Storage, LocalStorage -from api.utils.crontab.sync_cache import sync_download_times_by_app_id from api.utils.utils import file_format_path import logging @@ -21,6 +20,18 @@ except Exception as e: logger.error("import crontab.run failed Exception:%s" % (e)) +def sync_download_times_by_app_id(app_ids): + app_id_lists = [] + for app_id in app_ids: + down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"), app_id.get("app_id")]) + app_id_lists.append(down_tem_key) + down_times_lists = cache.get_many(app_id_lists) + for k, v in down_times_lists.items(): + app_id = k.split(CACHE_KEY_TEMPLATE.get("download_times_key"))[1].strip('_') + Apps.objects.filter(app_id=app_id).update(count_hits=v) + logger.info("sync_download_times_by_app_id app_id:%s count_hits:%s" % (app_id, v)) + + def get_download_url_by_cache(app_obj, filename, limit, isdownload=True, key='', udid=None): now = time.time() if isdownload is None: diff --git a/fir_ser/api/utils/utils.py b/fir_ser/api/utils/utils.py index 51e21ef..662a2e3 100644 --- a/fir_ser/api/utils/utils.py +++ b/fir_ser/api/utils/utils.py @@ -142,12 +142,22 @@ def is_valid_email(email): return False -def get_sender_token(sender, user_id, target, action): +def get_sender_token(sender, user_id, target, action, msg=None): sms_token_obj = DownloadToken() code = generateNumericTokenOfLength(6) token = sms_token_obj.make_token(code, time_limit=300, key=user_id) tmpCache.set_tmp_cache(user_id, token, target) - sender.send_change_msg(target, code) + if action == 'change': + sender.send_change_msg(target, code) + elif action == 'register': + sender.send_register_msg(target, code) + elif action == 'login': + sender.send_login_msg(target, code) + elif action == 'msg': + sender.send_email_msg(target, msg) + else: + logger.error("get_sender_token failed. action is %s" % (action)) + return None, None return token, code @@ -161,9 +171,9 @@ def is_valid_sender_code(key, token, code): return sms_token_obj.verify_token(token, code), tmpCache.get_tmp_cache(key, token) -def get_sender_email_token(key, email, action): +def get_sender_email_token(key, email, action, msg=None): sender = SendMessage('email') - return get_sender_token(sender, key, email, action) + return get_sender_token(sender, key, email, action, msg) def check_username_exists(username): @@ -178,3 +188,13 @@ def get_random_username(length=16): if check_username_exists(username): return get_random_username(length) return username + + +def send_ios_developer_active_status(developer, msg): + user_info = developer.user_id + act = 'email' + email = user_info.email + if email: + get_sender_email_token(act, email, 'msg', msg) + else: + logger.info("user %s has no email. so %s can't send!" % (user_info, msg)) diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 7ef3ea7..2f91467 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -304,6 +304,7 @@ SYNC_CACHE_TO_DATABASE = { 'try_login_times': 5, # 当天登录失败次数,超过该失败次数,锁定24小时 'auto_clean_apscheduler_log': 100000, # 定时清理定时任务执行的日志,该日志存在数据库中,该参数为日志保留的数量 'clean_local_tmp_file_from_mtime': 60 * 60, # 清理最后一次修改时间超过限制时间的临时文件,单位秒 + 'auto_check_ios_developer_active_times': 60 * 60 * 12, # ios开发者证书检测时间 } SERVER_DOMAIN = {