Class: Clacks::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/clacks/command.rb

Constant Summary collapse

PROC_NAME =
::File.basename($0.dup)
PROC_ARGV =
ARGV.map { |a| a.dup }

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/clacks/command.rb', line 8

def initialize(args)
  @options = { config_file: 'config/clacks.rb' }

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROC_NAME} [options]"

    if Clacks.rails_env?
      opts.separator "Rails options:"
      opts.on("-E", "--env RAILS_ENV", "use RAILS_ENV for defaults (default: development)") do |e|
        ENV['RAILS_ENV'] = e
      end
    end

    opts.separator "Ruby options:"

    opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
      $DEBUG = true
    end

    opts.on("-w", "--warn", "turn warnings on for your script") do
      $-w = true
    end

    opts.separator "Clacks options:"

    opts.on("-c", "--config-file FILE", "Clacks-specific config file (default: #{@options[:config_file]})") do |f|
      @options[:config_file] = f
    end

    opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
      @options[:daemonize] = !!d
    end

    opts.on("-P", "--pid FILE", "file to store PID (default: clacks.pid)") { |f|
      @options[:pid] = f
    }

    opts.separator "Common options:"

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
      exit
    end

    opts.on_tail("-v", "--version", "Show version") do
      puts "#{PROC_NAME} v#{Clacks::VERSION}"
      exit
    end
  end
  @args = opts.parse!(args)
end

Instance Method Details

#execObject



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
# File 'lib/clacks/command.rb', line 60

def exec
  daemonize if @options[:daemonize]

  Clacks.require_rails if Clacks.rails_env?

  Clacks.config = config = Clacks::Configurator.new(@options[:config_file])
  unless config[:pop3] || config[:imap]
    $stderr.puts "Either a POP3 or an IMAP server must be configured"
    exit!(1)
  end

  reopen_io($stdout, config[:stdout_path])
  reopen_io($stderr, config[:stderr_path])

  unless config[:pid]
    config.pid(@options[:pid] || 'clacks.pid')
  end
  pid = config[:pid]
  if wpid = running?(pid)
    $stderr.puts "#{Clacks::Command::PROC_NAME} already running with pid: #{wpid} (or stale #{pid})"
    exit!(1)
  end
  write_pid(pid)

  config[:after_initialize].call if config[:after_initialize]

  proc_name('master')

  setup_signal_handling

  @service = Clacks::Service.new
  @service.run
end