rake任务可以通过rails接口触发吗?

发布于 2024-10-07 12:52:29 字数 477 浏览 5 评论 0原文

还是我在思考这个问题是错误的?基本上,我希望能够将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-10-14 12:52:30

您可以从 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.

无风消散 2024-10-14 12:52:29

如果您确实想知道如何从 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

独自唱情﹋歌 2024-10-14 12:52:29

delayed_jobresque 被认为是后台处理的最佳实践,而不是运行 rake 任务。

两者背后的中心思想是:

  1. 需要在后台运行的批处理脚本或代码保存在 lib 中。
  2. 作业被序列化并存储到数据库(或支持的任何持久层或队列。例如RedisStarling)在请求周期期间。
  3. 在后台运行的守护进程将在队列中查找作业并以各种顺序处理它们(例如,优先级或 fifo 等)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文