dimanche 17 janvier 2016

Unable to invalidate low level cache in Rails 3

I have a scheduler which invalidates cache by running a rake task.

app/models/parameter.rb

class Parameter < ActiveRecord::Base
  after_update :invalidate_bliss_config

  def invalidate_bliss_config
    Parameter.invalidate_config
  end

  def self.bliss_config
    Rails.cache.fetch('bliss_config') { Parameter.first }
  end

  def self.invalidate_config
    Rails.cache.delete('bliss_config')
  end

  def self.auto_change_event_horizon
    Parameter.first.update_attributes!(event_horizon: (Time.zone.today.beginning_of_month + 5.month).end_of_month)
  end
end

config/schedule.rb

set :output, "#{File.expand_path(File.dirname(__FILE__))}/../log/cron_log.log"
set :rails_root, "#{File.expand_path(File.dirname(__FILE__))}/../"
set :environment, "production"

# Move event horizon on first day  of every month.
every '0 0 1 * *' do
  command "cd #{rails_root} && rvm use 1.9.3 && rvm gemset use rails_3_2_21 && RAILS_ENV=#{environment} bundle exec rake bliss:update_event_horizon"
end

lib/tasks/bliss.rake

namespace :bliss do
  task :update_event_horizon => :environment do
    Parameter.auto_change_event_horizon
  end
end

In UI, we access attribute value using Parameter.bliss_config.event_horizon

After running the task if I check value of attribute in UI, it doesn't return latest change. However if I check through rails console it returns latest value. If I restart the server, it take latest value(expected!).

P.S. 1. This issue occurs in production only. It works perfectly in development. 2. If I change this config from UI, it takes latest value.

Aucun commentaire:

Enregistrer un commentaire