Skip to content

Commit 2ab7f4c

Browse files
committed
fix: simplify webhook
1 parent 283acec commit 2ab7f4c

2 files changed

Lines changed: 18 additions & 31 deletions

File tree

runpod/serverless/modules/rp_fastapi.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import os
55
import uuid
6-
import time
76
import threading
87
from dataclasses import dataclass
98
from typing import Union, Optional, Dict, Any
@@ -86,15 +85,18 @@ class StreamOutput:
8685

8786

8887
# ------------------------------ Webhook Sender ------------------------------ #
89-
def _send_webhook(url: str, payload: Dict[str, Any]) -> None:
88+
def _send_webhook(url: str, payload: Dict[str, Any]) -> bool:
9089
"""
91-
Sends a webhook to the provided URL. Retries once if the first attempt fails.
90+
Sends a webhook to the provided URL.
9291
9392
Args:
9493
url (str): The URL to send the webhook to.
9594
payload (Dict[str, Any]): The JSON payload to send.
95+
96+
Returns:
97+
bool: True if the request was successful, False otherwise.
9698
"""
97-
def attempt_send(session, url, payload):
99+
with requests.Session() as session:
98100
try:
99101
response = session.post(url, json=payload, timeout=10)
100102
response.raise_for_status() # Raises exception for 4xx/5xx responses
@@ -103,19 +105,6 @@ def attempt_send(session, url, payload):
103105
print(f"Request to {url} failed: {err}")
104106
return False
105107

106-
with requests.Session() as session:
107-
if attempt_send(session, url, payload):
108-
return True
109-
110-
print("Retrying...")
111-
time.sleep(1) # Wait for 1 second before retrying
112-
113-
if attempt_send(session, url, payload):
114-
return True
115-
116-
print("Failed to send webhook after retry.")
117-
return False
118-
119108

120109
# ---------------------------------------------------------------------------- #
121110
# API Worker #

tests/test_serverless/test_modules/test_fastapi.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def test_runsync(self):
141141
with patch(f"{module_location}.FastAPI", Mock()), \
142142
patch(f"{module_location}.APIRouter", return_value=Mock()), \
143143
patch(f"{module_location}.uvicorn", Mock()), \
144-
patch(f"{module_location}.uuid.uuid4", return_value="123"):
144+
patch(f"{module_location}.uuid.uuid4", return_value="123"), \
145+
patch(f"{module_location}.threading") as mock_threading:
145146

146147
default_input_object = rp_fastapi.DefaultRequest(
147148
input={"test_input": "test_input"}
@@ -183,9 +184,8 @@ def generator_handler(job):
183184
assert "error" in error_runsync_return
184185

185186
# Test webhook caller sent
186-
with patch(f"{module_location}.threading") as mock_thread:
187-
asyncio.run(worker_api._sim_runsync(input_object_with_webhook))
188-
assert mock_thread.Thread.called
187+
asyncio.run(worker_api._sim_runsync(input_object_with_webhook))
188+
assert mock_threading.Thread.called
189189

190190
loop.close()
191191

@@ -201,7 +201,7 @@ def test_stream(self):
201201
patch(f"{module_location}.APIRouter", return_value=Mock()), \
202202
patch(f"{module_location}.uvicorn", Mock()), \
203203
patch(f"{module_location}.uuid.uuid4", return_value="123"), \
204-
patch(f"{module_location}.threading"):
204+
patch(f"{module_location}.threading") as mock_threading:
205205

206206
default_input_object = rp_fastapi.DefaultRequest(
207207
input={"test_input": "test_input"}
@@ -232,10 +232,9 @@ def test_stream(self):
232232
}
233233

234234
# Test webhook caller sent
235-
with patch(f"{module_location}.threading", return_value=True) as mock_threading:
236-
asyncio.run(worker_api._sim_run(input_object_with_webhook))
237-
asyncio.run(worker_api._sim_stream("test-123"))
238-
assert mock_threading.Thread.called
235+
asyncio.run(worker_api._sim_run(input_object_with_webhook))
236+
asyncio.run(worker_api._sim_stream("test-123"))
237+
assert mock_threading.Thread.called
239238

240239
# Test with generator handler
241240
def generator_handler(job):
@@ -265,7 +264,7 @@ def test_status(self):
265264
patch(f"{module_location}.APIRouter", return_value=Mock()), \
266265
patch(f"{module_location}.uvicorn", Mock()), \
267266
patch(f"{module_location}.uuid.uuid4", return_value="123"), \
268-
patch(f"{module_location}.threading"):
267+
patch(f"{module_location}.threading") as mock_threading:
269268

270269
worker_api = rp_fastapi.WorkerAPI({"handler": self.handler})
271270

@@ -296,10 +295,9 @@ def test_status(self):
296295
}
297296

298297
# Test webhook caller sent
299-
with patch(f"{module_location}.threading", return_value=True) as mock_threading:
300-
asyncio.run(worker_api._sim_run(input_object_with_webhook))
301-
asyncio.run(worker_api._sim_status("test-123"))
302-
assert mock_threading.Thread.called
298+
asyncio.run(worker_api._sim_run(input_object_with_webhook))
299+
asyncio.run(worker_api._sim_status("test-123"))
300+
assert mock_threading.Thread.called
303301

304302
# Test with generator handler
305303
def generator_handler(job):

0 commit comments

Comments
 (0)