Module: Kryten::RedDwarf

Defined in:
lib/kryten/summoner.rb

Class Method Summary collapse

Class Method Details

.aliveObject



92
93
94
# File 'lib/kryten/summoner.rb', line 92

def self.alive
  self.daemons.select(&:pid)
end

.can_shutdownObject



148
149
150
# File 'lib/kryten/summoner.rb', line 148

def self.can_shutdown
  @sd
end

.can_shutdown=(bool) ⇒ Object



144
145
146
# File 'lib/kryten/summoner.rb', line 144

def self.can_shutdown= bool
  @sd = bool
end

.daemon(name) ⇒ Object



88
89
90
# File 'lib/kryten/summoner.rb', line 88

def self.daemon name
  self.daemons.detect { |d| d.name == name }
end

.daemonsObject



84
85
86
# File 'lib/kryten/summoner.rb', line 84

def self.daemons
  (@@daemons ||= [])
end

.deadObject



96
97
98
# File 'lib/kryten/summoner.rb', line 96

def self.dead
  self.daemons - self.alive
end

.halt(job) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/kryten/summoner.rb', line 135

def self.halt job
  daemon = self.daemon job
  if daemon.pid
    daemon.stop
  else
    puts "daemon #{daemon} was not running"
  end
end

.monitorObject



100
101
102
103
# File 'lib/kryten/summoner.rb', line 100

def self.monitor
  self.start unless self.running?
  self.status
end

.run(job) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/kryten/summoner.rb', line 124

def self.run job
  self.can_shutdown = false
  daemon = self.daemon job
  if daemon.pid
    puts "daemon #{daemon} is still running"
  else
    daemon.start_daemon
  end
  self.can_shutdown = true
end

.running?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/kryten/summoner.rb', line 173

def self.running?
  !self.daemons.empty?
end

.startObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kryten/summoner.rb', line 65

def self.start

  unless self.daemons.empty?
    puts "BackgroundFactory is already running"
    return
  end

  raise "No Block Given" unless block_given?

  puts "starting all daemons"
  @@daemons = []
  @@daemons << yield
  @@daemons = @@daemons.flatten
  @@daemons.each { |d| d.init_daemon }

  self.can_shutdown = true

end

.statusObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kryten/summoner.rb', line 105

def self.status
  status = proc do |job|
    {
      name: job.daemon.pid.progname,
      pid: job.daemon.pid,
      pid_dir: job.daemon.pidfile_dir,
      logfile: job.daemon.logfile,
      output: job.daemon.output_logfile,
      status: job.status,
      object: job
    }
  end
  alive = self.alive.map(&status)
  dead = self.dead.map(&status)
  #puts "#{alive.count} running daemons"
  #puts "#{dead.count} dead daemons"
  { alive: alive, dead: dead }
end

.stopObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/kryten/summoner.rb', line 152

def self.stop

  return unless self.can_shutdown

  if self.running?
    puts "stopping all daemons"
    @@daemons.count.times do
      daemon = @@daemons.pop
      if daemon.pid
        pid = Process.fork { daemon.stop }
        Process.detach(pid)
      else
        puts "daemon #{daemon} was not running"
      end
    end
  else
    puts "BackgroundFactory has not been started yet"
  end

end