Skip to content

Commit 27c4058

Browse files
feat: timeout for bot
1 parent 69f4398 commit 27c4058

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

extras/telegram_bot/config.example.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ allowed_types = ["song", "album", "playlist"]
1818
max_tracks = 20
1919
max_song_duration_sec = 900
2020
max_total_duration_sec = 18000
21+
task_timeout_sec = 480
2122

2223
[user_default]
2324
language = "follow-user"

extras/telegram_bot/src/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class LimitsSettings(BaseModel):
2626
max_tracks: int = 100
2727
max_song_duration_sec: int = 1200
2828
max_total_duration_sec: int = 18000
29+
task_timeout_sec: int = 480
2930

3031

3132
class UserDefaultSettings(BaseModel):

extras/telegram_bot/src/handlers/download.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,18 @@ async def process_song(song_id: str, storefront: str, p_index=None):
452452

453453
# Spawn Rip Tasks
454454
if actual_songs_to_rip:
455+
async def rip_with_timeout(s_obj, cd, fl, pd, pl):
456+
import asyncio
457+
try:
458+
await asyncio.wait_for(
459+
ripper.rip_song(s_obj, cd, fl, parent_done=pd, playlist=pl),
460+
timeout=bot_config.limits.task_timeout_sec
461+
)
462+
except asyncio.TimeoutError:
463+
pass
464+
455465
for song_obj, p_index in actual_songs_to_rip:
456-
safely_create_task(ripper.rip_song(song_obj, codec, flags, parent_done=parent_done, playlist=p_index))
466+
safely_create_task(rip_with_timeout(song_obj, codec, flags, parent_done, p_index))
457467
else:
458468
# Everything was cached
459469
completed_event.set()

src/rip.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ async def rip_song(self, url: Song, codec: str, flags: Flags = Flags(),
207207
task.logger.logger.exception(f"Error processing song: {e}")
208208
task.update_status(Status.FAILED)
209209
task.error = e
210+
except asyncio.CancelledError:
211+
task.logger.logger.warning("Task processing timed out or was cancelled")
212+
task.update_status(Status.FAILED)
213+
task.error = Exception("Task execution timed out")
214+
raise
210215
finally:
211216
await self.download_manager.unregister_task(task)
212217
task.update_status(task.status) # Ensure status is set

0 commit comments

Comments
 (0)