1
1
from typing import Optional
2
2
3
- from fastapi import Body , Path , Query
3
+ from fastapi import Body , HTTPException , Path , Query
4
4
from fastapi .routing import APIRouter
5
5
from pydantic import BaseModel , Field
6
6
22
22
RetryItemsResult ,
23
23
SessionQueueCountsByDestination ,
24
24
SessionQueueItem ,
25
+ SessionQueueItemNotFoundError ,
25
26
SessionQueueStatus ,
26
27
)
27
28
from invokeai .app .services .shared .pagination import CursorPaginatedResults
@@ -59,10 +60,12 @@ async def enqueue_batch(
59
60
),
60
61
) -> EnqueueBatchResult :
61
62
"""Processes a batch and enqueues the output graphs for execution."""
62
-
63
- return await ApiDependencies .invoker .services .session_queue .enqueue_batch (
64
- queue_id = queue_id , batch = batch , prepend = prepend
65
- )
63
+ try :
64
+ return await ApiDependencies .invoker .services .session_queue .enqueue_batch (
65
+ queue_id = queue_id , batch = batch , prepend = prepend
66
+ )
67
+ except Exception as e :
68
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while enqueuing batch: { e } " )
66
69
67
70
68
71
@session_queue_router .get (
@@ -82,14 +85,17 @@ async def list_queue_items(
82
85
) -> CursorPaginatedResults [SessionQueueItem ]:
83
86
"""Gets cursor-paginated queue items"""
84
87
85
- return ApiDependencies .invoker .services .session_queue .list_queue_items (
86
- queue_id = queue_id ,
87
- limit = limit ,
88
- status = status ,
89
- cursor = cursor ,
90
- priority = priority ,
91
- destination = destination ,
92
- )
88
+ try :
89
+ return ApiDependencies .invoker .services .session_queue .list_queue_items (
90
+ queue_id = queue_id ,
91
+ limit = limit ,
92
+ status = status ,
93
+ cursor = cursor ,
94
+ priority = priority ,
95
+ destination = destination ,
96
+ )
97
+ except Exception as e :
98
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while listing all items: { e } " )
93
99
94
100
95
101
@session_queue_router .get (
@@ -104,11 +110,13 @@ async def list_all_queue_items(
104
110
destination : Optional [str ] = Query (default = None , description = "The destination of queue items to fetch" ),
105
111
) -> list [SessionQueueItem ]:
106
112
"""Gets all queue items"""
107
-
108
- return ApiDependencies .invoker .services .session_queue .list_all_queue_items (
109
- queue_id = queue_id ,
110
- destination = destination ,
111
- )
113
+ try :
114
+ return ApiDependencies .invoker .services .session_queue .list_all_queue_items (
115
+ queue_id = queue_id ,
116
+ destination = destination ,
117
+ )
118
+ except Exception as e :
119
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while listing all queue items: { e } " )
112
120
113
121
114
122
@session_queue_router .put (
@@ -120,7 +128,10 @@ async def resume(
120
128
queue_id : str = Path (description = "The queue id to perform this operation on" ),
121
129
) -> SessionProcessorStatus :
122
130
"""Resumes session processor"""
123
- return ApiDependencies .invoker .services .session_processor .resume ()
131
+ try :
132
+ return ApiDependencies .invoker .services .session_processor .resume ()
133
+ except Exception as e :
134
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while resuming queue: { e } " )
124
135
125
136
126
137
@session_queue_router .put (
@@ -132,7 +143,10 @@ async def Pause(
132
143
queue_id : str = Path (description = "The queue id to perform this operation on" ),
133
144
) -> SessionProcessorStatus :
134
145
"""Pauses session processor"""
135
- return ApiDependencies .invoker .services .session_processor .pause ()
146
+ try :
147
+ return ApiDependencies .invoker .services .session_processor .pause ()
148
+ except Exception as e :
149
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while pausing queue: { e } " )
136
150
137
151
138
152
@session_queue_router .put (
@@ -144,7 +158,10 @@ async def cancel_all_except_current(
144
158
queue_id : str = Path (description = "The queue id to perform this operation on" ),
145
159
) -> CancelAllExceptCurrentResult :
146
160
"""Immediately cancels all queue items except in-processing items"""
147
- return ApiDependencies .invoker .services .session_queue .cancel_all_except_current (queue_id = queue_id )
161
+ try :
162
+ return ApiDependencies .invoker .services .session_queue .cancel_all_except_current (queue_id = queue_id )
163
+ except Exception as e :
164
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while canceling all except current: { e } " )
148
165
149
166
150
167
@session_queue_router .put (
@@ -156,7 +173,10 @@ async def delete_all_except_current(
156
173
queue_id : str = Path (description = "The queue id to perform this operation on" ),
157
174
) -> DeleteAllExceptCurrentResult :
158
175
"""Immediately deletes all queue items except in-processing items"""
159
- return ApiDependencies .invoker .services .session_queue .delete_all_except_current (queue_id = queue_id )
176
+ try :
177
+ return ApiDependencies .invoker .services .session_queue .delete_all_except_current (queue_id = queue_id )
178
+ except Exception as e :
179
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while deleting all except current: { e } " )
160
180
161
181
162
182
@session_queue_router .put (
@@ -169,7 +189,12 @@ async def cancel_by_batch_ids(
169
189
batch_ids : list [str ] = Body (description = "The list of batch_ids to cancel all queue items for" , embed = True ),
170
190
) -> CancelByBatchIDsResult :
171
191
"""Immediately cancels all queue items from the given batch ids"""
172
- return ApiDependencies .invoker .services .session_queue .cancel_by_batch_ids (queue_id = queue_id , batch_ids = batch_ids )
192
+ try :
193
+ return ApiDependencies .invoker .services .session_queue .cancel_by_batch_ids (
194
+ queue_id = queue_id , batch_ids = batch_ids
195
+ )
196
+ except Exception as e :
197
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while canceling by batch id: { e } " )
173
198
174
199
175
200
@session_queue_router .put (
@@ -182,9 +207,12 @@ async def cancel_by_destination(
182
207
destination : str = Query (description = "The destination to cancel all queue items for" ),
183
208
) -> CancelByDestinationResult :
184
209
"""Immediately cancels all queue items with the given origin"""
185
- return ApiDependencies .invoker .services .session_queue .cancel_by_destination (
186
- queue_id = queue_id , destination = destination
187
- )
210
+ try :
211
+ return ApiDependencies .invoker .services .session_queue .cancel_by_destination (
212
+ queue_id = queue_id , destination = destination
213
+ )
214
+ except Exception as e :
215
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while canceling by destination: { e } " )
188
216
189
217
190
218
@session_queue_router .put (
@@ -197,7 +225,10 @@ async def retry_items_by_id(
197
225
item_ids : list [int ] = Body (description = "The queue item ids to retry" ),
198
226
) -> RetryItemsResult :
199
227
"""Immediately cancels all queue items with the given origin"""
200
- return ApiDependencies .invoker .services .session_queue .retry_items_by_id (queue_id = queue_id , item_ids = item_ids )
228
+ try :
229
+ return ApiDependencies .invoker .services .session_queue .retry_items_by_id (queue_id = queue_id , item_ids = item_ids )
230
+ except Exception as e :
231
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while retrying queue items: { e } " )
201
232
202
233
203
234
@session_queue_router .put (
@@ -211,11 +242,14 @@ async def clear(
211
242
queue_id : str = Path (description = "The queue id to perform this operation on" ),
212
243
) -> ClearResult :
213
244
"""Clears the queue entirely, immediately canceling the currently-executing session"""
214
- queue_item = ApiDependencies .invoker .services .session_queue .get_current (queue_id )
215
- if queue_item is not None :
216
- ApiDependencies .invoker .services .session_queue .cancel_queue_item (queue_item .item_id )
217
- clear_result = ApiDependencies .invoker .services .session_queue .clear (queue_id )
218
- return clear_result
245
+ try :
246
+ queue_item = ApiDependencies .invoker .services .session_queue .get_current (queue_id )
247
+ if queue_item is not None :
248
+ ApiDependencies .invoker .services .session_queue .cancel_queue_item (queue_item .item_id )
249
+ clear_result = ApiDependencies .invoker .services .session_queue .clear (queue_id )
250
+ return clear_result
251
+ except Exception as e :
252
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while clearing queue: { e } " )
219
253
220
254
221
255
@session_queue_router .put (
@@ -229,7 +263,10 @@ async def prune(
229
263
queue_id : str = Path (description = "The queue id to perform this operation on" ),
230
264
) -> PruneResult :
231
265
"""Prunes all completed or errored queue items"""
232
- return ApiDependencies .invoker .services .session_queue .prune (queue_id )
266
+ try :
267
+ return ApiDependencies .invoker .services .session_queue .prune (queue_id )
268
+ except Exception as e :
269
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while pruning queue: { e } " )
233
270
234
271
235
272
@session_queue_router .get (
@@ -243,7 +280,10 @@ async def get_current_queue_item(
243
280
queue_id : str = Path (description = "The queue id to perform this operation on" ),
244
281
) -> Optional [SessionQueueItem ]:
245
282
"""Gets the currently execution queue item"""
246
- return ApiDependencies .invoker .services .session_queue .get_current (queue_id )
283
+ try :
284
+ return ApiDependencies .invoker .services .session_queue .get_current (queue_id )
285
+ except Exception as e :
286
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while getting current queue item: { e } " )
247
287
248
288
249
289
@session_queue_router .get (
@@ -257,7 +297,10 @@ async def get_next_queue_item(
257
297
queue_id : str = Path (description = "The queue id to perform this operation on" ),
258
298
) -> Optional [SessionQueueItem ]:
259
299
"""Gets the next queue item, without executing it"""
260
- return ApiDependencies .invoker .services .session_queue .get_next (queue_id )
300
+ try :
301
+ return ApiDependencies .invoker .services .session_queue .get_next (queue_id )
302
+ except Exception as e :
303
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while getting next queue item: { e } " )
261
304
262
305
263
306
@session_queue_router .get (
@@ -271,9 +314,12 @@ async def get_queue_status(
271
314
queue_id : str = Path (description = "The queue id to perform this operation on" ),
272
315
) -> SessionQueueAndProcessorStatus :
273
316
"""Gets the status of the session queue"""
274
- queue = ApiDependencies .invoker .services .session_queue .get_queue_status (queue_id )
275
- processor = ApiDependencies .invoker .services .session_processor .get_status ()
276
- return SessionQueueAndProcessorStatus (queue = queue , processor = processor )
317
+ try :
318
+ queue = ApiDependencies .invoker .services .session_queue .get_queue_status (queue_id )
319
+ processor = ApiDependencies .invoker .services .session_processor .get_status ()
320
+ return SessionQueueAndProcessorStatus (queue = queue , processor = processor )
321
+ except Exception as e :
322
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while getting queue status: { e } " )
277
323
278
324
279
325
@session_queue_router .get (
@@ -288,7 +334,10 @@ async def get_batch_status(
288
334
batch_id : str = Path (description = "The batch to get the status of" ),
289
335
) -> BatchStatus :
290
336
"""Gets the status of the session queue"""
291
- return ApiDependencies .invoker .services .session_queue .get_batch_status (queue_id = queue_id , batch_id = batch_id )
337
+ try :
338
+ return ApiDependencies .invoker .services .session_queue .get_batch_status (queue_id = queue_id , batch_id = batch_id )
339
+ except Exception as e :
340
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while getting batch status: { e } " )
292
341
293
342
294
343
@session_queue_router .get (
@@ -304,7 +353,12 @@ async def get_queue_item(
304
353
item_id : int = Path (description = "The queue item to get" ),
305
354
) -> SessionQueueItem :
306
355
"""Gets a queue item"""
307
- return ApiDependencies .invoker .services .session_queue .get_queue_item (item_id )
356
+ try :
357
+ return ApiDependencies .invoker .services .session_queue .get_queue_item (item_id )
358
+ except SessionQueueItemNotFoundError :
359
+ raise HTTPException (status_code = 404 , detail = f"Queue item with id { item_id } not found in queue { queue_id } " )
360
+ except Exception as e :
361
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while fetching queue item: { e } " )
308
362
309
363
310
364
@session_queue_router .delete (
@@ -316,7 +370,10 @@ async def delete_queue_item(
316
370
item_id : int = Path (description = "The queue item to delete" ),
317
371
) -> None :
318
372
"""Deletes a queue item"""
319
- ApiDependencies .invoker .services .session_queue .delete_queue_item (item_id )
373
+ try :
374
+ ApiDependencies .invoker .services .session_queue .delete_queue_item (item_id )
375
+ except Exception as e :
376
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while deleting queue item: { e } " )
320
377
321
378
322
379
@session_queue_router .put (
@@ -331,8 +388,12 @@ async def cancel_queue_item(
331
388
item_id : int = Path (description = "The queue item to cancel" ),
332
389
) -> SessionQueueItem :
333
390
"""Deletes a queue item"""
334
-
335
- return ApiDependencies .invoker .services .session_queue .cancel_queue_item (item_id )
391
+ try :
392
+ return ApiDependencies .invoker .services .session_queue .cancel_queue_item (item_id )
393
+ except SessionQueueItemNotFoundError :
394
+ raise HTTPException (status_code = 404 , detail = f"Queue item with id { item_id } not found in queue { queue_id } " )
395
+ except Exception as e :
396
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while canceling queue item: { e } " )
336
397
337
398
338
399
@session_queue_router .get (
@@ -345,9 +406,12 @@ async def counts_by_destination(
345
406
destination : str = Query (description = "The destination to query" ),
346
407
) -> SessionQueueCountsByDestination :
347
408
"""Gets the counts of queue items by destination"""
348
- return ApiDependencies .invoker .services .session_queue .get_counts_by_destination (
349
- queue_id = queue_id , destination = destination
350
- )
409
+ try :
410
+ return ApiDependencies .invoker .services .session_queue .get_counts_by_destination (
411
+ queue_id = queue_id , destination = destination
412
+ )
413
+ except Exception as e :
414
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while fetching counts by destination: { e } " )
351
415
352
416
353
417
@session_queue_router .delete (
@@ -360,6 +424,9 @@ async def delete_by_destination(
360
424
destination : str = Path (description = "The destination to query" ),
361
425
) -> DeleteByDestinationResult :
362
426
"""Deletes all items with the given destination"""
363
- return ApiDependencies .invoker .services .session_queue .delete_by_destination (
364
- queue_id = queue_id , destination = destination
365
- )
427
+ try :
428
+ return ApiDependencies .invoker .services .session_queue .delete_by_destination (
429
+ queue_id = queue_id , destination = destination
430
+ )
431
+ except Exception as e :
432
+ raise HTTPException (status_code = 500 , detail = f"Unexpected error while deleting by destination: { e } " )
0 commit comments