ezmobius / nanite

self assembling fabric of ruby daemons

This URL has Read+Write access

nanite / bin / nanite-mapper
100755 51 lines (42 sloc) 1.236 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
 
require File.dirname(__FILE__) + '/../lib/nanite'
require 'optparse'
 
include Nanite::CommonConfig
 
options = {}
 
opts = OptionParser.new do |opts|
  opts.banner = "Usage: nanite-mapper [-flags] [argument]"
  opts.define_head "Nanite Mapper: clustered head unit for self assembling cluster of ruby processes."
  opts.separator '*'*80
 
  setup_mapper_options(opts, options)
end
 
opts.parse!
 
if ARGV[0] == 'stop' || ARGV[0] == 'status'
  mapper = Nanite::Mapper.new(options)
  pid_file = Nanite::PidFile.new(mapper.identity, mapper.options)
  unless pid = pid_file.read_pid
    puts "#{pid_file} not found"
    exit
  end
  if ARGV[0] == 'stop'
    puts "Stopping nanite mapper #{mapper.identity} (pid #{pid})"
    begin
      Process.kill('TERM', pid)
    rescue Errno::ESRCH
      puts "Process does not exist (pid #{pid})"
      exit
    end
    puts 'Done.'
  else
    if Process.getpgid(pid) != -1
      psdata = `ps up #{pid}`.split("\n").last.split
      memory = (psdata[5].to_i / 1024)
      puts "The mapper is alive, using #{memory}MB of memory"
    else
      puts "The mapper is not running but has a stale pid file at #{pid_file}"
    end
  end
  exit
end
 
EM.run do
  Nanite.start_mapper(options)
end