DrValera commited on
Commit
dd51f82
·
verified ·
1 Parent(s): 9e6e0eb

Corrected non-sleep behaviour for 15 minutes

Browse files
Files changed (1) hide show
  1. main.py +8 -12
main.py CHANGED
@@ -41,9 +41,8 @@ SDK_MAX_BODY_BYTES_extended = int(os.getenv("SDK_MAX_BODY_BYTES_extended", "1250
41
 
42
  # How long to wait for upstream (API) response; long runs may need 30+ min (1800+)
43
  UPSTREAM_TIMEOUT = float(os.getenv("UPSTREAM_TIMEOUT", "900")) # 15 minutes
44
- # While waiting for a response: send secure_ping to upstream every N seconds; if SELF_URL set, also ping self every SELF_PING_INTERVAL so this Space stays awake
45
- PING_UPSTREAM_INTERVAL = float(os.getenv("PING_UPSTREAM_INTERVAL", "270"))
46
- SELF_PING_INTERVAL = float(os.getenv("SELF_PING_INTERVAL", "120"))
47
 
48
  # Early global body-size guard (runs before routes)
49
  @app.middleware("http")
@@ -102,11 +101,10 @@ async def _ping_upstream_loop(ping_url: str, headers: dict, interval: float, sel
102
  pass
103
 
104
  def _start_ping_task(ping_url: str, headers: dict, self_url: str = ""):
105
- """Start background ping task. If SELF_URL set, uses SELF_PING_INTERVAL and pings self + backend; else PING_UPSTREAM_INTERVAL, backend only."""
106
- interval = (SELF_PING_INTERVAL if self_url and SELF_PING_INTERVAL > 0 else PING_UPSTREAM_INTERVAL) or PING_UPSTREAM_INTERVAL
107
- if interval <= 0:
108
  return None
109
- return asyncio.create_task(_ping_upstream_loop(ping_url, headers, interval, self_url))
110
 
111
  async def _cancel_ping_task(task: asyncio.Task | None):
112
  if task is None:
@@ -132,7 +130,7 @@ async def _forward(path: str, method: str = "GET", json_body=None, user_token: s
132
  Forward request to the PRIVATE Space:
133
  - 'Authorization: Bearer <HF_TOKEN>' to pass HF private gate
134
  - 'X-API-Key: <dt+...>' so your private app can validate the user token
135
- While waiting for the result, sends secure_ping to upstream every PING_UPSTREAM_INTERVAL seconds.
136
  """
137
  url = f"{UPSTREAM_URL}{path}"
138
  headers = {
@@ -157,8 +155,7 @@ async def _forward(path: str, method: str = "GET", json_body=None, user_token: s
157
  r = await client.request(method, url, headers=headers, json=json_body)
158
 
159
  request_task = asyncio.create_task(do_request())
160
- interval = PING_UPSTREAM_INTERVAL if PING_UPSTREAM_INTERVAL > 0 else 0.0
161
- wait_sec = (SELF_PING_INTERVAL if SELF_URL and SELF_PING_INTERVAL > 0 else interval) or interval
162
  try:
163
  while not request_task.done():
164
  if wait_sec <= 0:
@@ -316,8 +313,7 @@ async def _forward_multipart_json(path: str, files=None, data=None, user_token:
316
  r = await client.request(method, url, headers=headers, files=files, data=data)
317
 
318
  request_task = asyncio.create_task(do_request())
319
- interval = PING_UPSTREAM_INTERVAL if PING_UPSTREAM_INTERVAL > 0 else 0.0
320
- wait_sec = (SELF_PING_INTERVAL if SELF_URL and SELF_PING_INTERVAL > 0 else interval) or interval
321
  try:
322
  while not request_task.done():
323
  if wait_sec <= 0:
 
41
 
42
  # How long to wait for upstream (API) response; long runs may need 30+ min (1800+)
43
  UPSTREAM_TIMEOUT = float(os.getenv("UPSTREAM_TIMEOUT", "900")) # 15 minutes
44
+ # While waiting for a response: every PING_INTERVAL seconds we ping the backend (secure_ping) and, if SELF_URL is set, we also ping ourselves (keep-alive) so this Space stays awake
45
+ PING_INTERVAL = float(os.getenv("PING_INTERVAL", "270"))
 
46
 
47
  # Early global body-size guard (runs before routes)
48
  @app.middleware("http")
 
101
  pass
102
 
103
  def _start_ping_task(ping_url: str, headers: dict, self_url: str = ""):
104
+ """Start background ping task: every PING_INTERVAL seconds ping backend and, if self_url set, also ping self."""
105
+ if PING_INTERVAL <= 0:
 
106
  return None
107
+ return asyncio.create_task(_ping_upstream_loop(ping_url, headers, PING_INTERVAL, self_url))
108
 
109
  async def _cancel_ping_task(task: asyncio.Task | None):
110
  if task is None:
 
130
  Forward request to the PRIVATE Space:
131
  - 'Authorization: Bearer <HF_TOKEN>' to pass HF private gate
132
  - 'X-API-Key: <dt+...>' so your private app can validate the user token
133
+ While waiting, every PING_INTERVAL seconds: secure_ping to backend and, if SELF_URL set, ping self (keep-alive).
134
  """
135
  url = f"{UPSTREAM_URL}{path}"
136
  headers = {
 
155
  r = await client.request(method, url, headers=headers, json=json_body)
156
 
157
  request_task = asyncio.create_task(do_request())
158
+ wait_sec = PING_INTERVAL if PING_INTERVAL > 0 else 0.0
 
159
  try:
160
  while not request_task.done():
161
  if wait_sec <= 0:
 
313
  r = await client.request(method, url, headers=headers, files=files, data=data)
314
 
315
  request_task = asyncio.create_task(do_request())
316
+ wait_sec = PING_INTERVAL if PING_INTERVAL > 0 else 0.0
 
317
  try:
318
  while not request_task.done():
319
  if wait_sec <= 0: