如何从 rake 任务中调用控制器操作?
我有一个控制器操作,可以生成许多 Excel 报告,这大约需要 10 分钟才能完成。有时我从我的网络应用程序中调用它,这就是它是一个动作的原因。
但我还想创建一个 rake 任务来运行它,这样我就可以安排它每晚自动运行一次。
有办法做到这一点吗?
I have a controller action that generates a number of excel reports, this takes about 10 minutes to do. Sometimes I call it from my webapp, which is why it is an action.
But I also want to create a rake task to run this, so I can schedule it to automatically run once a night.
Any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能处理模型的报告生成吗?模型无论如何都应该完成大部分工作并且可以从 Rake 任务访问:
Can you handle the report generation from your models? Models should be doing most of the work anyway and can be accessed from Rake tasks:
我认为您必须将代码移至模型中。由于将有关输出渲染的知识放入模型中是不好的,因此我建议将所有业务逻辑和数据操作放入模型中,然后将渲染代码放入 rake 任务中。这将使 rake 任务类似于网络上使用的控制器 - 保持关注点分离。
您可以查看 ActionView::Base 并从那里开始了解如何触发模板的渲染。
I think you'll have to move your code into your model. Since it's bad to put knowledge about output rendering in models, I'd suggest putting all the business logic and data manipulation in the model, but then put the rendering code in your rake task. That would make the rake task analogous to the controller used on the web - maintaining separation of concerns.
You can look at ActionView::Base and work from there to figure out how to trigger rendering of templates.