12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- # coding:utf-8
- from a2s.proto import service_pb2
- from a2s.proto import service_pb2_grpc
- import grpc
- from pynats import NATSClient
- from a2s.tools import json_serialize
- def watch_monitor(a2s_ip, topic):
- """
- 监控
- :return:
- """
- try:
- with grpc.insecure_channel(a2s_ip) as channel:
- # 客户端实例
- stub = service_pb2_grpc.CallerStub(channel)
- # 调用服务端方法
- response = stub.ViewState(service_pb2.StateReq(topic=topic))
- # 反序列化
- count = response.currentRequest
- return count
- except:
- return None
- def close_worker(ip_list, a2s_ip, topic):
- """
- 关闭worker
- :param ip_list:
- :param a2s_ip:
- :param topic:
- :return:
- """
- with NATSClient(a2s_ip, name=topic) as nc:
- nc.connect()
- try:
- for host in ip_list:
- nc.publish(subject=topic, payload=json_serialize({"host": host}))
- return True
- except Exception as e:
- print(e)
- return False
|