Class: Twigg::Command::App

Inherits:
Twigg::Command show all
Defined in:
lib/twigg-app/command/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ App

Returns a new instance of App.



4
5
6
7
8
9
10
# File 'lib/twigg-app/command/app.rb', line 4

def initialize(*args)
  super
  @daemon  = @args.delete('-D') || @args.delete('--daemon')
  @pidfile = consume_option(%w[-P --pidfile], @args)
  @pidfile_path = File.expand_path(@pidfile) if @pidfile
  ignore @args
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/twigg-app/command/app.rb', line 12

def run
  stderr 'Daemonizing...' if @daemon

  if @pidfile_path
    stderr "Will write to pidfile #{@pidfile}"
    die 'Pidfile already exists' if File.exist?(@pidfile_path)
  end

  Process.daemon if @daemon

  if @pidfile_path
    flags = File::WRONLY | File::CREAT | File::EXCL
    File.open(@pidfile_path, flags) { |f| f.write Process.pid }
    at_exit { File.unlink(@pidfile_path) }
  end

  ::Twigg::App::Server.run!
end