Ksjsjjdj commited on
Commit
6ebcbec
·
verified ·
1 Parent(s): a72ccb7

Upload utils.py

Browse files
Files changed (1) hide show
  1. utils.py +27 -4
utils.py CHANGED
@@ -70,31 +70,54 @@ def format_bytes(size):
70
  return f"{size:.4f}{power_labels[n]+'B'}"
71
 
72
 
73
- LOGGER_QUEUE = queue.Queue(5)
74
 
75
 
76
  def logger():
 
 
 
 
77
  print("enable")
78
  while True:
79
- item = LOGGER_QUEUE.get()
 
 
 
 
 
80
  try:
81
  LOG_PORT = os.environ.get("LOG_PORT")
82
  if LOG_PORT:
 
83
  requests.post(
84
  LOG_PORT,
85
  headers={"Content-Type": "application/json"},
86
  json=item,
 
87
  )
88
  except Exception:
 
89
  pass
90
 
91
 
92
  if os.environ.get("LOG_PORT"):
93
- threading.Thread(target=logger).start()
 
 
94
 
95
 
96
  def log(item):
97
- LOGGER_QUEUE.put_nowait(item)
 
 
 
 
 
 
 
 
 
98
 
99
 
100
  def web_search(query: str, top_k: int = 3) -> str:
 
70
  return f"{size:.4f}{power_labels[n]+'B'}"
71
 
72
 
73
+ LOGGER_QUEUE = queue.Queue(int(os.environ.get('LOGGER_QUEUE_SIZE', 100)))
74
 
75
 
76
  def logger():
77
+ """Background thread to post logs to LOG_PORT. Uses blocking get so the thread
78
+ will wait for items and won't spin when queue empty. Any errors are swallowed
79
+ to avoid crashing the logger thread.
80
+ """
81
  print("enable")
82
  while True:
83
+ try:
84
+ item = LOGGER_QUEUE.get()
85
+ except Exception:
86
+ # If queue is unexpectedly closed or an error occurs, keep running
87
+ time.sleep(0.1)
88
+ continue
89
  try:
90
  LOG_PORT = os.environ.get("LOG_PORT")
91
  if LOG_PORT:
92
+ # Best-effort; ignore any network error
93
  requests.post(
94
  LOG_PORT,
95
  headers={"Content-Type": "application/json"},
96
  json=item,
97
+ timeout=5,
98
  )
99
  except Exception:
100
+ # never let log failures escape to the main thread
101
  pass
102
 
103
 
104
  if os.environ.get("LOG_PORT"):
105
+ # make the logger thread a daemon so it won't block process exit
106
+ t = threading.Thread(target=logger, daemon=True)
107
+ t.start()
108
 
109
 
110
  def log(item):
111
+ try:
112
+ LOGGER_QUEUE.put_nowait(item)
113
+ except queue.Full:
114
+ # Queue is full: drop the log (best-effort). Avoid raising to keep the
115
+ # application responsive; optionally print a fallback log to console
116
+ try:
117
+ # Use a short, non-blocking print so at least something is recorded
118
+ print("LOG DROP: queue full, dropping log item")
119
+ except Exception:
120
+ pass
121
 
122
 
123
  def web_search(query: str, top_k: int = 3) -> str: