Sha256: 11d1ece5594a039610d8b9c9a84940cf6ce69cbed1b7207664c46ea793520aed

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module GoodJob
  class ProbeServer
    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 = HttpServer.new(self, port: @port, logger: GoodJob.logger)
      @future = Concurrent::Future.new { @handler.run }
      @future.add_observer(self.class, :task_observer)
      @future.execute
    end

    def running?
      @handler&.running?
    end

    def stop
      @handler&.stop
      @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

8 entries across 8 versions & 1 rubygems

Version Path
good_job-3.18.2 lib/good_job/probe_server.rb
good_job-3.18.1 lib/good_job/probe_server.rb
good_job-3.18.0 lib/good_job/probe_server.rb
good_job-3.17.4 lib/good_job/probe_server.rb
good_job-3.17.3 lib/good_job/probe_server.rb
good_job-3.17.2 lib/good_job/probe_server.rb
good_job-3.17.1 lib/good_job/probe_server.rb
good_job-3.17.0 lib/good_job/probe_server.rb