Sha256: 4670d83e5f00d40a3e2e0f09f6cb78b253cdedf0356e21e3705a27a5ec2d0a48

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

module ServiceManager
  class CreateError < StandardError; end
  class ServiceNotFound < StandardError; end
  class ServiceError < StandardError; end

  class ServiceStruct < Struct.new(:service_name, :display_name, :binary_path_name)
  end

  def self.create(service_name, display_name, binary_path_name)
    cmd = ['create']
    cmd << service_name
    cmd << "DisplayName=" << display_name
    cmd << "binPath=" << binary_path_name.inspect
    status, out = sc(*cmd)
    raise CreateError.new(out) unless status == 0

    return true
  end

  def self.exist?(service_name)
    status, out = sc('query', service_name)
    status == 0
  end

  def self.open(service_name)
    status, out = sc('qc', service_name, 4096)
    raise ServiceNotFound.new(out) unless status == 0

    out =~ /BINARY\_PATH\_NAME.*\: (.*)$/
    binary_path_name = $1.strip

    out =~ /DISPLAY\_NAME.*\: (.*)$/
    display_name = $1.strip

    svc = ServiceStruct.new(service_name, display_name, binary_path_name)

    yield svc if block_given?
    svc
  end

  def self.getdisplayname(service_name)
    status, out = sc('GetDisplayName', service_name)
    raise ServiceNotFound.new(out) unless status == 0

    out =~ /\=(.*)$/
    $1.strip
  end

  def self.stop(service_name)
    status, out = net('stop', service_name)
    raise ServiceError.new(out) unless status == 0

    return true
  end

  def self.delete(service_name)
    status, out = sc('delete', service_name)
    raise ServiceError.new(out) unless status == 0

    return true
  end

  private

  def self.sc(*args)
    output = `sc #{args.join(' ')} 2>&1`
    return [$?.exitstatus, output]
  end

  def self.net(*args)
    output = `net #{args.join(' ')} 2>&1`
    return [$?.exitstatus, output]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongrel_service-0.4.beta2 lib/mongrel_service/service_manager.rb
mongrel_service-0.4.beta1 lib/mongrel_service/service_manager.rb