Class: Puma::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/cli.rb

Overview

Handles invoke a Puma::Server in a command line style.

Defined Under Namespace

Classes: Worker

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout = STDOUT, stderr = STDERR) ⇒ CLI

Create a new CLI object using argv as the command line arguments.

stdout and stderr can be set to IO-like objects which this object will report status on.



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
# File 'lib/puma/cli.rb', line 25

def initialize(argv, stdout=STDOUT, stderr=STDERR)
  @debug = false
  @argv = argv
  @stdout = stdout
  @stderr = stderr

  @phase = 0
  @workers = []

  @events = Events.new @stdout, @stderr

  @server = nil
  @status = nil

  @restart = false
  @phased_state = :idle

  ENV['NEWRELIC_DISPATCHER'] ||= "puma"

  setup_options

  generate_restart_data

  @binder = Binder.new(@events)
  @binder.import_from_env
end

Instance Method Details

#all_workers_booted?Boolean

Returns:

  • (Boolean)


676
677
678
# File 'lib/puma/cli.rb', line 676

def all_workers_booted?
  @workers.count { |w| !w.booted? } == 0
end

#check_workersObject



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/puma/cli.rb', line 680

def check_workers
  while @workers.any?
    pid = Process.waitpid(-1, Process::WNOHANG)
    break unless pid

    @workers.delete_if { |w| w.pid == pid }
  end

  spawn_workers

  if @phased_state == :idle && all_workers_booted?
    # If we're running at proper capacity, check to see if
    # we need to phase any workers out (which will restart
    # in the right phase).
    #
    w = @workers.find { |x| x.phase != @phase }

    if w
      @phased_state = :waiting
      log "- Stopping #{w.pid} for phased upgrade..."
      w.term
    end
  end
end

#daemon?Boolean

Returns:

  • (Boolean)


408
409
410
# File 'lib/puma/cli.rb', line 408

def daemon?
  @options[:daemon]
end

#debug(str) ⇒ Object



148
149
150
151
152
# File 'lib/puma/cli.rb', line 148

def debug(str)
  if @options[:debug]
    @events.log "- #{str}"
  end
end

#delete_pidfileObject



305
306
307
308
309
# File 'lib/puma/cli.rb', line 305

def delete_pidfile
  if path = @options[:pidfile]
    File.unlink path
  end
end

#error(str) ⇒ Object

Delegate error to @events



144
145
146
# File 'lib/puma/cli.rb', line 144

def error(str)
  @events.error str
end

#generate_restart_dataObject



56
57
58
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
91
92
93
94
95
# File 'lib/puma/cli.rb', line 56

def generate_restart_data
  # Use the same trick as unicorn, namely favor PWD because
  # it will contain an unresolved symlink, useful for when
  # the pwd is /data/releases/current.
  if dir = ENV['PWD']
    s_env = File.stat(dir)
    s_pwd = File.stat(Dir.pwd)

    if s_env.ino == s_pwd.ino and s_env.dev == s_pwd.dev
      @restart_dir = dir
      @options[:worker_directory] = dir
    end
  end

  @restart_dir ||= Dir.pwd

  @original_argv = ARGV.dup

  if defined? Rubinius::OS_ARGV
    @restart_argv = Rubinius::OS_ARGV
  else
    require 'rubygems'

    # if $0 is a file in the current directory, then restart
    # it the same, otherwise add -S on there because it was
    # picked up in PATH.
    #
    if File.exists?($0)
      arg0 = [Gem.ruby, $0]
    else
      arg0 = [Gem.ruby, "-S", $0]
    end

    # Detect and reinject -Ilib from the command line
    lib = File.expand_path "lib"
    arg0[1,0] = ["-I", lib] if $:[0] == lib

    @restart_argv = arg0 + ARGV
  end
end

#graceful_stop(server) ⇒ Object



352
353
354
355
356
357
358
# File 'lib/puma/cli.rb', line 352

