procrastinate icon indicating copy to clipboard operation
procrastinate copied to clipboard

Get the already enqueued task ID/PK on AlreadyEnqueued exception

Open mecampbellsoup opened this issue 4 years ago • 5 comments

Hey guys -

This question is regarding queueing locks and AlreadyEnqueued errors.

It would be really cool (!!) if we could do something like:

try:
  enqueue_a_task_and_block()
except AlreadyEnqueued as e:
  tid = e.args[0]  # the already-enqueued task's ID/PK
  await check_task_is_completed(tid)  # this function blocks application code from continuing until the task is done, perhaps

Would it be possible to get the already enqueued task's ID on the exception instance? Is this already available somehow? Thanks!

mecampbellsoup avatar Sep 22 '21 18:09 mecampbellsoup

I think it's kind of like https://github.com/peopledoc/procrastinate/issues/370 ? Do you confirm ?

Ah or is it about updating the exception to attach information on the task that is blocked ?

ewjoachim avatar Sep 23 '21 08:09 ewjoachim

Ah or is it about updating the exception to attach information on the task that is blocked ?

Yea, this! The context is that we have some spots in our application code that need to block until a particular task completes. With queueing locks, we may try to enqueue some task, only to discover that for some reason that task has already been enqueued. In this case, the application code (Python ASGI web app) should wait for the existing task to complete.

mecampbellsoup avatar Sep 23 '21 15:09 mecampbellsoup

This is a valid usecase. You could also use normal locks instead of queuing locks, and have your task check at the beginning if it hasn't actually been done and exit immediately if so.

ewjoachim avatar Sep 23 '21 16:09 ewjoachim

I thought it would be a "simple" matter of improving the exception, but it might require changing SQL a bit. Not sure though.

ewjoachim avatar Sep 27 '21 08:09 ewjoachim

I thought it would be a "simple" matter of improving the exception, but it might require changing SQL a bit. Not sure though.

Yes it will definitely require changing some SQL as far as I can tell. I'm not sure if Postgres provides a way to identify the conflicting record's PK/ID when some constraint is violated... but that would be great if so!

mecampbellsoup avatar Sep 28 '21 15:09 mecampbellsoup