[#4604] Force clearing any lockfiles when submitted from the web
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user