Rake task works in development, but not in production

淺唱寂寞╮ 提交于 2019-12-11 13:11:03

问题


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?

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

回答1:


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



来源:https://stackoverflow.com/questions/11668208/rake-task-works-in-development-but-not-in-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!