Rake task works in development, but not in product

2019-07-30 09:56发布

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条回答
干净又极端
2楼-- · 2019-07-30 10:34

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

查看更多
登录 后发表回答