Class: Unleash::ScheduledExecutor
- Inherits:
-
Object
- Object
- Unleash::ScheduledExecutor
- Defined in:
- lib/unleash/scheduled_executor.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#max_exceptions ⇒ Object
Returns the value of attribute max_exceptions.
-
#name ⇒ Object
Returns the value of attribute name.
-
#retry_count ⇒ Object
Returns the value of attribute retry_count.
Instance Method Summary collapse
-
#initialize(name, interval, max_exceptions = 5) ⇒ ScheduledExecutor
constructor
A new instance of ScheduledExecutor.
- #run(&blk) ⇒ Object
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
#interval ⇒ Object
Returns the value of attribute interval.
4 5 6 |
# File 'lib/unleash/scheduled_executor.rb', line 4 def interval @interval end |
#max_exceptions ⇒ Object
Returns the value of attribute max_exceptions.
4 5 6 |
# File 'lib/unleash/scheduled_executor.rb', line 4 def max_exceptions @max_exceptions end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/unleash/scheduled_executor.rb', line 4 def name @name end |
#retry_count ⇒ Object
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 |