a2s_monitor.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # coding:utf-8
  2. from a2s.proto import service_pb2
  3. from a2s.proto import service_pb2_grpc
  4. import grpc
  5. from pynats import NATSClient
  6. from a2s.tools import json_serialize
  7. def watch_monitor(a2s_ip, topic):
  8. """
  9. 监控
  10. :return:
  11. """
  12. try:
  13. with grpc.insecure_channel(a2s_ip) as channel:
  14. # 客户端实例
  15. stub = service_pb2_grpc.CallerStub(channel)
  16. # 调用服务端方法
  17. response = stub.ViewState(service_pb2.StateReq(topic=topic))
  18. # 反序列化
  19. count = response.currentRequest
  20. return count
  21. except:
  22. return None
  23. def close_worker(ip_list, a2s_ip, topic):
  24. """
  25. 关闭worker
  26. :param ip_list:
  27. :param a2s_ip:
  28. :param topic:
  29. :return:
  30. """
  31. with NATSClient(a2s_ip, name=topic) as nc:
  32. nc.connect()
  33. try:
  34. for host in ip_list:
  35. nc.publish(subject=topic, payload=json_serialize({"host": host}))
  36. return True
  37. except Exception as e:
  38. print(e)
  39. return False