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, immediate_execution = false) ⇒ ScheduledExecutor

Returns a new instance of ScheduledExecutor.



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

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

Instance Attribute Details

#immediate_executionObject

Returns the value of attribute immediate_execution.



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

def immediate_execution
  @immediate_execution
end

#intervalObject

Returns the value of attribute interval.



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

def interval
  @interval
end

#max_exceptionsObject

Returns the value of attribute max_exceptions.



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

def max_exceptions
  @max_exceptions
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#retry_countObject

Returns the value of attribute retry_count.



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

def retry_count
  @retry_count
end

#threadObject

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#exitObject



41
42
43
44
45
46
47
48
49
# File 'lib/unleash/scheduled_executor.rb', line 41

def exit
  if self.running?
    Unleash.logger.warn "thread #{name} will exit!"
    self.thread.exit
    self.thread.join if self.running?
  else
    Unleash.logger.info "thread #{name} was already stopped!"
  end
end

#run(&blk) ⇒ Object



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

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

    run_blk{ blk.call } if self.immediate_execution

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

      run_blk{ blk.call }

      if exceeded_max_exceptions?
        Unleash.logger.error "thread #{name} retry_count (#{self.retry_count}) exceeded " \
            "max_exceptions (#{self.max_exceptions}). Stopping with retries."
        break
      end
    end
    Unleash.logger.debug "thread #{name} loop ended"
  end
end

#running?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/unleash/scheduled_executor.rb', line 37

def running?
  self.thread.is_a?(Thread) && self.thread.alive?
end