rake任务可以通过rails接口触发吗?
还是我在思考这个问题是错误的?基本上,我希望能够将 zip 文件上传到模型,上传后我想在其上运行一堆进程。如果能够从后端/控制台执行此操作,并且有一种通过 Rails 前端接口触发操作的方法,那就太好了。
这里的想法:
画廊有collection.zip
画廊有很多子项:item
collection.zip包含每个项目的信息
我想触发一个批处理gallery.items.build进程,该进程将从zip中提取信息并使用它来创建新的项目。我考虑过使用 Paperclip::Processor 来完成这项任务,但我还没有找到很多真正有用或全面的文档来说明它是如何工作的,而实验只会导致沮丧和困惑。我看到有些人使用 rake 任务来完成这类事情,但我真的不想使用控制台来执行任务,并且真的只想有一个显示“生成图库”的按钮来运行所有必要的任务。
那么,有办法做到这一点吗?这会被视为不好的做法吗?如果是这样,我应该有其他方法来解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从 Rails 内部触发 rake 任务,但我会在类或模块中编写解压缩和处理 zip 文件的逻辑。然后您可以在任一位置使用该代码。
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.
如果您确实想知道如何从 Rails 运行 Rake 任务,Ryan Bates 有一个精彩的免费截屏视频向您展示如何操作。
http://railscasts.com/episodes/127-rake-in-background
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
或resque
被认为是后台处理的最佳实践,而不是运行 rake 任务。两者背后的中心思想是:
lib
中。Redis
或Starling
)在请求周期期间。delayed_job
orresque
are considered to be the best practices for background processing, rather than running a rake task.The central idea behind both:
lib
.Redis
orStarling
) during the request cycle.