def test_cache_update_all_together(self):
        uid = 'db_uid'
        resource_type = 'movi'
        item_id = 'db_item_id'
        feature_name = 'db_feature_name'
        self.assertEqual(True, db_models_update.set_db_offline_user_resource_visitinfo(uid, resource_type, 'db_visit_info_data'))
        self.assertEqual(True, db_models_update.set_db_offline_user_feature_list(uid, resource_type, 'db_feature_list_data'))
        self.assertEqual(True, db_models_update.set_db_offline_user_recommend_item_list(uid, resource_type, 'db_user_recommend_item_list'))
        self.assertEqual(True, db_models_update.set_db_offline_item_features(item_id, resource_type, 'db_item_features_data'))
        self.assertEqual(True, db_models_update.set_db_offline_item_recommend_item_list(item_id, resource_type, 'db_item_id_list_data'))
        self.assertEqual(True, db_models_update.set_db_offline_feature_hot_item_list(feature_name, resource_type, 'db_hot_item_list_data'))
        self.assertEqual(True, db_models_update.set_db_offline_global_hot_feature_list(resource_type, 'db_hot_feature_list_data'))
        self.assertEqual(True, db_models_update.set_db_offline_item_id_new_to_old(resource_type, item_id, 'db_old_item_id_data'))

        cache_uid = 'cache_uid'
        cache_item_id = 'cache_item_id'
        cache_feature_name = 'cache_feature_name'
        self.assertEqual(True, cache_models.set_cache_online_user_resource_visitinfo(cache_uid, resource_type, 'on_cache_visit_info_data'))
        self.assertEqual(True, cache_models.set_cache_offline_user_resource_visitinfo(cache_uid, resource_type, 'cache_visit_info_data'))
        self.assertEqual(True, cache_models.set_cache_online_user_feature_list(cache_uid, resource_type, 'on_cache_feature_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_user_feature_list(cache_uid, resource_type, 'cache_feature_list_data'))
        self.assertEqual(True, cache_models.set_cache_online_user_feature_reason(cache_uid, 'cache_feature_list_data'))
        self.assertEqual(True, cache_models.set_cache_online_user_favor_item_list(cache_uid, resource_type, 'cache_item_id_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_user_recommend_item_list(cache_uid, resource_type, 'cache_feature_name_item_id_list_data'))
        self.assertEqual(True, cache_models.set_cache_online_user_push_item_list(cache_uid, 'cache_item_id_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_item_recommend_item_list(cache_item_id, 'cache_item_id_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_item_features(cache_item_id, 'cache_item_feature_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_feature_hot_item_list(cache_feature_name, 'cache_item_id_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_global_hot_feature_list(resource_type, 'cache_item_feature_list_data'))
        self.assertEqual(True, cache_models.set_cache_offline_item_id_new_to_old(cache_item_id, 'cache_old_item_id_data'))
        import cache_update_process
        all_resource_type_list = ['movi', 'news']
        common_resource_type_list = ['movi']
        self.assertEqual(True, cache_update_process.cache_update_all_together(all_resource_type_list, common_resource_type_list))
        self.assertEqual((True, {}), cache_models.get_cache_online_user_resource_visitinfo(cache_uid, [resource_type]))
        self.assertEqual((True, {resource_type:'db_visit_info_data'}), cache_models.get_cache_offline_user_resource_visitinfo(uid, [resource_type]))
        self.assertEqual((True, {}), cache_models.get_cache_online_user_feature_list(cache_uid, [resource_type]))
        self.assertEqual((True, {resource_type:'db_feature_list_data'}), cache_models.get_cache_offline_user_feature_list(uid, [resource_type]))
        self.assertEqual((True, None), cache_models.get_cache_online_user_feature_reason(cache_uid))
        self.assertEqual((True, {}), cache_models.get_cache_online_user_favor_item_list(cache_uid, [resource_type]))
        self.assertEqual((True, 'cache_item_id_list_data'), cache_models.get_cache_online_user_push_item_list(cache_uid))
        self.assertEqual((True,{resource_type:'db_user_recommend_item_list'}), cache_models.get_cache_offline_user_recommend_item_list(uid, [resource_type]))
        self.assertEqual((True,{item_id:'db_item_id_list_data'}), cache_models.get_cache_offline_item_recommend_item_list([item_id]))
        self.assertEqual((True,{item_id:'db_item_features_data'}), cache_models.get_cache_offline_item_features([item_id]))
        self.assertEqual((True,{feature_name:'db_hot_item_list_data'}), cache_models.get_cache_offline_feature_hot_item_list([feature_name]))
        self.assertEqual((True,{resource_type:'db_hot_feature_list_data'}), cache_models.get_cache_offline_global_hot_feature_list([resource_type]))
        self.assertEqual((True,{item_id:'db_old_item_id_data'}), cache_models.get_cache_offline_item_id_new_to_old([item_id]))
def run(deadline_time):

    common_resource_type_list = ['movi', 'novl', 'tele', 'cart', 'show']
    all_resource_type_list = common_resource_type_list + ['news']

    import datetime
    date_time = datetime.datetime.now()
    day_index = get_day_index(date_time)
    now_hour = date_time.hour
    #是否落在3~5点
    print 'do cache update run, now_hour[%s] deadline_time[%s] at %s.' % (now_hour, deadline_time, date_time)
    if now_hour < deadline_time:
        if OfflineCacheStatus.exists(day_index):
            ocs = OfflineCacheStatus.Get(day_index)
            if None == ocs.status or OfflineCacheStatus.OFFLINE_UPDATE_STATUS_OFFLINE_READY == ocs.status or OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_FAIL == ocs.status:
                # do cache update
                # update to db
                if False == cache_update_process.cache_update_all_together(all_resource_type_list, common_resource_type_list):
                    ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_FAIL
                else:
                    ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_SUCCESS
                ocs.try_times += 1;
                ocs.cache_update_time = datetime.datetime.now()
                ocs.save()
                print 'update all cache together day_index[%s] status[%s], at %s.' % (day_index, ocs.status, date_time)
            else:
                print 'do nothing. day_index[%s] status[%s], at %s' % (day_index, ocs.status, date_time)
        else:
            print 'do nothing because offline data is null. at %s' % date_time
    elif now_hour >= deadline_time:
        if OfflineCacheStatus.exists(day_index):
            ocs = OfflineCacheStatus.Get(day_index)
        else:
            ocs = OfflineCacheStatus()
            ocs.day_index = day_index
            ocs.status = None
            ocs.try_times = 0
        # 如果离线端已近插入数据,但是由于更新失败,则重新更新
        if OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_FAIL == ocs.status:
            # do cache update
            # update to db
            if False == cache_update_process.cache_update_all_together(all_resource_type_list, common_resource_type_list):
                ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_FAIL
            else:
                ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_SUCCESS
            ocs.try_times += 1;
            ocs.cache_update_time = datetime.datetime.now()
            ocs.save()
            print 'update all cache together day_index[%s] status[%s], at %s.' % (day_index, ocs.status, date_time)
        # 仅进行online端更新 
        elif None == ocs.status or OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_ONLY_FAIL == ocs.status:
            # do online cache update
            if False == cache_update_process.cache_clear_online_user_feature_reason():
                ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_ONLY_FAIL
            else:
                ocs.status = OfflineCacheStatus.OFFLINE_UPDATE_STATUS_ONLINE_ONLY_SUCCESS
            ocs.try_times += 1;
            ocs.cache_update_time = datetime.datetime.now()
            ocs.save()
            print 'clear user feature reason only, day_index[%s] status[%s], at %s.' % (day_index, ocs.status, date_time)
        else:
            print 'do nothing. day_index[%s] status[%s], at %s' % (day_index, ocs.status, date_time)