本文介绍了Rake 任务在开发中有效,但在生产中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,每隔几个小时就会更改主页上的数据.我已经对其进行了测试,并且在开发中运行良好.但它在生产中不起作用.我需要做什么才能获得我想要看到的更改?我应该添加一个重新启动服务器的命令吗?这会让服务器承认更改吗?有没有更聪明的方法来做到这一点?

I've got a rake task that changes data on the homepage every few hours. I've tested it out and it works fine in development. But it doesn't work in production. What do I have to do to get the changes I want to see? Should I add a command that restarts the server? Would that make the server acknowledge the change? Is there a smarter way to do this?

抽水任务如下.它将由 heroku 的调度程序插件运行,因此它当前位于 lib/tasks/scheduler.rake 文件中.

The rake task is below. It'll be run by heroku's scheduler add on, so it's currently in the lib/tasks/scheduler.rake file.

desc 'changes the meta tags'
task :mixup_meta_tags => :environment do
  regex = /@meta_tag/
  file = File.open('app/controllers/site_controller.rb', 'r')
  lines = []
  file.each_line do |line|
    (line =~ regex) ? (lines << replace_line(line)) : (lines << line)
  end
  file.close
  file = File.open('app/controllers/site_controller.rb', 'w')
  lines.each{|line| file.write line}
  file.close
end

def replace_line(line)
  meta_tags = MetaTag.all.map { |tag| tag["tag"] }
  new_tag = meta_tags.sample(1)[0]
  line = "    @meta_tag = \"#{new_tag}\" \n" # added the newline
end

推荐答案

是的,在生产环境中对 Rails 应用程序的更改需要重新启动才能被服务器接收.为了让它即时工作,您可能想尝试这篇文章中提到的解决方案 为什么-code-need-to-be-reloaded-in-rails-3

Yes, changes to your Rails application in Production require a restart for them to get picked up by the server. To get this to work on the fly you might want to try the solution mentioned in this post why-does-code-need-to-be-reloaded-in-rails-3

这篇关于Rake 任务在开发中有效,但在生产中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:51