import traceback import pymongo from pymongo import UpdateOne from pydantic import Field from model import BaseModel from .task import Task from utils import * class RepairGunfu(Task): """ 补充滚服 """ class Model(BaseModel): game_role_id: str = Field(..., min_length=1, title="角色id", alias='_game_role_id') device_id: str = Field(..., min_length=1, title='设备id', alias='_device_id') def cleaning(self, cursor_list): for cursor in cursor_list: # type:dict for source_coll, ts in cursor.items(): # type:str,dict if ts['cursor_st'] == ts['cursor_et']: continue logger.info(f'开始处理{self.game_name} 处理 {source_coll} 游标 {ts}') where = { 'role_create_time': { '$gte': ts['cursor_st'], '$lt': ts['cursor_et'], }, 'gunfu_num': {'$exists': False} } projection = self.Model.get_fields() bulk_data = [] for item in self.local_db[source_coll].find(where, projection).sort('role_create_time', pymongo.ASCENDING): try: # 新角色 model = self.Model(**item) device_id = model.device_id # 查找该设备所有滚服角色 role_cnt = self.local_db[source_coll].count( {'_device_id': device_id, 'gunfu_num': {'$exists': True}}) bulk_data.append( UpdateOne({'_game_role_id': model.game_role_id}, {'$set': {'gunfu_num': role_cnt + 1}}, upsert=True)) except Exception as e: msg = traceback.format_exc() ddsend_msg(f'{self.game_name}.{source_coll}字段异常 {msg}') logger.error(repr(e)) if bulk_data: self.local_db[self.dest_coll].bulk_write(bulk_data, ordered=False) self.remote_db[self.dest_coll].bulk_write(bulk_data, ordered=False) self.set_cursor(cursor_st=ts['cursor_st'], cursor_et=ts['cursor_et'])