def graceful_stop(server)
  log " - Gracefully stopping, waiting for requests to finish"
  @status.stop(true) if @status
  server.stop(true)
  delete_pidfile
  log " - Goodbye!"
end

#jruby?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/puma/cli.rb', line 154

def jruby?
  IS_JRUBY
end

#jruby_daemon?Boolean

Returns:

  • (Boolean)


412
413
414
# File 'lib/puma/cli.rb', line 412

def jruby_daemon?
  daemon? and jruby?
end

#log(str) ⇒ Object

Delegate log to @events



138
139
140
# File 'lib/puma/cli.rb', line 138

def log(str)
  @events.log str
end

#output_headerObject



416
417
418
419
420
421
422
423
# File 'lib/puma/cli.rb', line 416

def output_header
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  log "Puma #{Puma::Const::PUMA_VERSION} starting..."
  log "* Min threads: #{min_t}, max threads: #{max_t}"
  log "* Environment: #{ENV['RACK_ENV']}"
end

#parse_optionsObject

:nodoc:



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/puma/cli.rb', line 332

def parse_options
  @parser.parse! @argv

  if @argv.last
    @options[:rackup] = @argv.shift
  end

  @config = Puma::Configuration.new @options

  # Advertise the Configuration
  Puma.cli_config = @config

  @config.load

  if @options[:workers] > 0
    unsupported "worker mode not supported on JRuby and Windows",
                jruby? || windows?
  end
end

#redirect_ioObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/puma/cli.rb', line 360

def redirect_io
  stdout = @options[:redirect_stdout]
  stderr = @options[:redirect_stderr]
  append = @options[:redirect_append]

  if stdout
    STDOUT.reopen stdout, (append ? "a" : "w")
    STDOUT.sync = true
    STDOUT.puts "=== puma startup: #{Time.now} ==="
  end

  if stderr
    STDERR.reopen stderr, (append ? "a" : "w")
    STDERR.sync = true
    STDERR.puts "=== puma startup: #{Time.now} ==="
  end
end

#restart!Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puma/cli.rb', line 97

def restart!
  @options[:on_restart].each do |blk|
    blk.call self
  end

  if jruby?
    @binder.listeners.each_with_index do |(str,io),i|
      io.close

      # We have to unlink a unix socket path that's not being used
      uri = URI.parse str
      if uri.scheme == "unix"
        path = "#{uri.host}#{uri.path}"
        File.unlink path
      end
    end

    require 'puma/jruby_restart'
    JRubyRestart.chdir_exec(@restart_dir, @restart_argv)
  else
    redirects = {:close_others => true}
    @binder.listeners.each_with_index do |(l,io),i|
      ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
      redirects[io.to_i] = io.to_i
    end

    if cmd = @options[:restart_cmd]
      argv = cmd.split(' ') + @original_argv
    else
      argv = @restart_argv
    end

    Dir.chdir @restart_dir

    argv += [redirects] unless RUBY_VERSION < '1.9'
    Kernel.exec(*argv)
  end
end

#restart_on_stop!Object



52
53
54
# File 'lib/puma/cli.rb', line 52

def restart_on_stop!
  @restart = true
end

#runObject

Parse the options, load the rackup, start the server and wait for it to finish.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/puma/cli.rb', line 381

def run
  begin
    parse_options
  rescue UnsupportedOption
    exit 1
  end

  if dir = @options[:directory]
    Dir.chdir dir
  end

  clustered = @options[:workers] > 0

  if clustered
    @events = PidEvents.new STDOUT, STDERR
    @options[:logger] = @events
  end

  set_rack_environment

  if clustered
    run_cluster
  else
    run_single
  end
end

#run_clusterObject



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'lib/puma/cli.rb', line 712

