停止入库信号
This commit is contained in:
parent
b11dae3ed5
commit
ff79538302
16
app.py
16
app.py
@ -1,3 +1,4 @@
|
|||||||
|
# coding:utf-8
|
||||||
import time
|
import time
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ class XProcess(Process):
|
|||||||
transmitter.add_source(handler_event, 5000, 60)
|
transmitter.add_source(handler_event, 5000, 60)
|
||||||
transmitter.add_source(handler_user, 500, 60)
|
transmitter.add_source(handler_user, 500, 60)
|
||||||
last_ts = int(time.time())
|
last_ts = int(time.time())
|
||||||
consumer = create_consumer(self.partition)
|
consumer, kafka_client = create_consumer(self.partition)
|
||||||
|
|
||||||
for topic, msg in consumer():
|
for topic, msg in consumer():
|
||||||
# print(msg)
|
# print(msg)
|
||||||
@ -47,6 +48,19 @@ class XProcess(Process):
|
|||||||
# continue
|
# continue
|
||||||
obj = getattr(handler_event, type_)
|
obj = getattr(handler_event, type_)
|
||||||
obj(msg)
|
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:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
# ck数据库连接
|
# ck数据库连接
|
||||||
CK_CONFIG = {'host': '139.159.159.3',
|
CK_CONFIG = {'host': '139.159.159.3',
|
||||||
@ -13,13 +14,21 @@ class Config:
|
|||||||
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],
|
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],
|
||||||
'value_deserializer': json.loads,
|
'value_deserializer': json.loads,
|
||||||
'auto_offset_reset': 'earliest',
|
'auto_offset_reset': 'earliest',
|
||||||
|
'enable_auto_commit': True,
|
||||||
|
'auto_commit_interval_ms': 10000,
|
||||||
|
|
||||||
# 每个游戏不一样
|
# 每个游戏不一样
|
||||||
'group_id': 'shanhai_group'
|
'group_id': 'shanhai_group'
|
||||||
}
|
}
|
||||||
|
KAFKA_PRODUCER_CONF = {
|
||||||
|
'bootstrap_servers': ["192.168.0.30:9092", "192.168.0.71:9092", "192.168.0.229:9092"],
|
||||||
|
'value_serializer': lambda v: json.dumps(v).encode('utf-8'),
|
||||||
|
}
|
||||||
# 游戏数据库名
|
# 游戏数据库名
|
||||||
GAME = 'shanhai'
|
GAME = 'shanhai'
|
||||||
|
|
||||||
|
STOP_SIGNAL = 'stop_MntxuXMc'
|
||||||
|
|
||||||
REDIS_CONF = {
|
REDIS_CONF = {
|
||||||
'host': '192.168.0.161',
|
'host': '192.168.0.161',
|
||||||
'port': 6379,
|
'port': 6379,
|
||||||
|
21
stop.py
Normal file
21
stop.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# coding:utf-8
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from kafka import KafkaProducer
|
||||||
|
|
||||||
|
from settings import settings
|
||||||
|
|
||||||
|
producer = KafkaProducer(**settings.KAFKA_PRODUCER_CONF)
|
||||||
|
|
||||||
|
msg = {
|
||||||
|
'#type': settings.STOP_SIGNAL,
|
||||||
|
'#time': int(time.time())
|
||||||
|
}
|
||||||
|
for i in range(0,16):
|
||||||
|
print(f'发送分区{i}')
|
||||||
|
future1 = producer.send(settings.GAME, msg, partition=i)
|
||||||
|
try:
|
||||||
|
future1.get(timeout=10)
|
||||||
|
except Exception as e: # 发送失败抛出kafka_errors
|
||||||
|
traceback.format_exc()
|
@ -7,8 +7,9 @@ __all__ = 'create_consumer',
|
|||||||
|
|
||||||
|
|
||||||
def create_consumer(partition=-1):
|
def create_consumer(partition=-1):
|
||||||
def consumer():
|
|
||||||
c = KafkaConsumer(**settings.KAFKA_CONSUMER_CONF)
|
c = KafkaConsumer(**settings.KAFKA_CONSUMER_CONF)
|
||||||
|
|
||||||
|
def consumer():
|
||||||
if partition > 0:
|
if partition > 0:
|
||||||
c.assign([TopicPartition(settings.SUBSCRIBE_TOPIC, partition)])
|
c.assign([TopicPartition(settings.SUBSCRIBE_TOPIC, partition)])
|
||||||
else:
|
else:
|
||||||
@ -19,4 +20,4 @@ def create_consumer(partition=-1):
|
|||||||
val = msg.value
|
val = msg.value
|
||||||
yield topic, val
|
yield topic, val
|
||||||
|
|
||||||
return consumer
|
return consumer, c
|
||||||
|
Loading…
Reference in New Issue
Block a user