File: recurring_executor.rb

package info (click to toggle)
ruby-jaeger-client 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: ruby: 3,381; makefile: 6; sh: 4
file content (35 lines) | stat: -rw-r--r-- 622 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
# frozen_string_literal: true

module Jaeger
  # Executes a given block periodically. The block will be executed only once
  # when interval is set to 0.
  class RecurringExecutor
    def initialize(interval:)
      @interval = interval
    end

    def start(&block)
      raise 'Already running' if @thread

      @thread = Thread.new do
        if @interval <= 0
          yield
        else
          loop do
            yield
            sleep @interval
          end
        end
      end
    end

    def running?
      @thread&.alive?
    end

    def stop
      @thread.kill
      @thread = nil
    end
  end
end