rack-ecg 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/basic.ru +0 -1
- data/examples/checks.ru +0 -1
- data/examples/mounted_path.ru +0 -1
- data/examples/stand_alone.ru +3 -0
- data/lib/rack/ecg.rb +22 -46
- data/lib/rack/ecg/check.rb +17 -0
- data/lib/rack/ecg/check/error.rb +16 -0
- data/lib/rack/ecg/check/git_revision.rb +23 -0
- data/lib/rack/ecg/check/http.rb +16 -0
- data/lib/rack/ecg/check/migration_version.rb +31 -0
- data/lib/rack/ecg/check_registry.rb +21 -0
- data/lib/rack/ecg/version.rb +1 -1
- metadata +9 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6f59a0b33808baa8d2e2e9fce089e2cfdf03668c
         | 
| 4 | 
            +
              data.tar.gz: 3cc238f62ea9afc55dccea25814b24aecc9975aa
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: f122cef317b2d199fe20075495826a9581af55042addceb61e7ce3f9a32ffb92f3a566590e4b2afa33ee78ca00da9b624265ae9fa1d06363066edb452da64944
         | 
| 7 | 
            +
              data.tar.gz: 2cf179f722e4024aaf52c3a5c10d676e083a88c4a1b5b5fdc3b6cd63ed5ec9b7c5cf85f384bc35504a95aed0e085e73335638514c1d9b2aae3848728c36622d4
         | 
    
        data/examples/basic.ru
    CHANGED
    
    
    
        data/examples/checks.ru
    CHANGED
    
    
    
        data/examples/mounted_path.ru
    CHANGED
    
    
    
        data/lib/rack/ecg.rb
    CHANGED
    
    | @@ -1,24 +1,29 @@ | |
| 1 1 | 
             
            require "rack/ecg/version"
         | 
| 2 2 | 
             
            require "json"
         | 
| 3 3 | 
             
            require "open3"
         | 
| 4 | 
            +
            require "rack/ecg/check"
         | 
| 4 5 |  | 
| 5 6 | 
             
            module Rack
         | 
| 6 7 | 
             
              class ECG
         | 
| 7 8 | 
             
                DEFAULT_MOUNT_AT = "/_ecg"
         | 
