def start_subject_statistics_schedule(self):
    """
    定时启动题目分析任务
    :param self:
    :return:
    """
    try:
        logger.info('START(%s)' % self.request.id)
        category_list = ReportSubjectStatisticsMiddle.sync_distinct('category')

        ReportSubjectStatisticsMiddle().get_sync_collection(
            read_preference=ReadPreference.PRIMARY).drop()

        task_dt = datetime.now()
        logger.info('-- category -- %s' % str(category_list))
        if not category_list:
            category_list = [{
                'subject_cid': '$subject_cid',
                'province_code': '$province_code',
                'city_code': '$city_code'
            }]

        for category in category_list:
            logger.info('-- start -- %s' % str(category))
            RedisCache.hdel(KEY_CACHE_REPORT_CONDITION, str(category))
            start_split_subject_stat_task.delay(category, task_dt)

    except Exception:
        logger.error(str(traceback.format_exc()))

    logger.info(' END (%s)' % self.request.id)
예제 #2
0
def start_extract_subjects(self,
                           subject_choice_rule: SubjectChoiceRules,
                           times=1):
    try:
        logger.info('START(%s): rules_cid=%s' %
                    (self.request.id, subject_choice_rule.cid))
        cut_off_datetime = datetime.datetime.now()

        # 抽题
        def extract():
            for i in range(times):
                extract_subjects(subject_choice_rule)

        while True:
            extract()
            count = SubjectBanks.sync_count(
                dict(rule_cid=subject_choice_rule.cid),
                read_preference=ReadPreference.PRIMARY)
            if count >= 4096:
                break
        # 删除就题库
        SubjectBanks.sync_delete_many(
            dict(rule_cid=subject_choice_rule.cid,
                 choice_dt={'$lt': cut_off_datetime}))

        logger.info('END(%s): rules_cid=%s' %
                    (self.request.id, subject_choice_rule.cid))
    except Exception:
        logger.error(traceback.format_exc())
    finally:
        # 删除题库数量缓存
        RedisCache.hdel(KEY_EXTRACTING_SUBJECT_QUANTITY,
                        subject_choice_rule.cid)
        # 删除抽题状态
        RedisCache.hdel(KEY_PREFIX_EXTRACTING_SUBJECT_RULE,
                        subject_choice_rule.cid)
        # 删除数量缓存
        RedisCache.delete(
            '%s_%s' %
            (KEY_PREFIX_SUBJECT_BANKS_COUNT, subject_choice_rule.cid))