Sha256: 5b863eeb5133464c533367e9fb5ab001727d50fe235bfc345c89ecb5a85ed070

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require "daemons"

module Matchd
  # Controls the demonizing of a {Matchd::Server}
  class Control
    def initialize(options = {})
      @name = options.delete(:name, "matchd")
    end

    def start(options = {})
      run! "start", ontop: options.fetch(:ontop, false)
    end

    def stop
      run! "stop"
    end

    def status
      run! "status"
    end

    def restart(options = {})
      stop
      start(options)
    end

    # @private
    def run!(argv, options = {})
      run_options = { ARGV: Array(argv), **options, **daemon_opts }
      Daemons.run_proc(@name, run_options) do
        require "matchd/server"
        Matchd::Server.new(*server_opts).run
      end
    end

    # @private
    def server_opts
      [
        Matchd::Registry.load_file(Matchd::Config.registry_file),
        Matchd::Config.listen,
        { resolver: Matchd::Config.resolver }
      ]
    end

    # @private
    def daemon_opts
      daemon_dir = Matchd::Config.dot_dir
      {
        dir_mode: :normal,
        dir: daemon_dir,
        log_output: true,
        log_dir: daemon_dir
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchd-0.1.0 lib/matchd/control.rb