Class: FakeSMTPd::Runner
- Inherits:
-
Object
- Object
- FakeSMTPd::Runner
- Defined in:
- lib/fakesmtpd/runner.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#http_port ⇒ Object
readonly
Returns the value of attribute http_port.
-
#logfile ⇒ Object
readonly
Returns the value of attribute logfile.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pidfile ⇒ Object
readonly
Returns the value of attribute pidfile.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server_pid ⇒ Object
readonly
Returns the value of attribute server_pid.
-
#startup_sleep ⇒ Object
readonly
Returns the value of attribute startup_sleep.
Instance Method Summary collapse
- #command ⇒ Object
- #description ⇒ Object
-
#initialize(options = {}) ⇒ Runner
constructor
A new instance of Runner.
- #log ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Runner
Returns a new instance of Runner.
10 11 12 13 14 15 16 17 |
# File 'lib/fakesmtpd/runner.rb', line 10 def initialize( = {}) @dir = .fetch(:dir) @port = Integer(.fetch(:port)) @http_port = Integer([:http_port] || port + 1) @pidfile = [:pidfile] || 'fakesmtpd.pid' @startup_sleep = [:startup_sleep] || 0.5 @logfile = [:logfile] || $stderr end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
7 8 9 |
# File 'lib/fakesmtpd/runner.rb', line 7 def dir @dir end |
#http_port ⇒ Object (readonly)
Returns the value of attribute http_port.
7 8 9 |
# File 'lib/fakesmtpd/runner.rb', line 7 def http_port @http_port end |
#logfile ⇒ Object (readonly)
Returns the value of attribute logfile.
8 9 10 |
# File 'lib/fakesmtpd/runner.rb', line 8 def logfile @logfile end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/fakesmtpd/runner.rb', line 8 def @options end |
#pidfile ⇒ Object (readonly)
Returns the value of attribute pidfile.
7 8 9 |
# File 'lib/fakesmtpd/runner.rb', line 7 def pidfile @pidfile end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/fakesmtpd/runner.rb', line 7 def port @port end |
#server_pid ⇒ Object (readonly)
Returns the value of attribute server_pid.
8 9 10 |
# File 'lib/fakesmtpd/runner.rb', line 8 def server_pid @server_pid end |
#startup_sleep ⇒ Object (readonly)
Returns the value of attribute startup_sleep.
8 9 10 |
# File 'lib/fakesmtpd/runner.rb', line 8 def startup_sleep @startup_sleep end |
Instance Method Details
#command ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fakesmtpd/runner.rb', line 23 def command ( [ RbConfig.ruby, File.('../server.rb', __FILE__), port.to_s, dir, '--pidfile', pidfile, ] + (logfile == $stderr ? [] : ['--logfile', logfile]) ).join(' ') end |
#description ⇒ Object
19 20 21 |
# File 'lib/fakesmtpd/runner.rb', line 19 def description "fakesmtpd server on port #{port}" end |
#log ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/fakesmtpd/runner.rb', line 59 def log @log ||= Logger.new(@logfile).tap do |l| l.formatter = proc do |severity, datetime, _, msg| "[fakesmtpd-runner] #{severity} #{datetime} - #{msg}\n" end end end |
#start ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fakesmtpd/runner.rb', line 35 def start if dir FileUtils.mkdir_p(dir) end process_command = command log.info "Starting #{description}" log.info " ---> #{process_command}" @server_pid = Process.spawn(process_command) sleep startup_sleep server_pid end |
#stop ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fakesmtpd/runner.rb', line 47 def stop real_pid = Integer(File.read(pidfile).chomp) rescue nil if server_pid && real_pid log.info "Stopping #{description} " << "(shell PID=#{server_pid}, server PID=#{real_pid})" [real_pid, server_pid].each do |pid| Process.kill(:TERM, pid) rescue nil end end end |