From f9bd3c0e53728246b1abc1072f21fe1467949b3a Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Wed, 13 Oct 2010 12:41:50 -0700 Subject: [PATCH] [#4604] Force clearing any lockfiles when submitted from the web --- app/controllers/rate_caches_controller.rb | 4 ++-- app/models/rate.rb | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/controllers/rate_caches_controller.rb b/app/controllers/rate_caches_controller.rb index 5f34e90..1b8fe64 100644 --- a/app/controllers/rate_caches_controller.rb +++ b/app/controllers/rate_caches_controller.rb @@ -23,10 +23,10 @@ class RateCachesController < ApplicationController def update if params[:cache].present? if params[:cache].match(/missing/) - Rate.update_all_time_entries_with_missing_cost + Rate.update_all_time_entries_with_missing_cost(:force => true) flash[:notice] = l(:text_caches_loaded_successfully) elsif params[:cache].match(/reload/) - Rate.update_all_time_entries_to_refresh_cache + Rate.update_all_time_entries_to_refresh_cache(:force => true) flash[:notice] = l(:text_caches_loaded_successfully) end end diff --git a/app/models/rate.rb b/app/models/rate.rb index 58beebd..b4e0870 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -3,6 +3,7 @@ require 'lockfile' class Rate < ActiveRecord::Base unloadable class InvalidParameterException < Exception; end + CACHING_LOCK_FILE_NAME = 'rate_cache' belongs_to :project belongs_to :user @@ -70,8 +71,8 @@ class Rate < ActiveRecord::Base return rate.amount end - def self.update_all_time_entries_with_missing_cost - Lockfile('update_cost_cache', :retries => 0) do + def self.update_all_time_entries_with_missing_cost(options={}) + with_common_lockfile(options[:force]) do TimeEntry.all(:conditions => {:cost => nil}).each do |time_entry| begin time_entry.save_cached_cost @@ -83,8 +84,8 @@ class Rate < ActiveRecord::Base store_cache_timestamp('last_caching_run', Time.now.to_s) end - def self.update_all_time_entries_to_refresh_cache - Lockfile('refresh_cache', :retries => 0) do + def self.update_all_time_entries_to_refresh_cache(options={}) + with_common_lockfile(options[:force]) do TimeEntry.find_each do |time_entry| # batch find begin time_entry.save_cached_cost @@ -139,6 +140,16 @@ class Rate < ActiveRecord::Base Setting.plugin_redmine_rate = Setting.plugin_redmine_rate.merge({cache_name => timestamp}) end + def self.with_common_lockfile(force = false, &block) + # Wait 1 second after stealing a forced lock + options = {:retries => 0, :suspend => 1} + options[:max_age] = 1 if force + + Lockfile(CACHING_LOCK_FILE_NAME, options) do + block.call + end + end + if Rails.env.test? public generator_for :date_in_effect => Date.today