Sha256: 84f3511277bb1f741faf50c44673ba0049eafe55adcbc23be99f9a28d3f753cf

Contents?: true

Size: 1.61 KB

Versions: 77

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module GoodJob
  class ProbeServer
    RACK_SERVER = 'webrick'

    def self.task_observer(time, output, thread_error) # rubocop:disable Lint/UnusedMethodArgument
      return if thread_error.is_a? Concurrent::CancelledOperationError

      GoodJob._on_thread_error(thread_error) if thread_error
    end

    def initialize(port:)
      @port = port
    end

    def start
      @handler = Rack::Handler.get(RACK_SERVER)
      @future = Concurrent::Future.new(args: [@handler, @port, GoodJob.logger]) do |thr_handler, thr_port, thr_logger|
        thr_handler.run(self, Port: thr_port, Host: '0.0.0.0', Logger: thr_logger, AccessLog: [])
      end
      @future.add_observer(self.class, :task_observer)
      @future.execute
    end

    def running?
      @handler&.instance_variable_get(:@server)&.status == :Running
    end

    def stop
      @handler&.shutdown
      @future&.value # wait for Future to exit
    end

    def call(env)
      case Rack::Request.new(env).path
      when '/', '/status'
        [200, {}, ["OK"]]
      when '/status/started'
        started = GoodJob::Scheduler.instances.any? && GoodJob::Scheduler.instances.all?(&:running?)
        started ? [200, {}, ["Started"]] : [503, {}, ["Not started"]]
      when '/status/connected'
        connected = GoodJob::Scheduler.instances.any? && GoodJob::Scheduler.instances.all?(&:running?) &&
                    GoodJob::Notifier.instances.any? && GoodJob::Notifier.instances.all?(&:listening?)
        connected ? [200, {}, ["Connected"]] : [503, {}, ["Not connected"]]
      else
        [404, {}, ["Not found"]]
      end
    end
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
good_job-3.16.4 lib/good_job/probe_server.rb
good_job-3.16.3 lib/good_job/probe_server.rb
good_job-3.16.2 lib/good_job/probe_server.rb
good_job-3.16.1 lib/good_job/probe_server.rb
good_job-3.16.0 lib/good_job/probe_server.rb
good_job-3.15.14 lib/good_job/probe_server.rb
good_job-3.15.13 lib/good_job/probe_server.rb
good_job-3.15.12 lib/good_job/probe_server.rb
good_job-3.15.11 lib/good_job/probe_server.rb
good_job-3.15.10 lib/good_job/probe_server.rb
good_job-3.15.9 lib/good_job/probe_server.rb
good_job-3.15.8 lib/good_job/probe_server.rb
good_job-3.15.7 lib/good_job/probe_server.rb
good_job-3.15.6 lib/good_job/probe_server.rb
good_job-3.15.5 lib/good_job/probe_server.rb
good_job-3.15.4 lib/good_job/probe_server.rb
good_job-3.15.3 lib/good_job/probe_server.rb
good_job-3.15.2 lib/good_job/probe_server.rb
good_job-3.15.1 lib/good_job/probe_server.rb
good_job-3.15.0 lib/good_job/probe_server.rb