lib/taskinator/persistence.rb in taskinator-0.3.5 vs lib/taskinator/persistence.rb in taskinator-0.3.6

- old
+ new

@@ -192,32 +192,27 @@ end yaml ? Taskinator::Persistence.deserialize(yaml) : {} end end - def cleanup + EXPIRE_IN = 10 * 60 # 10 minutes + + def cleanup(expire_at=Time.now+EXPIRE_IN) Taskinator.redis do |conn| - process_key = self.process_key + # use the "clean up" visitor + RedisCleanupVisitor.new(conn, self, expire_at).visit - # delete processes/tasks data - conn.scan_each(:match => "#{process_key}:*", :count => 1000) do |key| - conn.del(key) - end - - # remove the process - conn.del process_key - # remove from the list - conn.srem Persistence.processes_list_key(scope), uuid + conn.srem(Persistence.processes_list_key(scope), uuid) end end end - class RedisSerializationVisitor < Visitor::Base + class RedisSerializationVisitor < Taskinator::Visitor::Base # # the redis connection is passed in since it is # in the multi statement mode in order to produce # one roundtrip to the redis server @@ -450,9 +445,39 @@ conn.hget(base.key_for(uuid), :type) end klass = Kernel.const_get(type) LazyLoader.new(klass, uuid, @instance_cache) end + end + + class RedisCleanupVisitor < Taskinator::Visitor::Base + + attr_reader :instance + attr_reader :expire_at + + def initialize(conn, instance, expire_at) + @conn = conn + @instance = instance + @expire_at = expire_at.utc + @key = instance.key + end + + def visit + @instance.accept(self) + @conn.expireat(@key, expire_at.to_i) + end + + def visit_process(attribute) + process = @instance.send(attribute) + RedisCleanupVisitor.new(@conn, process, expire_at).visit if process + end + + def visit_tasks(tasks) + tasks.each do |task| + RedisCleanupVisitor.new(@conn, task, expire_at).visit + end + end + end # lazily loads the object specified by the type and uuid class LazyLoader < Delegator