Add tasks to generate and expire stats cache

We're generating stats every 2 hours because it's less than the time it
will take to generate stats for every process. Once stats are generated,
this task should take less than a second.

The regenerate task has been added so we can manually execute it.
This commit is contained in:
Javi Martín
2019-04-08 16:27:23 +02:00
parent aa0e813970
commit 13dd77dd1b
2 changed files with 28 additions and 0 deletions

View File

@@ -27,6 +27,10 @@ every 1.day, at: "5:00 am" do
rake "-s sitemap:refresh"
end
every 2.hours do
rake "-s stats:generate"
end
# Temporally not send dashboard's notifications
# every 1.day, at: "7:00 am" do
# rake "dashboards:send_notifications"

24
lib/tasks/stats.rake Normal file
View File

@@ -0,0 +1,24 @@
namespace :stats do
desc "Generates stats which are not cached yet"
task generate: :environment do
Budget.find_each do |budget|
Budget::Stats.new(budget).generate
print "."
end
Poll.find_each do |poll|
Poll::Stats.new(poll).generate
print "."
end
end
desc "Expires stats cache"
task expire_cache: :environment do
[Budget, Poll].each do |model_class|
model_class.find_each { |record| record.find_or_create_stats_version.touch }
end
end
desc "Deletes stats cache and generates it again"
task regenerate: [:expire_cache, :generate]
end