def run_cluster
  log "Puma #{Puma::Const::PUMA_VERSION} starting in cluster mode..."
  log "* Process workers: #{@options[:workers]}"
  log "* Min threads: #{@options[:min_threads]}, max threads: #{@options[:max_threads]}"
  log "* Environment: #{ENV['RACK_ENV']}"

  @binder.parse @options[:binds], self

  unless @config.app_configured?
    error "No application configured, nothing to run"
    exit 1
  end

  read, @wakeup = Puma::Util.pipe

  Signal.trap "SIGCHLD" do
    wakeup!
  end

  stop = false

  begin
    Signal.trap "SIGUSR2" do
      @restart = true
      stop = true
      wakeup!
    end
  rescue Exception
  end

  master_pid = Process.pid

  begin
    Signal.trap "SIGTERM" do
      # The worker installs their own SIGTERM when booted.
      # Until then, this is run by the worker and the worker
      # should just exit if they get it.
      if Process.pid != master_pid
        log "Early termination of worker"
        exit! 0
      else
        stop = true
        wakeup!
      end
    end
  rescue Exception
  end

  phased_restart = false

  begin
    Signal.trap "SIGUSR1" do
      phased_restart = true
      wakeup!
    end
  rescue Exception
  end

  # Used by the workers to detect if the master process dies.
  # If select says that @check_pipe is ready, it's because the
  # master has exited and @suicide_pipe has been automatically
  # closed.
  #
  @check_pipe, @suicide_pipe = Puma::Util.pipe

  if @options[:daemon]
    Process.daemon(true)
  else
    log "Use Ctrl-C to stop"
  end

  @master_pid = Process.pid

  redirect_io

  write_state

  @master_read, @worker_write = read, @wakeup
  spawn_workers

  Signal.trap "SIGINT" do
    stop = true
    wakeup!
  end

  begin
    while !stop
      begin
        res = IO.select([read], nil, nil, 5)

        if res
          req = read.read_nonblock(1)

          if req == "b"
            pid = read.gets.to_i
            w = @workers.find { |x| x.pid == pid }
            if w
              w.boot!
              log "- Worker #{pid} booted, phase: #{w.phase}"
            else
              log "! Out-of-sync worker list, no #{pid} worker"
            end
          end
        end

        check_workers

        if phased_restart
          start_phased_restart
          phased_restart = false
        end

      rescue Interrupt
        stop = true
      end
    end

    stop_workers
  ensure
    delete_pidfile
    @check_pipe.close
    @suicide_pipe.close
    read.close
    @wakeup.close
  end

  if @restart
    log "* Restarting..."
    restart!
  end
end

#run_singleObject



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/puma/cli.rb', line 457

def run_single
  already_daemon = false

  if jruby_daemon?
    require 'puma/jruby_restart'

    if JRubyRestart.daemon?
      # Bind before redirecting IO so binding errors show up on stdout/stderr
      @binder.parse @options[:binds], self
    end

    already_daemon = JRubyRestart.daemon_init
  end

  output_header

  if !jruby_daemon?
    @binder.parse @options[:binds], self
  end

  unless @config.app_configured?
    error "No application configured, nothing to run"
    exit 1
  end

  # Load the app before we daemonize.
  begin
    app = @config.app
  rescue Exception => e
    log "! Unable to load application"
    raise e
  end

  if jruby_daemon?
    unless already_daemon
      require 'puma/jruby_restart'

      pid = nil

      Signal.trap "SIGUSR2" do
        log "* Started new process #{pid} as daemon..."
        exit
      end

      pid = JRubyRestart.daemon_start(@restart_dir, @restart_argv)
      sleep
    end
  elsif daemon?
    Process.daemon(true)
  end

  write_state

  server = Puma::Server.new app, @events
  server.binder = @binder
  server.min_threads = @options[:min_threads]
  server.max_threads = @options[:max_threads]

  @server = server

  if str = @options[:control_url]
    start_control server, str
  end

  begin
    Signal.trap "SIGUSR2" do
      @restart = true
      server.begin_restart
    end
  rescue Exception
    log "*** Sorry signal SIGUSR2 not implemented, restart feature disabled!"
  end

  begin
    Signal.trap "SIGTERM" do
      log " - Gracefully stopping, waiting for requests to finish"
      server.stop false
    end
  rescue Exception
    log "*** Sorry signal SIGTERM not implemented, gracefully stopping feature disabled!"
  end

  unless @options[:daemon]
    log "Use Ctrl-C to stop"
  end

  redirect_io

  if jruby?
    Signal.trap("INT") do
      graceful_stop server
      exit
    end
  end

  begin
    server.run.join
  rescue Interrupt
    graceful_stop server
  end

  if @restart
    log "* Restarting..."
    @status.stop true if @status
    restart!
  end
