Class: FakeSMTPd::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @dir = options.fetch(:dir)
  @port = Integer(options.fetch(:port))
  @http_port = Integer(options[:http_port] || port + 1)
  @pidfile = options[:pidfile] || 'fakesmtpd.pid'
  @startup_sleep = options[:startup_sleep] || 0.5
  @logfile = options[:logfile] || $stderr
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



7
8
9
# File 'lib/fakesmtpd/runner.rb', line 7

def dir
  @dir
end

#http_portObject (readonly)

Returns the value of attribute http_port.



7
8
9
# File 'lib/fakesmtpd/runner.rb', line 7

def http_port
  @http_port
end

#logfileObject (readonly)

Returns the value of attribute logfile.



8
9
10
# File 'lib/fakesmtpd/runner.rb', line 8

def logfile
  @logfile
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/fakesmtpd/runner.rb', line 8

def options
  @options
end

#pidfileObject (readonly)

Returns the value of attribute pidfile.



7
8
9
# File 'lib/fakesmtpd/runner.rb', line 7

def pidfile
  @pidfile
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/fakesmtpd/runner.rb', line 7

def port
  @port
end

#server_pidObject (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_sleepObject (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

#commandObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fakesmtpd/runner.rb', line 23

def command
  (
    [
      RbConfig.ruby,
      File.expand_path('../server.rb', __FILE__),
      port.to_s,
      dir,
      '--pidfile', pidfile,
    ] + (logfile == $stderr ? [] : ['--logfile', logfile])
  ).join(' ')
end

#descriptionObject



19
20
21
# File 'lib/fakesmtpd/runner.rb', line 19

def description
  "fakesmtpd server on port #{port}"
end

#logObject



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

#startObject



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

#stopObject



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