Class: Spider::PeriodicRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/utils/periodic_runner.rb

Overview

TODO: remove

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sleep_time = nil) ⇒ PeriodicRunner

Returns a new instance of PeriodicRunner.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spiderfw/utils/periodic_runner.rb', line 7

def initialize(sleep_time=nil)
    @entries = []
    @sleep_time = sleep_time
    @mutex = Mutex.new
    if (sleep_time)
        @runner_thread = Thread.new{
            while (true)
                sleep(sleep_time)
                run
            end
        }
    end
end

Instance Attribute Details

#runner_threadObject (readonly)

:nodoc:



5
6
7
# File 'lib/spiderfw/utils/periodic_runner.rb', line 5

def runner_thread
  @runner_thread
end

Instance Method Details

#add(secs, &proc) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/spiderfw/utils/periodic_runner.rb', line 21

def add(secs, &proc)
    proc.call
    @mutex.synchronize {
        @entries << {
            :secs => secs,
            :proc => proc,
            :last => Time.now.to_i
        }
    }
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spiderfw/utils/periodic_runner.rb', line 32

def run
    Spider::Logger.debug("Periodic_runner")
    @mutex.synchronize {
        Spider::Logger.debug("In synchro")
        @entries.each do |entry|
            if (!entry[:last] || (entry[:last] + secs) < Time.now.to_i )
                proc.call
                entry[:last] = Time.now.to_i
            end
        end
    }
end