Or am I thinking about this wrong? Basically, I want to be able to upload a zip file to a model, and after uploading I want to run a bunch of processes on it. It would be nice to be able to do this from the back end/console, as well as have a way of triggering the actions via the rails front-end interface.
The idea here:
Gallery has collection.zip
Gallery has many children :item
collection.zip has information for each Item
I want to trigger a batch gallery.items.build process that will extract the information from the zip and use it to create the new items. I thought about using a Paperclip::Processor for this task, but I have yet to find a lot of truly useful or comprehensive documentation on how that would work, and experimentation has only led to frustratio开发者_Go百科n and confusion. I saw some people use rake tasks for this sort of thing, but I really don't want to have to use the console to do perform the task, and would really just like to have a button that says "Generate Gallery" that will run all the necessary tasks.
So, is there a way of doing this? Would this be considered bad practice? If so, is there another way that I should be approaching this problem?
If you do want to know how to run a rake task from rails, Ryan Bates has an excellent free screencast that show you how.
http://railscasts.com/episodes/127-rake-in-background
delayed_job
or resque
are considered to be the best practices for background processing, rather than running a rake task.
The central idea behind both:
- Batch scripts or code that needs to be run in background is kept in
lib
. - Jobs are serialized and stored to DB (or any persistence layer or queue that is supported. eg.
Redis
orStarling
) during the request cycle. - Daemon that runs in background, will look for jobs in the queue and process them in various orders (say, priority or fifo etc)
You can trigger a rake task from within rails, but I'd write the logic to unzip and process the zip file in a class or module. Then you could use that code in either place.
精彩评论