Class: MailSandbox::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_sandbox/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



11
12
13
# File 'lib/mail_sandbox/runner.rb', line 11

def initialize
  self.config = MailSandbox::Config.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/mail_sandbox/runner.rb', line 9

def config
  @config
end

Instance Method Details

#configureObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mail_sandbox/runner.rb', line 37

def configure
  config.load_from_yml_file(env) if config.config_file

  if config.http_observe?
    MailSandbox.subscribe MailSandbox::Observer::Http.new(config.http_observe_url)
  end

  MailSandbox.logger.level = case config.log_level.to_sym
                               when :info then Logger::INFO
                               when :error then Logger::ERROR
                               when :warn then Logger::WARN
                               when :debug then Logger::DEBUG
                             end
  STDOUT.sync = true
  MailSandbox::Signals.trap(self)
  MailSandbox::Server.parms = config.server_params
end

#envObject



68
69
70
# File 'lib/mail_sandbox/runner.rb', line 68

def env
  config.environment || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || :development
end

#option_parseObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mail_sandbox/runner.rb', line 15

def option_parse
  OptionParser.new do |opts|

    opts.on("-c", "--config-file FILE", "Config file") do |f|
      config.config_file = f
    end

    opts.on("-E", "--environment", "Environment") do |f|
      config.environment = f
    end

    opts.on("-D", "--daemonize", "Run daemonized") do |f|
      config.daemonize = f
    end

    opts.on("-P", "--pid FILE", "File to store PID") do |f|
      config.pidfile = ::File.expand_path(f)
    end

  end.parse!
end

#startObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mail_sandbox/runner.rb', line 55

def start
  configure

  Process.daemon if config.daemonize
  write_pidfile if config.pidfile

  MailSandbox.logger.info "Start MailSandbox::Server on #{config.listen}:#{config.port}"

  EventMachine::run {
    EventMachine::start_server config.listen, config.port, MailSandbox::Server
  }
end

#terminateObject



86
87
88
89
90
# File 'lib/mail_sandbox/runner.rb', line 86

def terminate
  MailSandbox.logger.info "Got quit/terminate signal. Bye."
  @simple_pid.cleanup if @simple_pid
  exit
end

#write_pidfileObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mail_sandbox/runner.rb', line 72

def write_pidfile
  @simple_pid = SimplePid.new(config.pidfile)

  if @simple_pid.exists?
    unless @simple_pid.running?
      @simple_pid.cleanup
      @simple_pid.write!
    end
  else
    @simple_pid.write!
  end

end