Sha256: c8502767456ae2a4657effe0534bc0f5f3c8a962f6d5b2fcdd13184fb8637e60

Contents?: true

Size: 1.92 KB

Versions: 29

Compression:

Stored size: 1.92 KB

Contents

module Puma
  module App
    class Status
      def initialize(cli)
        @cli = cli
        @auth_token = nil
      end
      OK_STATUS = '{ "status": "ok" }'.freeze

      attr_accessor :auth_token

      def authenticate(env)
        return true unless @auth_token
        env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
      end

      def rack_response(status, body, content_type='application/json')
        headers = {
          'Content-Type' => content_type,
          'Content-Length' => body.bytesize.to_s
        }

        [status, headers, [body]]
      end

      def call(env)
        unless authenticate(env)
          return rack_response(403, 'Invalid auth token', 'text/plain')
        end

        case env['PATH_INFO']
        when /\/stop$/
          @cli.stop
          return rack_response(200, OK_STATUS)

        when /\/halt$/
          @cli.halt
          return rack_response(200, OK_STATUS)

        when /\/restart$/
          @cli.restart
          return rack_response(200, OK_STATUS)

        when /\/phased-restart$/
          if !@cli.phased_restart
            return rack_response(404, '{ "error": "phased restart not available" }')
          else
            return rack_response(200, OK_STATUS)
          end

        when /\/reload-worker-directory$/
          if !@cli.send(:reload_worker_directory)
            return rack_response(404, '{ "error": "reload_worker_directory not available" }')
          else
            return rack_response(200, OK_STATUS)
          end

        when /\/gc$/
          GC.start
          return rack_response(200, OK_STATUS)

        when /\/gc-stats$/
          json = "{" + GC.stat.map { |k, v| "\"#{k}\": #{v}" }.join(",") + "}"
          return rack_response(200, json)

        when /\/stats$/
          return rack_response(200, @cli.stats)
        else
          rack_response 404, "Unsupported action", 'text/plain'
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 4 rubygems

Version Path
piesync-puma-3.12.6.1 lib/puma/app/status.rb
piesync-puma-3.12.6 lib/puma/app/status.rb
puma-3.12.6-java lib/puma/app/status.rb
puma-3.12.6 lib/puma/app/status.rb
puma-3.12.5-java lib/puma/app/status.rb
puma-3.12.5 lib/puma/app/status.rb
puma-3.12.4-java lib/puma/app/status.rb
puma-3.12.4 lib/puma/app/status.rb
puma-3.12.2-java lib/puma/app/status.rb
puma-3.12.2 lib/puma/app/status.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/puma-3.12.1/lib/puma/app/status.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/puma-3.12.1/lib/puma/app/status.rb
puma-3.12.1-java lib/puma/app/status.rb
puma-3.12.1 lib/puma/app/status.rb
puma-3.12.0-java lib/puma/app/status.rb
puma-3.12.0 lib/puma/app/status.rb
puma-3.11.4-java lib/puma/app/status.rb
puma-3.11.4 lib/puma/app/status.rb
tdiary-5.0.8 vendor/bundle/gems/puma-3.11.3/lib/puma/app/status.rb
puma-3.11.3-java lib/puma/app/status.rb