Sha256: 174cb1bb982b283d6f8374a2c313daec68e363a30f353c86b83a449510418c30

Contents?: true

Size: 1.9 KB

Versions: 39

Compression:

Stored size: 1.9 KB

Contents

# encoding: utf-8

require 'thread'

module OneApm
  module Support

    class WorkerLoop

      attr_accessor :period, :propagate_errors
      attr_reader :iterations

      def initialize(opts = {})
        @should_run = true
        @next_invocation_time = Time.now
        @period = 60.0
        @duration = opts[:duration] if opts[:duration]
        @limit = opts[:limit] if opts[:limit]
        @iterations = 0
        @propagate_errors = opts.fetch(:propagate_errors, false)
      end

      def setup(period, task)
        @task = task
        @period = period if period
        @should_run = true
        @iterations = 0

        now = Time.now
        @deadline = now + @duration if @duration
        @next_invocation_time = (now + @period)
      end

      def run(period = nil, &block)
        setup(period, block)
        while keep_running? do
          sleep_time = schedule_next_invocation
          sleep(sleep_time) if sleep_time > 0
          run_task if keep_running?
          @iterations += 1
        end
      end

      def schedule_next_invocation
        now = Time.now
        while @next_invocation_time <= now && @period > 0
          @next_invocation_time += @period
        end
        @next_invocation_time - Time.now
      end

      def keep_running?
        @should_run && under_duration? && under_limit?
      end

      def under_duration?
        !@deadline || Time.now < @deadline
      end

      def under_limit?
        @limit.nil? || @iterations < @limit
      end

      def stop
        @should_run = false
      end

      def run_task
        if @propagate_errors
          @task.call
        else
          begin
            @task.call
          rescue OneApm::ForceRestartException, OneApm::ForceDisconnectException
            raise
          rescue => e
            OneApm::Manager.logger.error "Error running task in Agent Worker Loop:", e
          end
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.4.1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.4.0 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.7 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.6 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.5 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.4 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.3 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.3.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.2 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.2.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.1.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.0 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.3.0.rc1 lib/one_apm/support/event/worker_loop.rb
oneapm_rpm-1.2.9 lib/one_apm/support/event/worker_loop.rb