# coding:utf-8
import time
from multiprocessing import Process

from settings import settings
from v2 import *
from v2.struct_cache import StructCacheFile, StructCacheRedis


class XProcess(Process):

    def __init__(self, partition, lock, ipsearch, log, rdb=None, event_attr=None):
        super(XProcess, self).__init__()
        # self.daemon = True
        self.partition = partition
        self.lock = lock
        self.ipsearch = ipsearch
        self.rdb = rdb
        self.log = log
        self.event_attr = event_attr

    def run(self):
        db_client = CK(**settings.CK_CONFIG)
        sketch = Sketch(db_client, StructCacheRedis(self.rdb))
        handler_event = HandlerEvent(db_client, settings.GAME, self.ipsearch)
        handler_user = HandlerUser(db_client, settings.GAME)
        transmitter = Transmitter(db_client, settings.GAME, sketch, self.log, self.lock, self.event_attr,
                                  self.partition)
        transmitter.start_ping()
        transmitter.add_source(handler_event, 5000, 60)
        transmitter.add_source(handler_user, 500, 60)

        last_ts = int(time.time())
        consumer, kafka_client = create_consumer(self.partition)

        for topic, msg in consumer():
            # print(msg)
            type_ = msg['#type']
            del msg['#type']
            ts = int(time.time())

            if 'user' in type_:
                # continue
                obj = getattr(handler_user, type_)
                handler_user.receive_data.append(UserAct(obj, msg))
                if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts:
                    last_ts = ts
                    handler_user.execute()

            elif 'track' in type_:
                # continue
                obj = getattr(handler_event, type_)
                obj(msg)
            elif type_ == settings.STOP_SIGNAL:
                # 1 小时内有效
                if msg.get('#time', 0) + 3600 < int(time.time()):
                    continue
                kafka_client.close()
                # 停止消费kafka
                print(f'进程{self.partition} 等待90秒')
                time.sleep(90)
                print(f'进程{self.partition} 写入数据')
                transmitter.run()
                print(f'进程{self.partition} 结束')

                break
            else:
                continue

            transmitter.run()
