File: scheduled_executor.rb

package info (click to toggle)
ruby-unleash 3.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,098; makefile: 10; sh: 4
file content (66 lines) | stat: -rwxr-xr-x 1,817 bytes parent folder | download
1
2
3
4
5
6
7
8
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Unleash
  class ScheduledExecutor
    attr_accessor :name, :interval, :max_exceptions, :retry_count, :thread

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

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

        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

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

    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

    private

    def run_blk(&blk)
      Unleash.logger.debug "thread #{name} starting execution"

      yield(blk)
      self.retry_count = 0
    rescue StandardError => e
      self.retry_count += 1
      Unleash.logger.error "thread #{name} threw exception #{e.class} " \
          " (#{self.retry_count}/#{self.max_exceptions}): '#{e}'"
      Unleash.logger.debug "stacktrace: #{e.backtrace}"
    end

    def exceeded_max_exceptions?
      self.retry_count > self.max_exceptions
    end
  end
end