You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lc_client.py 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import logging
  2. import os
  3. import time
  4. import requests
  5. LOG = logging.getLogger(__name__)
  6. class LCClientConfig:
  7. def __init__(self):
  8. self.lc_server = os.getenv("LC_SERVER", "http://127.0.0.1:9100")
  9. class LCClient:
  10. _retry = 3
  11. _retry_interval_seconds = 0.5
  12. config = LCClientConfig()
  13. @classmethod
  14. def send(cls, worker_name, message: dict):
  15. url = '{0}/neptune/workers/{1}/info'.format(
  16. cls.config.lc_server,
  17. worker_name
  18. )
  19. error = None
  20. for i in range(cls._retry):
  21. try:
  22. res = requests.post(url=url, json=message)
  23. LOG.info(
  24. f"send to lc, url={url}, data={message},"
  25. f"state={res.status_code}")
  26. return res.status_code < 300
  27. except Exception as e:
  28. error = e
  29. time.sleep(cls._retry_interval_seconds)
  30. LOG.warning(
  31. f"can't connect to lc[{cls.config.lc_server}] "
  32. f"data={message}, error={error}, "
  33. f"retry times: {cls._retry}")
  34. return False