end

#set_rack_environmentObject



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/puma/cli.rb', line 293

def set_rack_environment 
  # Try the user option first, then the environment variable,
  # finally default to development

  env = @options[:environment] ||
               ENV['RACK_ENV'] ||
                 'development'

  @options[:environment] = env
  ENV['RACK_ENV'] = env
end

#setup_optionsObject

Build the OptionParser object to handle the available options.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/puma/cli.rb', line 170

def setup_options
  @options = {
    :min_threads => 0,
    :max_threads => 16,
    :quiet => false,
    :debug => false,
    :binds => [],
    :workers => 0,
    :daemon => false,
    :worker_boot => []
  }

  @parser = OptionParser.new do |o|
    o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
      @options[:binds] << arg
    end

    o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
      @options[:config_file] = arg
    end

    o.on "--control URL", "The bind url to use for the control server",
                          "Use 'auto' to use temp unix server" do |arg|
      if arg
        @options[:control_url] = arg
      elsif jruby?
        unsupported "No default url available on JRuby"
      end
    end

    o.on "--control-token TOKEN",
         "The token to use as authentication for the control server" do |arg|
      @options[:control_auth_token] = arg
    end

    o.on "-d", "--daemon", "Daemonize the server into the background" do
      @options[:daemon] = true
      @options[:quiet] = true
    end

    o.on "--debug", "Log lowlevel debugging information" do
      @options[:debug] = true
    end

    o.on "--dir DIR", "Change to DIR before starting" do |d|
      @options[:directory] = d.to_s
      @options[:worker_directory] = d.to_s
    end

    o.on "-e", "--environment ENVIRONMENT",
         "The environment to run the Rack app on (default development)" do |arg|
      @options[:environment] = arg
    end

    o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
      $LOAD_PATH.unshift(*arg.split(':'))
    end

    o.on "-p", "--port PORT", "Define what port TCP port to bind to",
                              "Use -b for more advanced options" do |arg|
      @options[:binds] << "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
    end

    o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
      @options[:pidfile] = arg
    end

    o.on "-q", "--quiet", "Quiet down the output" do
      @options[:quiet] = true
    end

    o.on "-R", "--restart-cmd CMD",
         "The puma command to run during a hot restart",
         "Default: inferred" do |cmd|
      @options[:restart_cmd] = cmd
    end

    o.on "-S", "--state PATH", "Where to store the state details" do |arg|
      @options[:state] = arg
    end

    o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
      min, max = arg.split(":")
      if max
        @options[:min_threads] = min.to_i
        @options[:max_threads] = max.to_i
      else
        @options[:min_threads] = 0
        @options[:max_threads] = arg.to_i
      end
    end

    o.on "-V", "--version", "Print the version information" do
      puts "puma version #{Puma::Const::VERSION}"
      exit 1
    end

    o.on "-w", "--workers COUNT",
               "Activate cluster mode: How many worker processes to create" do |arg|
      @options[:workers] = arg.to_i
    end

  end

  @parser.banner = "puma <options> <rackup file>"

  @parser.on_tail "-h", "--help", "Show help" do
    log @parser
    exit 1
  end
end

#spawn_workersObject



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/puma/cli.rb', line 660

def spawn_workers
  diff = @options[:workers] - @workers.size

  upgrade = (@phased_state == :waiting)

  diff.times do
    pid = fork { worker(upgrade) }
    debug "Spawned worker: #{pid}"
    @workers << Worker.new(pid, @phase)
  end

  if diff > 0
    @phased_state = :idle
  end
