From 8d7b012a535b18925b37d0d00d9c33e7183c36e9 Mon Sep 17 00:00:00 2001 From: Heiko Bernloehr Date: Wed, 30 Jul 2014 03:01:36 +0200 Subject: Moved gc of anonymous participants to rake task. Don't forget to garbage collect anonymous participants in a cronjob. --- app/models/participant.rb | 15 +++------------ lib/tasks/ecs_garbage_collect_anonymous_participants.rake | 10 ++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 lib/tasks/ecs_garbage_collect_anonymous_participants.rake 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 -- cgit v1.2.3