Skip to content

andykingking/http_server_manager

 
 

Repository files navigation

http_server_manager

Aims to simplify managing the lifecycle of HTTP server processes.

Given a server start-up command, http_server_manager can:

  • Start the server, which creates a pid file, redirects stdout and stderr to a log file and blocks until started
  • Provide the status of the server (started or stopped)
  • Stop the server, killing the servers process tree and deleting the generated pid file
  • Restart the server, stopping a potentially running server and then starting it

It is currently distributed as a development and testing tool and is not recommended for production use.

Status

Build Status Gem Version Code Climate Test Coverage

Motivation

For projects whose production environment is completed managed by a PaaS provider, such as Heroku or Engine Yard, using god or monit to manage your http processes in development and test environments can be overkill.

http_server_manager provides a simple means of managing the lifecycle of these processes in these environments.

It is particularly useful for automated management of these processes as part of a continuous integration pipeline.

Usage

Step 1: Install via gem install http_server_manager or gem 'http_server_manager in your Gemfile

Step 2: Require:

    require 'http_server_manager'

Step 3: Configure the location of server pid files and logs:

    HttpServerManager.pid_dir = "some/pid/dir"
    HttpServerManager.log_dir = "some/log/dir"

Step 4: Create a server class:

    require 'http_server_manager'

    class MyServer < HttpServerManager::Server

        def initialize
          super(name: :my_server, host: "localhost", port: 3000)
        end

        def start_command
          "rackup --host #{host} --port #{port} my/rack_application.ru"
        end

    end

Optionally provide a timeout_in_seconds to the server constructor to determine how long http_server_manager should wait until the server starts. Defaults to 20 seconds:

    def initialize
        super(name: :my_server, host: "localhost", port: 3000, timeout_in_seconds: 60)
    end

Optionally provide a ping_uri to the server constructor, this uri will be requested when http_server_manager assesses if the server is available. Any response code from this uri is acceptable. Defaults to "/":

    def initialize
        super(name: :my_server, host: "localhost", port: 3000, ping_uri: "/ping")
    end

Step 5: Control the status of the server:

    server = MyServer.new

    server.start! # blocks until server is running

    server.status # returns :started

    server.restart! # kills process

    server.status # returns :started

    server.stop! # kills process

    server.status # returns :stopped

Alternatively, via Rake:

    require 'http_server_manager/rake/task_generators'

    HttpServerManager::Rake::ServerTasks.new(server)
    # Generates tasks:
    # my_server:start
    # my_server:restart
    # my_server:stop
    # my_server:status

Testing

Ensure that your server is configured correctly:

    require 'http_server_manager/test_support'

    describe MyServer do

      include_context "managed http server integration utilities" # Provided by http_server_manager as a test utility

      let(:server) { MyServer.new }

      describe "#start!" do

        after(:each) { force_stop! }

        it "should start the server via the provided command" do
          server.start!

          wait_until_started!
        end

      end

Or, the same thing a little simpler:

    require 'http_server_manager/test_support'

    describe MyServer do

      let(:server) { MyServer.new }

      it_should_behave_like "a managed http server" # Provided by http_server_manager as a test utility

    end

For those not using RSpec, see http_server_manager/test/server_integration_utilities.rb

OS Support

Mac, Linux, Windows

Requirements

  • Ruby >= 2.3

About

Manages the lifecycle of HTTP server processes

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%