Module: Dorsal::Privates

Included in:
Controller, ImplementationServer
Defined in:
lib/dorsal/privates.rb

Overview

module mixin for privates methods for both Controller and ImplementationServer

Instance Method Summary collapse

Instance Method Details

#daemonize(_options) { ... } ⇒ Fixnum

method for daemonize blocks

Examples:

usage inline

require 'dorsal/privates'
class Test
  include Dorsal::Privates
  private :daemonize
  def initialize
    @loop = Proc::new do 
      loop do
        sleep 1
      end
    end
  end

  def run
    daemonize({:description => "A loop daemon", :pid_file => '/tmp/pid.file'}, &@loop)
  end
 end

usage block

require 'dorsal/privates'
class Test
  include Dorsal::Privates
  private :daemonize
  def initialize
  end

  def run
    daemonize :description => "A loop daemon", :pid_file => '/tmp/pid.file' do
      loop do
        sleep 1
      end
    end
  end
 end

Parameters:

  • _options (Hash)

    the list of options, keys are symbols

Options Hash (_options):

  • :description (String)

    the description of the process, use for $0

  • :pid_file (String)

    the pid filenam

Yields:

  • a process definion or block given

Returns:

  • (Fixnum)

    pid the pid of the forked processus



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dorsal/privates.rb', line 58

def daemonize(_options)
  options = Methodic::get_options(_options)
  options.specify_presences_of :description, :pid_file
  options.validate
  return yield if options[:debug]
  pid = fork do 
    trap("SIGINT"){ eit! 0 }
    trap("SIGTERM"){ eit! 0 }
    trap("SIGHUP"){ eit! 0 }
    Process.daemon
    $0 = options[:description]
    yield
  end
  File.open(options[:pid_file],"w"){|f| f.puts pid } if options[:pid_file]
  return pid
end

#start(_options = {}) { ... } ⇒ Fixnum

daemonize wrapper to prevent processus cloning

Examples:

usage inline

require 'dorsal/privates'
class Test
  include Dorsal::Privates
  private :start
  private :daemonize
  def initialize
    @loop = Proc::new do 
      loop do
        sleep 1
      end
    end
  end

  def run
    start({:description => "A loop daemon", :pid_file => '/tmp/pid.file'}, &@loop)
  end
 end

usage block

require 'dorsal/privates'
class Test
  include Dorsal::Privates
  private :daemonize
  private :start
  def initialize
  end

  def run
    start :description => "A loop daemon", :pid_file => '/tmp/pid.file' do
      loop do
        sleep 1
      end
    end
  end
 end 

Parameters:

  • _options (Hash) (defaults to: {})

    the list of options, keys are symbols

Options Hash (_options):

  • :description (String)

    the description of the process, use for $0

  • :pid_file (String)

    the pid filenam

Yields:

  • a process definion or block given

Returns:

  • (Fixnum)

    pid the pid of the forked processus

Raises:



118
119
120
121
122
123
124
125
126
127
# File 'lib/dorsal/privates.rb', line 118

def start(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :description, :pid_file
  options.validate
  raise Dorsal::RingServerError::new('already running, pid file exist') if File::exist?(options[:pid_file])
  raise Dorsal::RingServerError::new('already running, process found') unless `ps aux|grep -v grep |grep '#{options[:description]}'`.empty?
  return daemonize(options) do
    yield
  end
end

#status(_options = {}) ⇒ TrueClass, FalseClass

give the status of a processus

Examples:

usage inline

#in the same class
def service_status
  status :name => 'service', :description => 'A loop daemon', :pid_file => '/tmp/pid.file'
end

Parameters:

  • _options (Hash) (defaults to: {})

    the list of options, keys are symbols

Options Hash (_options):

  • :description (String)

    the description of the process, use for $0

  • :pid_file (String)

    the pid filename

  • :name (String)

    the name of the processus (OPTIONAL)

Returns:

  • (TrueClass, FalseClass)

    true if service running, false otherwise



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dorsal/privates.rb', line 170

def status(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :description, :pid_file
  options.validate
  pid = `COLUMNS=160 ps aux|grep -v grep |grep '#{options[:description]}'|awk '{ print $2}'`
  if pid.empty? then
    return false
  else
    File.open(options[:pid_file],"w"){|f| f.puts pid } unless File::exist?(options[:pid_file])
    return true
  end
end

#stop(_options = {}) ⇒ TrueClass, FalseClass

stop a running processus

Examples:

usage inline

#in the same class
def stop_service
  stop :name => 'service', :description => 'A loop daemon', :pid_file => '/tmp/pid.file'
end

Parameters:

  • _options (Hash) (defaults to: {})

    the list of options, keys are symbols

Options Hash (_options):

  • :description (String)

    the description of the process, use for $0

  • :pid_file (String)

    the pid filename

  • :name (String)

    the name of the processus (OPTIONAL)

Returns:

  • (TrueClass, FalseClass)

    true if a service really closed, false otherwise

Raises:

  • (Dorsal::ServerError)

    if can’t close an existant service



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dorsal/privates.rb', line 141

def stop(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :description, :pid_file
  options.validate
  File::unlink(options[:pid_file]) if File::exist?(options[:pid_file])
  pid = `COLUMNS=160 ps aux|grep -v grep |grep '#{options[:description]}'|awk '{ print $2}'`
  if pid.empty? then
    return false
  else
    if options[:name] == 'ringserver' then
      raise Dorsal::ServerError::new('Stopping failed') unless system("kill -TERM #{pid} > /dev/null")
    else
      return false unless system("kill -TERM #{pid} > /dev/null")
    end
    return true
  end
end