开发者

Rails - Is it okay (or possible) to run rake inside a controller?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