Class: Pigeon::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/pigeon/launcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(with_engine = Pigeon::Engine, logging: true) {|_self| ... } ⇒ Launcher

Instance Methods =====================================================

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
# File 'lib/pigeon/launcher.rb', line 14

def initialize(with_engine = Pigeon::Engine, logging: true)
  @engine = with_engine
  @logging = !!logging
  
  yield(self) if (block_given?)
end

Class Method Details

.launch(engine = Pigeon::Engine, *arguments, logging: true) ⇒ Object

Class Methods ========================================================



6
7
8
9
10
# File 'lib/pigeon/launcher.rb', line 6

def self.launch(engine = Pigeon::Engine, *arguments, logging: true)
  arguments = %w[ start ] if (arguments.empty?)
  
  new(engine, logging: logging).handle_args(*arguments)
end

Instance Method Details

#handle_args(*args) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pigeon/launcher.rb', line 21

def handle_args(*args)
  op = OptionParser.new do |parser|
    parser.on('-v', '--version') do
      pigeon_version = 'Pigeon %s' % Pigeon.version

      version =
        if (@engine.respond_to?(:version))
          '%s (%s)' % [ @engine.version, pigeon_version ]
        else
          pigeon_version
        end

      puts version
      exit(0)
    end
  end
  
  command = op.parse(*args.flatten).first

  begin
    case (command)
    when 'start'
      @engine.start do |pid, launched|
        yield(pid, launched) if (block_given?)
        self.start(pid, launched)
      end
    when 'stop'
      @engine.stop do |pid|
        yield(pid) if (block_given?)
        self.stop(pid)
      end
    when 'restart'
      @engine.restart do |pid, old_pid|
        yield(pid, old_pid) if (block_given?)
        self.restart(pid, old_pid)
      end
    when 'status'
      @engine.status do |pid|
        yield(pid) if (block_given?)
        self.status(pid)
      end
    when 'run'
      @engine.engine_logger = Pigeon::Logger.new(STDOUT)

      @engine.run do |pid|
        yield(pid) if (block_given?)
        self.run(pid)
      end
    else
      usage
    end

  rescue Interrupt
    exit(0)
  end
end

#log(message) ⇒ Object



123
124
125
126
127
# File 'lib/pigeon/launcher.rb', line 123

def log(message)
  if (@logging)
    puts(message)
  end
end

#restart(pid, old_pid) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/pigeon/launcher.rb', line 107

def restart(pid, old_pid)
  if (old_pid)
    log "#{@engine.name} terminated. [%d]" % old_pid
  end

  log "#{@engine.name} now running. [%d]" % pid
end

#run(pid) ⇒ Object



78
79
80
81
# File 'lib/pigeon/launcher.rb', line 78

def run(pid)
  log "#{@engine.name} now running. [%d]" % pid
  log "Use ^C to terminate."
end

#shutdown(pid) ⇒ Object



115
116
117
# File 'lib/pigeon/launcher.rb', line 115

def shutdown(pid)
  log "Shutting down."
end

#start(pid, launched) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/pigeon/launcher.rb', line 83

def start(pid, launched)
  if (launched)
    log "#{@engine.name} now running. [%d]" % pid
  else
    log "#{@engine.name} already running. [%d]" % pid
  end
end

#status(pid) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/pigeon/launcher.rb', line 99

def status(pid)
  if (pid)
    log "#{@engine.name} running. [%d]" % pid
  else
    log "#{@engine.name} is not running."
  end
end

#stop(pid) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/pigeon/launcher.rb', line 91

def stop(pid)
  if (pid)
    log "#{@engine.name} shut down. [%d]" % pid
  else
    log "#{@engine.name} was not running."
  end
end

#usageObject



119
120
121
# File 'lib/pigeon/launcher.rb', line 119

def usage
  log "Usage: #{File.basename($0)} [start|stop|restart|status|run]"
end