#coding:utf-8
import os
import redis

from app import XProcess
from multiprocessing import Lock

from settings import settings
from v2.event_attr import EventAttr
from v2.ipregion import IpSearch, Ip2Region
from v2.log import logger

if __name__ == '__main__':
    pid = os.getpid()
    # 杀死所有进程
    stop_shell = """#!/bin/bash
echo `pstree -p {pid}`|awk 'BEGIN{{ FS="(" ; RS=")" }} NF>1 {{ print $NF }}'|xargs kill &>/dev/null
"""
    with open('stop.sh', 'w', encoding='utf8') as f:
        f.write(stop_shell.format(pid=pid))

    lock = Lock()
    rdb = redis.Redis(**settings.REDIS_CONF)
    event_attr = EventAttr(rdb)

    ipsearch = IpSearch(Ip2Region, "ip2region.db")
    for i in range(0, 16):
        XProcess(i, lock, ipsearch, logger, rdb, event_attr).start()
