Top Level Namespace

Defined Under Namespace

Modules: MockWS Classes: ApplicationController

Instance Method Summary collapse

Instance Method Details

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

Returns pid the pid of the forked processus.

Examples:

usage inline

class Test
  include Splash::Helpers
  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

class Test
  include Splash::Helpers
  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 filename

  • :daemon_user (String)

    the user to change privileges

  • :daemon_group (String)

    the group to change privileges

  • :stderr_trace (String)

    the path of the file where to redirect STDERR

  • :stdout_trace (String)

    the path of the file where to redirect STDOUT

  • :sigint_handler (Proc)

    handler Proc for SIGINT signal

  • :sigterm_handler (Proc)

    handler Proc for SIGTERM signal

  • :sighup_handler (Proc)

    handler Proc for SIGHuP signal

  • :foreground (Bool)

    option to run foreground

Yields:

  • a process definion or block given

Returns:

  • (Fixnum)

    pid the pid of the forked processus



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mockws.rb', line 155

def daemonize(options)
  {
   :sighup_handler => 'SIGHUP',
   :sigint_handler => 'SIGINT',
   :sigterm_handler => 'SIGTERM',
  }.each do |key,value|
    trap(value){
      if options[:sighup_handler].include? key then
        options[:sighup_handler].call
      else
        exit! 0
      end
    }
  end
  if options[:foreground]
    Process.setproctitle options[:description] if options[:description]
    return yield
  end
  fork do
    File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file]
    if options[:daemon_user] and options[:daemon_group] then
      uid = Etc.getpwnam(options[:daemon_user]).uid
      gid = Etc.getgrnam(options[:daemon_group]).gid
      Process::UID.change_privilege(uid)
      Process::GID.change_privilege(gid)
    end
    $stdout.reopen(options[:stdout_trace], "w") if options[:stdout_trace]
    $stderr.reopen(options[:stderr_trace], "w") if options[:stderr_trace]
    Process.setproctitle options[:description] if options[:description]
    yield
  end
  return 0
end

#search_file_in_gem(gem, file) ⇒ String, False

facility to find a file in gem path

Parameters:

  • gem (String)

    a Gem name

  • file (String)

    a file relative path in the gem

Returns:

  • (String)

    the path of the file, if found.

  • (False)

    if not found



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mockws.rb', line 17

def search_file_in_gem(gem, file)
  if Gem::Specification.respond_to?(:find_by_name)
    begin
      spec = Gem::Specification.find_by_name(gem)
    rescue LoadError
      spec = nil
    end
  else
    spec = Gem.searcher.find(gem)
  end
  if spec
    res = if Gem::Specification.respond_to?(:find_by_name)
            spec.lib_dirs_glob.split('/')
          else
            Gem.searcher.lib_dirs_for(spec).split('/')
          end
    res.pop
    services_path = res.join('/').concat("/#{file}")
    return services_path if File.exist?(services_path)

  end
  false
end