Module: ShootingStar

Defined in:
lib/shooting_star/shooter.rb,
lib/shooting_star.rb,
lib/shooting_star/config.rb,
lib/shooting_star/server.rb,
lib/shooting_star/channel.rb

Overview

DRbObject

Defined Under Namespace

Modules: Server Classes: Channel, Config, Shooter

Constant Summary collapse

VERSION =
'1.0.5'
CONFIG =
Config.new(
:config => 'config/shooting_star.yml',
:pid_file => 'log/shooting_star.pid',
:log_file => 'log/shooting_star.log',
:daemon => false,
:slient => false,
:session_timeout => 10,
:sweep_timeout => 500_000)

Class Method Summary collapse

Class Method Details

.configure(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/shooting_star.rb', line 20

def self.configure(options = {})
  if @log_file
    @log_file.close
    @log_file = nil
  end
  config_file = options[:config] || CONFIG.config
  CONFIG.merge!(YAML.load_file(config_file)) if File.exist?(config_file)
  CONFIG.merge!(options)
end

.initObject

install config file and plugin



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shooting_star.rb', line 35

def self.init
  base_dir = CONFIG.directory || `pwd`.chop
  config_dir = File.join(base_dir, 'config')
  `mkdir -p #{config_dir}` unless File.exist? config_dir
  config_file = File.join(config_dir, 'shooting_star.yml')
  unless File.exist? config_file
    open(config_file, 'w') do |file|
      open(__FILE__) do |data|
        data.gets("__END__\n")
        file.write data.read
      end
    end
  end
  log_dir = File.join(base_dir, 'log')
  `mkdir -p #{log_dir}` unless File.exist? log_dir
  plugin_dir = File.join(base_dir, 'vendor/plugins')
  `mkdir -p #{plugin_dir}` unless File.exist? plugin_dir
  meteor_strike_dir = File.join(plugin_dir, 'meteor_strike')
  src_dir = File.join(File.dirname(__FILE__),
    '../vendor/plugins/meteor_strike')
  `mkdir -p #{meteor_strike_dir}`
  `cp -Rf #{src_dir}/* #{meteor_strike_dir}`
end

.reportObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/shooting_star.rb', line 113

def self.report
  puts "#{'-' * 79}\nconnections channel name\n#{'-' * 79}"
  total_connections = 0
  shooter.channels.each do |channel|
    count = shooter.count(channel)
    puts "%11d %s" % [count, channel]
    puts shooter.listeners(channel).join(',') if CONFIG.with_uid
    puts shooter.signatures(channel).join(',') if CONFIG.with_sig
    total_connections +=  count
  end
  puts "#{'-' * 79}\n%11d %s\n#{'-' * 79}" % [total_connections, 'TOTAL']
end

.restartObject



107
108
109
110
111
# File 'lib/shooting_star.rb', line 107

def self.restart
  command = stop
  Thread.pass while File.exist?(CONFIG.pid_file)
  system(command)
end

.shooterObject



30
31
32
# File 'lib/shooting_star.rb', line 30

def self.shooter
  @@shooter ||= DRbObject.new_with_uri(CONFIG.shooter.uri)
end

.start(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shooting_star.rb', line 59

def self.start(&block)
  if File.exist?(CONFIG.pid_file)
    log 'shooting_star is already running.'
    return
  end
  if CONFIG.daemon
    Signal.trap(:ALRM){exit} and sleep if fork
    Process.setsid
  end
  require 'shooting_star/shooter'
  @@druby = DRb.start_service(CONFIG.shooter.uri, Shooter.new)
  require 'shooting_star/server'
  Asteroid::run(CONFIG.server.host, CONFIG.server.port, Server) do
    File.open(CONFIG.pid_file, "w") do |file|
      file.puts Process.pid
      file.puts $command_line
    end
    Signal.trap(:INT) do
      Asteroid::stop
      @@druby.stop_service
      log "shooting_star service stopped."
      File.rm_f(CONFIG.pid_file)
    end
    Signal.trap(:EXIT) do
      File.rm_f(CONFIG.pid_file)
      @log_file.close if @log_file
    end
    log "shooting_star service started."
    Process.kill(:ALRM, Process.ppid) if CONFIG.daemon
    block.call if block
  end
end

.stopObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/shooting_star.rb', line 92

def self.stop
  command = ''
  File.open(CONFIG.pid_file) do |file|
    Process.kill(:INT, pid = file.gets.to_i)
    command = file.gets
  end
  Thread.pass while File.exist?(CONFIG.pid_file)
rescue Errno::ENOENT
  log "shooting_star service is not running."
rescue Errno::ESRCH
  File.unlink(CONFIG.pid_file)
ensure
  return command
end

.timestampObject



126
127
128
129
# File 'lib/shooting_star.rb', line 126

def self.timestamp
  now = Time.now
  "%d%06d" % [now.tv_sec, now.tv_usec]
end