Josh Kovach
Josh Kovach

Reputation: 7749

Can a rake task be triggered via a rails interface?

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 frustration 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?

Upvotes: 1

Views: 1663

Answers (3)

Swanand
Swanand

Reputation: 12426

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:

  1. Batch scripts or code that needs to be run in background is kept in lib.
  2. Jobs are serialized and stored to DB (or any persistence layer or queue that is supported. eg. Redis or Starling) during the request cycle.
  3. Daemon that runs in background, will look for jobs in the queue and process them in various orders (say, priority or fifo etc)

Upvotes: 4

Greg Fairbrother
Greg Fairbrother

Reputation: 1041

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

Upvotes: 4

idlefingers
idlefingers

Reputation: 32047

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.

Upvotes: 2

Related Questions