Reputation: 1574
In my application, I'm using Resque to resize images. If an image is in the resizing queue, I want to show a "resizing image" icon.
This means that I need to be able to find all current jobs relating to a specific model ID in the queue. Right now I do it like this:
Resque.peek(:resize, 0, 100).find_all { |job| /#{model.id}/.match(job["args"][0]) }
This is stupid. But is there any way to query the Resque queue to find all jobs where the first argument is equal to [id]?
Thanks in advance.
Upvotes: 9
Views: 13840
Reputation: 4255
I think the easiest way might be to use a redis set to cache this information.
When you add an image to the 'resize' queue, also add the image id to the 'resize_in_progress' set using SADD. (I assume you have some kind of unique key or name to refer to the image, even if not stored in the db. Maybe the full path to the filename.)
In the 'resize' process, as one of the final actions after successfully resizing the image, remove it from the set using the SREM command.
When you want a list of all images, you can fetch that with SMEMBERS. If you want just the members for a specific model id, you might need to store a separate set for each model, named something like 'resize_in_progress_3451' where 3451 is the id of the model that has the images being resized.
See http://redis.io/commands#set for more set commands.
Upvotes: 0
Reputation: 32715
Give resque-status a try. It is an extension to Resque that adds job tracking.
resque-status provides a set of simple classes that extend resque’s default functionality (with 0% monkey patching) to give apps a way to track specific job instances and their status. It achieves this by giving job instances UUID’s and allowing the job instances to report their status from within their iterations.
Note: d11wtq mentioned this above as a comment, but is actually the best answer so far.
Upvotes: 10
Reputation: 31538
Instead of querying resque queue, you should store image meta-data along with your model.
Lets assume you are storing product images. You are likely using a Redis hash to store product details. Just add another flag like this -
hset product:123 is_resizing true
You can them perform a simple lookup to show the resizing image icon. At the end of your resque job, delete the is_resizing
key, and add the resized_image_url
key.
Upvotes: 5