Rails 3 + delayed_job: how can I track the progress of a task (e.g. progress bar on a landing page)?

Delayed_job finally works for me (using the collectidea gem in RoR3) for several tasks. The first is that the administrator uploads the zip image file to the gallery and extracts each image to create a photo of the object belonging to this gallery. The second is deleting galleries. These galleries can be quite large (550 pictures in ~ 3 minutes), and I would like to show that the gallery is still being processed or queued for deletion, even if it is not fully displayed.

Right now, I just use the default method (@ gallery.delay.extract_photos and @ gallery.delay.destroy).

When the administrator clicks on the checkout link, they are directed to the gallery page (which is empty) with a flash message informing them that he is processing the gallery. A page refresh periodically displays a list of images that have been extracted to this point. However, it is not entirely clear when the work ends, as the update once clears the flash message. Basically, I would like to be able to show a progress bar, which is updated every second or so with a quantitative progress next to it ([====== ] 33/86) and / or update the gallery to show images as they are additions.

Removing large galleries can also take a lot of time, so I would like to save this as a deferred work. However, the gallery still exists when you go to the landing page after deleting it, so it appears until it disappears after the update. I would like to either have a way to mark it as being queued for deletion, or show a progress bar and disappear when it finishes the deletion.

I was looking for RoR3 AJAX help, but everything I seem to find was for 2.3.8 and is deprecated. In addition, I need to know how to track whether work is being performed, and I have not noticed anything like this in the documentation for the team. I realized that since jobs are stored in a table, maybe I could track it using DelayedJob.thisjob.exists? or something like that, but trying DelayedJob.all in the rails console just gave me errors (even while running).

So my question is, in a nutshell: how can I track a delayed job and show its progress in any of these ways in a view? [edit] I know that I can do active polls on the gallery pages, but I don’t want this to happen all the time - just while the work is being done (or scheduled). Is there a way to verify the existence of a running task or a scheduled execution? Because I did not find a way to track it in the views.

Thanks.

+4
source share
2 answers

Partial answer ...

Is there a way to verify the existence of work that is or is being planned?

you can use

 Delayed::Job 

to get Active Record jobs in the queue and their states.

 Delayed::Job.find :all , :conditions=>"locked_at IS NOT NULL" 

I would recommend you use resque for fine-grained control over your processes.

+3
source

Do you have the ability to use Nginx and its download modules? I hope so, because it will improve the performance of your application as a whole. Check out this blog post from Gautam Rege, where it uses Nginx, Rails and Delayed Job for relatively large downloads:

http://blog.joshsoftware.com/2010/10/20/uploading-multiple-files-with-nginx-upload-module-and-upload-progress-bar/

The message does not mention whether it uses Rails 3, but since Nginx handles downloads, there is not much Rails code, and you can adjust it if necessary.

+1
source

Source: https://habr.com/ru/post/1334296/


All Articles