Class: Unleash::ScheduledExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/scheduled_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, interval, max_exceptions = 5) ⇒ ScheduledExecutor

Returns a new instance of ScheduledExecutor.



6
7
8
9
10
11
# File 'lib/unleash/scheduled_executor.rb', line 6

def initialize(name, interval, max_exceptions = 5)
  self.name = name || ''
  self.interval = interval
  self.max_exceptions = max_exceptions
  self.retry_count = 0
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



4
5
6
# File 'lib/unleash/scheduled_executor.rb', line 4

def interval
  @interval
end

#max_exceptionsObject

Returns the value of attribute max_exceptions.



4
5
6
# File 'lib/unleash/scheduled_executor.rb', line 4

def max_exceptions
  @max_exceptions
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/unleash/scheduled_executor.rb', line 4

def name
  @name
end

#retry_countObject

Returns the value of attribute retry_count.



4
5
6
# File 'lib/unleash/scheduled_executor.rb', line 4

def retry_count
  @retry_count
end

Instance Method Details

#run(&blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/unleash/scheduled_executor.rb', line 13

def run(&blk)
  thread = Thread.new do
    Thread.current[:name] = self.name

    loop do
      Unleash.logger.debug "thread #{name} sleeping for #{interval} seconds"
      sleep interval

      Unleash.logger.debug "thread #{name} started"
      begin
        yield
        self.retry_count = 0
      rescue Exception => e
        self.retry_count += 1
        Unleash.logger.error "thread #{name} throwing exception (#{self.retry_count} of #{self.max_exceptions})", e
      end

      break if self.retry_count > self.max_exceptions
    end
  end
end