[#4604] Force clearing any lockfiles when submitted from the web

This commit is contained in:
Eric Davis
2010-10-13 12:41:50 -07:00
parent e4f1a623f1
commit f9bd3c0e53
2 changed files with 17 additions and 6 deletions

View File

@@ -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

View File

@@ -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