9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/acts/expireable.rb', line 9
def acts_as_expireable(options = {})
config = {:respawn => false, :lifespan => 10.seconds, :delay=> 0.seconds}
config.update options if options.is_a?(Hash)
unless expireable? cattr_accessor :respawn, :lifespan, :delay
self.respawn = config[:respawn]
self.lifespan = config[:lifespan]
self.delay = config[:delay]
Thread.new do
while true
self.find(:all, :conditions => ["spawn_at <= ? and expires_at >= ?", Time.now, Time.now]).each do |expireable|
expireable.spawn
end
self.find(:all, :conditions => ["expires_at <= ?", Time.now]).each do |expireable|
expireable.expire
end
sleep 0.5
end
end
end
before_create :setup_expireable
include InstanceMethods
rescue
puts $!
end
|