Пример #1
0
 def test_update_user_feature_reason(self):
     uid = 'urv_007'
     item_feature_list_data = 'item_feature_list_data_%s' % uid
     self.assertEqual(True, cache_models.set_cache_online_user_feature_reason(uid, item_feature_list_data))
     res = cache_models.get_cache_online_user_feature_reason(uid)
     self.assertEqual(True, res[0])
     self.assertEqual(item_feature_list_data, res[1])
     self.assertEqual(True, cache_update_process.cache_clear_online_user_feature_reason())
     res = cache_models.get_cache_online_user_feature_reason(uid)
     self.assertEqual(True, res[0])
     self.assertEqual(None, res[1])
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)