end

#start_control(server, str) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/puma/cli.rb', line 425

def start_control(server, str)
  require 'puma/app/status'

  uri = URI.parse str

  app = Puma::App::Status.new server, self

  if token = @options[:control_auth_token]
    app.auth_token = token unless token.empty? or token == :none
  end

  status = Puma::Server.new app, @events
  status.min_threads = 0
  status.max_threads = 1

  case uri.scheme
  when "tcp"
    log "* Starting status server on #{str}"
    status.add_tcp_listener uri.host, uri.port
  when "unix"
    log "* Starting status server on #{str}"
    path = "#{uri.host}#{uri.path}"

    status.add_unix_listener path
  else
    error "Invalid status URI: #{str}"
  end

  status.run
  @status = status
end

#start_phased_restartObject



630
631
632
633
# File 'lib/puma/cli.rb', line 630

def start_phased_restart
  @phase += 1
  log "- Starting phased worker restart, phase: #{@phase}"
end

#stopObject



844
845
846
847
848
# File 'lib/puma/cli.rb', line 844

def stop
  @status.stop(true) if @status
  @server.stop(true) if @server
  delete_pidfile
end

#stop_workersObject



617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/puma/cli.rb', line 617

def stop_workers
  log "- Gracefully shutting down workers..."
  @workers.each { |x| x.term }

  begin
    Process.waitall
  rescue Interrupt
    log "! Cancelled waiting for workers"
  else
    log "- Goodbye!"
  end
end

#unsupported(str, cond = true) ⇒ Object

Raises:



162
163
164
165
166
# File 'lib/puma/cli.rb', line 162

def unsupported(str, cond=true)
  return unless cond
  @events.error str
  raise UnsupportedOption
end

#wakeup!Object



705
706
707
708
709
710
# File 'lib/puma/cli.rb', line 705

def wakeup!
  begin
    @wakeup.write "!" unless @wakeup.closed?
  rescue SystemCallError, IOError
  end
end

#windows?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/puma/cli.rb', line 158

def windows?
  RUBY_PLATFORM =~ /mswin32|ming32/
end

#worker(upgrade) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/puma/cli.rb', line 565

def worker(upgrade)
  $0 = "puma: cluster worker: #{@master_pid}"
  Signal.trap "SIGINT", "IGNORE"

  @master_read.close
  @suicide_pipe.close

  Thread.new do
    IO.select [@check_pipe]
    log "! Detected parent died, dying"
    exit! 1
  end

  # Be sure to change the directory again before loading
  # the app. This way we can pick up new code.
  if upgrade
    if dir = @options[:worker_directory]
      log "+ Changing to #{dir}"
      Dir.chdir dir
    end
  end

  # Invoke any worker boot hooks so they can get
  # things in shape before booting the app.
  hooks = @options[:worker_boot]
  hooks.each { |h| h.call }

  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  server = Puma::Server.new @config.app, @events
  server.min_threads = min_t
  server.max_threads = max_t
  server.inherit_binder @binder

  Signal.trap "SIGTERM" do
    server.stop
  end

  begin
    @worker_write << "b#{Process.pid}\n"
  rescue SystemCallError, IOError
    STDERR.puts "Master seems to have exitted, exitting."
    return
  end

  server.run.join

ensure
  @worker_write.close
end

#write_pidObject

If configured, write the pid of the current process out to a file.



285
286
287
288
289
290
291
# File 'lib/puma/cli.rb', line 285

def write_pid
  if path = @options[:pidfile]
    File.open(path, "w") do |f|
      f.puts Process.pid
    end
  end
end

#write_stateObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/puma/cli.rb', line 311

def write_state
  write_pid

  require 'yaml'

  if path = @options[:state]
    state = { "pid" => Process.pid }

    cfg = @config.dup
    
    [ :logger, :worker_boot, :on_restart ].each { |o| cfg.options.delete o }

    state["config"] = cfg

    File.open(path, "w") do |f|
      f.write state.to_yaml
    end
  end
end