aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeiko Bernloehr <Heiko.Bernloehr@FreeIT.de>2014-07-30 03:01:36 +0200
committerHeiko Bernloehr <Heiko.Bernloehr@FreeIT.de>2014-07-30 03:18:50 +0200
commit8d7b012a535b18925b37d0d00d9c33e7183c36e9 (patch)
treecb661bb610bc7fa75a2bd8062ee3a4f653cd9bed
parent5235cc3e6fa882ba2c8bb886276981d898a7c0bd (diff)
downloadecs2-8d7b012a535b18925b37d0d00d9c33e7183c36e9.tar.gz
ecs2-8d7b012a535b18925b37d0d00d9c33e7183c36e9.zip
Moved gc of anonymous participants to rake task.
Don't forget to garbage collect anonymous participants in a cronjob.
-rw-r--r--app/models/participant.rb15
-rw-r--r--lib/tasks/ecs_garbage_collect_anonymous_participants.rake10
2 files changed, 13 insertions, 12 deletions
diff --git a/app/models/participant.rb b/app/models/participant.rb
index 3e7d924..603a10c 100644
--- a/app/models/participant.rb
+++ b/app/models/participant.rb
@@ -17,10 +17,8 @@
class Participant < ActiveRecord::Base
- TTL = 3600 # seconds, how long an anonymous participant lives
+ TTL = 1.hour # how long an anonymous participant lives
- after_save :garbage_collect
- after_create :garbage_collect
after_destroy :delete_messages
belongs_to :organization
@@ -73,7 +71,7 @@ class Participant < ActiveRecord::Base
"dns"=>"N/A",
"organization_id"=>Organization.find_by_name("not available").id,
"email"=>"N/A",
- "ttl"=> DateTime.now.utc + TTL.seconds,
+ "ttl"=> DateTime.now.utc + TTL,
"anonymous"=>true
}
ap = new(params)
@@ -82,7 +80,7 @@ class Participant < ActiveRecord::Base
end
def self.touch_ttl(participant)
- participant.ttl = DateTime.now.utc + TTL.seconds
+ participant.ttl = DateTime.now.utc + TTL
participant.save
end
@@ -93,13 +91,6 @@ class Participant < ActiveRecord::Base
private
- def garbage_collect
- # garbage collect only if a new anonymous participant was created
- if self.anonymous
- Participant.destroy_all(["(anonymous = ?) AND (ttl < ?)", true, DateTime.now.utc])
- end
- end
-
def delete_messages
Message.destroy_all(["sender = ?", self.id])
end
diff --git a/lib/tasks/ecs_garbage_collect_anonymous_participants.rake b/lib/tasks/ecs_garbage_collect_anonymous_participants.rake
new file mode 100644
index 0000000..f0c4836
--- /dev/null
+++ b/lib/tasks/ecs_garbage_collect_anonymous_participants.rake
@@ -0,0 +1,10 @@
+namespace :ecs do
+ desc "Delete outtimed anonymous participants."
+ task :gc_anonymous_participants => :environment do
+ num= Participant.find(:all, :conditions => ["(anonymous = ?) AND (ttl < ?)", true, DateTime.now.utc]).length
+ Participant.destroy_all(["(anonymous = ?) AND (ttl < ?)", true, DateTime.now.utc])
+ txt= "Number of deleted messages: #{num}"
+ puts txt
+ RAILS_DEFAULT_LOGGER.info txt
+ end
+end