| 8 | 
            -
                DEFAULT_CHECKS = [ : | 
| 9 | 
            +
                DEFAULT_CHECKS = [ :http ]
         | 
| 9 10 |  | 
| 10 | 
            -
                def initialize(app, options={})
         | 
| 11 | 
            +
                def initialize(app=nil, options={})
         | 
| 11 12 | 
             
                  @app = app
         | 
| 12 | 
            -
             | 
| 13 | 
            -
                   | 
| 14 | 
            -
                  @ | 
| 13 | 
            +
             | 
| 14 | 
            +
                  check_names = options.delete(:checks) || []
         | 
| 15 | 
            +
                  @check_classes = build_check_classes(check_names)
         | 
| 16 | 
            +
             | 
| 15 17 | 
             
                  @at = options.delete(:at) || DEFAULT_MOUNT_AT
         | 
| 16 18 | 
             
                end
         | 
| 17 19 |  | 
| 18 20 | 
             
                def call(env)
         | 
| 19 21 | 
             
                  if env["PATH_INFO"] == @at
         | 
| 20 22 |  | 
| 21 | 
            -
                    check_results = @ | 
| 23 | 
            +
                    check_results = @check_classes.inject({}){|results, check_class|
         | 
| 24 | 
            +
                      check = check_class.new
         | 
| 25 | 
            +
                      results.merge(check.result.to_json)
         | 
| 26 | 
            +
                    }
         | 
| 22 27 |  | 
| 23 28 | 
             
                    response_status = check_results.any?{|check| check[1][:status] == "error" } ? 500 : 200
         | 
| 24 29 |  | 
| @@ -30,51 +35,22 @@ module Rack | |
| 30 35 | 
             
                    response_body = JSON.pretty_generate(check_results)
         | 
| 31 36 |  | 
| 32 37 | 
             
                    [response_status, response_headers, [response_body]]
         | 
| 33 | 
            -
                   | 
| 38 | 
            +
                  elsif @app
         | 
| 34 39 | 
             
                    @app.call(env)
         | 
| 40 | 
            +
                  else
         | 
| 41 | 
            +
                    [404, {},[]]
         | 
| 35 42 | 
             
                  end
         | 
| 36 43 | 
             
                end
         | 
| 37 44 |  | 
| 38 45 | 
             
                private
         | 
| 39 | 
            -
                def  | 
| 40 | 
            -
                   | 
| 41 | 
            -
                  #  | 
| 42 | 
            -
                  { | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
                   | 
| 47 | 
            -
                  {error: {status: "error", value: "PC LOAD LETTER" } }
         | 
| 48 | 
            -
                end
         | 
| 49 | 
            -
             | 
| 50 | 
            -
                def check_git_revision
         | 
| 51 | 
            -
                  _stdin, stdout, stderr, wait_thread = Open3.popen3("git rev-parse HEAD")
         | 
| 52 | 
            -
             | 
| 53 | 
            -
                  success = wait_thread.value.success?
         | 
| 54 | 
            -
                  status = success ? "ok" : "error"
         | 
| 55 | 
            -
                  value = success ? stdout.read : stderr.read
         | 
| 56 | 
            -
                  value = value.strip
         | 
| 57 | 
            -
                  {git_revision: {status: status, value: value} }
         | 
| 58 | 
            -
                end
         | 
| 59 | 
            -
             | 
| 60 | 
            -
                def check_migration_version
         | 
| 61 | 
            -
                  value = ""
         | 
| 62 | 
            -
                  status = "ok"
         | 
| 63 | 
            -
                  begin
         | 
| 64 | 
            -
                    if defined?(ActiveRecord)
         | 
| 65 | 
            -
                      connection = ActiveRecord::Base.connection
         | 
| 66 | 
            -
                      result_set = connection.execute("select max(version) as version from schema_migrations")
         | 
| 67 | 
            -
                      version = result_set.first
         | 
| 68 | 
            -
                      value = version["version"]
         | 
| 69 | 
            -
                    else
         | 
| 70 | 
            -
                      status = "error"
         | 
| 71 | 
            -
                      value = "ActiveRecord not found"
         | 
| 72 | 
            -
                    end
         | 
| 73 | 
            -
                  rescue => e
         | 
| 74 | 
            -
                    status = "error"
         | 
| 75 | 
            -
                    value = e.message
         | 
| 76 | 
            -
                  end
         | 
| 77 | 
            -
                  {migration_version: {status: status, value: value} }
         | 
| 46 | 
            +
                def build_check_classes(check_names)
         | 
| 47 | 
            +
                  check_names = Array(check_names) # handle nil, or not a list
         | 
| 48 | 
            +
                  check_names = check_names | DEFAULT_CHECKS # add the :http check if it's not there
         | 
| 49 | 
            +
                  check_names.map{|check_name|
         | 
| 50 | 
            +
                    check_class = CheckRegistry.instance[check_name]
         | 
| 51 | 
            +
                    raise "Don't know about check #{check_name}" unless check_class
         | 
| 52 | 
            +
                    check_class
         | 
| 53 | 
            +
                  }
         | 
| 78 54 | 
             
                end
         | 
| 79 55 | 
             
              end
         | 
| 80 56 | 
             
            end
         | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require "rack/ecg/check_registry"
         | 
| 2 | 
            +
            require "rack/ecg/check/error"
         | 
| 3 | 
            +
            require "rack/ecg/check/git_revision"
         | 
| 4 | 
            +
            require "rack/ecg/check/http"
         | 
| 5 | 
            +
            require "rack/ecg/check/migration_version"
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            module Rack
         | 
| 8 | 
            +
              class ECG
         | 
| 9 | 
            +
                module Check
         | 
| 10 | 
            +
                  class Result < Struct.new(:name, :status, :value)
         | 
| 11 | 
            +
                    def to_json
         | 
| 12 | 
            +
                      {name => {:status => status, :value => value}}
         | 
| 13 | 
            +
                    end
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            module Rack
         | 
| 2 | 
            +
              class ECG
         | 
| 3 | 
            +
                module Check
         | 
| 4 | 
            +
                  # if rack-ecg is serving a request - http is obviously working so far...
         | 
| 5 | 
            +
                  # this is basically a "hello-world"
         | 
| 6 | 
            +
                  class Error
         | 
| 7 | 
            +
                    def result
         | 
| 8 | 
            +
                      Result.new(:error, "error", "PC LOAD LETTER")
         | 
| 9 | 
            +
                    end
         | 
| 10 | 
            +
                  end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  CheckRegistry.instance.register(:error, Error)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            module Rack
         | 
| 2 | 
            +
              class ECG
         | 
| 3 | 
            +
                module Check
         | 
| 4 | 
            +
                  class GitRevision
         | 
| 5 | 
            +
                    def result
         | 
| 6 | 
            +
                      _stdin, stdout, stderr, wait_thread = Open3.popen3("git rev-parse HEAD")
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                      success = wait_thread.value.success?
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                      status = success ? "ok" : "error"
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                      value = success ? stdout.read : stderr.read
         | 
| 13 | 
            +
                      value = value.strip
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                      Result.new(:git_revision, status, value)
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  CheckRegistry.instance.register(:git_revision, GitRevision)
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            module Rack
         | 
| 2 | 
            +
              class ECG
         | 
| 3 | 
            +
                module Check
         | 
| 4 | 
            +
                  # if rack-ecg is serving a request - http is obviously working so far...
         | 
| 5 | 
            +
                  # this is basically a "hello-world"
         | 
| 6 | 
            +
                  class Http
         | 
| 7 | 
            +
                    def result
         | 
| 8 | 
            +
                      Result.new(:http, "ok", "online")
         | 
| 9 | 
            +
                    end
         | 
| 10 | 
            +
                  end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  CheckRegistry.instance.register(:http, Http)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            module Rack
         | 
| 2 | 
            +
              class ECG
         | 
| 3 | 
            +
                module Check
         | 
| 4 | 
            +
                  class MigrationVersion
         | 
| 5 | 
            +
                    def result
         | 
| 6 | 
            +
                      value = ""
         | 
| 7 | 
            +
                      status = "ok"
         | 
| 8 | 
            +
                      begin
         | 
| 9 | 
            +
                        if defined?(ActiveRecord)
         | 
| 10 | 
            +
                          connection = ActiveRecord::Base.connection
         | 
| 11 | 
            +
                          result_set = connection.execute("select max(version) as version from schema_migrations")
         | 
| 12 | 
            +
                          version = result_set.first
         | 
| 13 | 
            +
                          value = version["version"]
         | 
| 14 | 
            +
                        else
         | 
| 15 | 
            +
                          status = "error"
         | 
| 16 | 
            +
                          value = "ActiveRecord not found"
         | 
| 17 | 
            +
                        end
         | 
| 18 | 
            +
                      rescue => e
         | 
| 19 | 
            +
                        status = "error"
         | 
| 20 | 
            +
                        value = e.message
         | 
| 21 | 
            +
                      end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                      Result.new(:migration_version, status, value)
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  CheckRegistry.instance.register(:migration_version, MigrationVersion)
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            require "singleton"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Rack
         | 
| 4 | 
            +
              class ECG
         | 
| 5 | 
            +
                class CheckRegistry
         | 
| 6 | 
            +
                  include Singleton
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def initialize()
         | 
| 9 | 
            +
                    @registry = {}
         | 
| 10 | 
            +
                  end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  def register(name, check_class)
         | 
| 13 | 
            +
                    @registry[name] = check_class
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  def [](name)
         | 
| 17 | 
            +
                    @registry[name]
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
    
        data/lib/rack/ecg/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rack-ecg
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Envato
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2015- | 
| 12 | 
            +
            date: 2015-06-17 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rack
         | 
| @@ -115,8 +115,15 @@ files: | |
| 115 115 | 
             
            - examples/basic.ru
         | 
| 116 116 | 
             
            - examples/checks.ru
         | 
| 117 117 | 
             
            - examples/mounted_path.ru
         | 
| 118 | 
            +
            - examples/stand_alone.ru
         | 
| 118 119 | 
             
            - lib/rack-ecg.rb
         | 
| 119 120 | 
             
            - lib/rack/ecg.rb
         | 
| 121 | 
            +
            - lib/rack/ecg/check.rb
         | 
| 122 | 
            +
            - lib/rack/ecg/check/error.rb
         | 
| 123 | 
            +
            - lib/rack/ecg/check/git_revision.rb
         | 
| 124 | 
            +
            - lib/rack/ecg/check/http.rb
         | 
| 125 | 
            +
            - lib/rack/ecg/check/migration_version.rb
         | 
| 126 | 
            +
            - lib/rack/ecg/check_registry.rb
         | 
| 120 127 | 
             
            - lib/rack/ecg/version.rb
         | 
| 121 128 | 
             
            - rack-ecg.gemspec
         | 
| 122 129 | 
             
            - spec/rack_middleware_spec.rb
         |