Pathong
Pathong

Reputation: 73

Signaling a long running Huey task to stop

I'm using Huey to perform some processing operations on objects I have in my Django application.

@db_task()
def process_album(album: Album) -> None:
    images = album.objects.find_non_processed_images()

    for image in images:
        if album.should_pause():
            return

        process_image(album, image)

This is a simplified example of the situation I wish to solve. I have an Album model that stores a various number of Images that are required to be processed. The processing operation is defined in a different function that s wrapped with @task decorator so it'll be able to run concurrently (when the number of workers > 1).

The question is how to implement album.should_pause() in the right way. Current implementation looks like this:

def should_pause(self):
    self.refresh_from_db()
    return self.processing_state != AlbumProcessingState.RUNNING

Therefore, on each iteration, the database is queried to update the model, to make sure that state field didn't change to something other than AlbumProcessingState.RUNNING, which would indicate that the album processing tasks should break.

Although it works, it feels wrong since I have to update the model from the database on each iteration, but these feelings might be false. What do you think?

Upvotes: 1

Views: 640

Answers (0)

Related Questions