Class: Asmodai::Daemon

Inherits:
Object
  • Object
show all
Extended by:
ProcessManagement, RakeTask, Logging
Includes:
Logging
Defined in:
lib/asmodai/daemon.rb

Defined Under Namespace

Modules: ProcessManagement, RakeTask

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProcessManagement

is_running?, pid, pid_file_path, terminate

Methods included from RakeTask

install_rake_task

Constructor Details

#initializeDaemon

Returns a new instance of Daemon.



24
25
# File 'lib/asmodai/daemon.rb', line 24

def initialize
end

Class Method Details

.autoload_pathsObject



11
12
13
# File 'lib/asmodai/daemon.rb', line 11

def autoload_paths
  @autoload_paths ||= []
end

.daemon_nameObject



19
20
21
# File 'lib/asmodai/daemon.rb', line 19

def daemon_name
  @daemon_name || name.split("::").last.to_s.underscore
end

.set_daemon_name(name) ⇒ Object



15
16
17
# File 'lib/asmodai/daemon.rb', line 15

def set_daemon_name(name)
  @daemon_name=name.to_s.underscore
end

Instance Method Details

#daemon_nameObject



27
28
29
# File 'lib/asmodai/daemon.rb', line 27

def daemon_name
  self.class.daemon_name
end

#foregroundObject



36
37
38
39
# File 'lib/asmodai/daemon.rb', line 36

def foreground
  prepare_run
  perform_run
end

#on_signal(sig) ⇒ Object



31
32
33
# File 'lib/asmodai/daemon.rb', line 31

def on_signal(sig)
  # dummy implementation
end

#startObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/asmodai/daemon.rb', line 41

def start 
  prepare_run

  raise 'Fork failed' if (pid=fork) == -1

  if !pid
    Process.setsid

    raise 'Fork failed' if (pid=fork) == -1
    if !pid
      pid = Process.pid

      self.class.pid_file_path.open("w") do |f|
        f.puts pid
      end

      $stdin.reopen('/dev/null')
      $stdout.reopen(log_file)
      $stderr.reopen(log_file)
      logger.info "Starting up #{daemon_name} at #{Time.now}, pid: #{Process.pid}"

      perform_run
    end
  end
end