Skip to content

Commit

Permalink
[#4604] Add a method and rake task to clear and update all TimeEntry …
Browse files Browse the repository at this point in the history
…cost caches
  • Loading branch information
edavis10 committed Oct 7, 2010
1 parent 16a7a31 commit fb3b0a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/models/rate.rb
Expand Up @@ -82,6 +82,19 @@ def self.update_all_time_entries_with_missing_cost
end
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
TimeEntry.find_each do |time_entry| # batch find
begin
time_entry.save_cached_cost
rescue Rate::InvalidParameterException => ex
puts "Error saving #{time_entry.id}: #{ex.message}"
end
end
end
store_cache_timestamp('last_cache_clearing_run', Time.now.to_s)
end

private
def self.for_user_project_and_date(user, project, date)
Expand Down
7 changes: 6 additions & 1 deletion lib/tasks/cache.rake
@@ -1,8 +1,13 @@
namespace :rate_plugin do
namespace :cache do
desc "Update Time Entry cost caches"
desc "Update Time Entry cost caches for Time Entries without a cost"
task :update_cost_cache => :environment do
Rate.update_all_time_entries_with_missing_cost
end

desc "Clear and update all Time Entry cost caches"
task :refresh_cost_cache => :environment do
Rate.update_all_time_entries_to_refresh_cache
end
end
end
29 changes: 29 additions & 0 deletions test/unit/rate_test.rb
Expand Up @@ -301,4 +301,33 @@ def rate_valid_attributes
assert Time.parse(Setting.plugin_redmine_rate['last_caching_run']), "Last run timestamp not parseable"
end
end

context "#update_all_time_entries_to_refresh_cache" do
setup do
@user = User.generate!
@project = Project.generate!
@date = Date.today.to_s
@rate = Rate.generate!(:user => @user, :project => @project, :date_in_effect => @date, :amount => 200.0)
@time_entry1 = TimeEntry.generate!({:user => @user, :project => @project, :spent_on => @date, :hours => 10.0, :activity => TimeEntryActivity.generate!})
@time_entry2 = TimeEntry.generate!({:user => @user, :project => @project, :spent_on => @date, :hours => 20.0, :activity => TimeEntryActivity.generate!})
end

should "update the caches of all Time Entries" do
assert_equal "0", ActiveRecord::Base.connection.select_all('select count(*) as count from time_entries where cost IS NULL').first["count"]

Rate.update_all_time_entries_to_refresh_cache

assert_equal "0", ActiveRecord::Base.connection.select_all('select count(*) as count from time_entries where cost IS NULL').first["count"]

end

should "timestamp a successful run" do
assert_equal nil, Setting.plugin_redmine_rate['last_cache_clearing_run']

Rate.update_all_time_entries_to_refresh_cache

assert Setting.plugin_redmine_rate['last_cache_clearing_run'], "Last run not timestamped"
assert Time.parse(Setting.plugin_redmine_rate['last_cache_clearing_run']), "Last run timestamp not parseable"
end
end
end

0 comments on commit fb3b0a4

Please sign in to comment.