Rails - 可以(或可能)在控制器内运行 rake 吗?
我正在使用 sitemap_generator gem 构建 xml 站点地图。来自自述文件:
...根据需要运行 rake sitemap:refresh 来创建/重建站点地图文件
我希望在内容控制器中运行 create
操作时执行此操作。是否有做这样的事情的最佳实践?
I am using the sitemap_generator gem to build an xml sitemap. From the readme:
... run rake sitemap:refresh as needed to create/rebuild your Sitemap files
I'd prefer to do this any time the create
action is ran in my Content controller. Is there a best practice for doing something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的,是的。但我不会推荐它。 Rake 任务往往需要至少几秒钟的时间来运行,这将占用服务器请求并延长对客户端的响应。
如果您想在每次创建后刷新站点地图,那么我会推荐两种解决方案之一。分析 rake 任务 sitemap:refresh 的作用并直接使用控制器中的代码。但我只会这样做,只要它不会花费太多时间来运行,并且由于我对 sitemap_generator 不太了解,所以我无法判断。
另一种选择是从 delayed_job 运行 rake 任务,我发现这是首选替代方案。这样,您可以从创建操作触发作业,但不必等待它完成。
It is possible, yes. But I would not recommend it. Rake tasks tend to take at least a few seconds to run which will occupy a server request and prolong the response to the client.
If you want to refresh the sitemap after every create, then I would recommend one of two solutions. Either analyse what the rake task sitemap:refresh does and use the code directly from your controller. But I would only do that as long as it doesn't take too much time to run and since I do not know much about sitemap_generator, I can't tell.
The other option would be to run the rake task from a delayed_job which I found to be the preferred alternative. That way, you can trigger the job from your create action but you don't have to wait for it to finish.