Class: Shinq::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ CLI

Returns a new instance of CLI.



14
15
16
17
# File 'lib/shinq/cli.rb', line 14

def initialize(args=ARGV)
  setup_option(args)
  bootstrap
end

Instance Method Details

#bootstrapObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/shinq/cli.rb', line 80

def bootstrap
  target = options.require
  Shinq.logger = Shinq::Logger.logger

  if File.directory?(target)
    require 'rails'
    require File.expand_path("#{target}/config/application.rb")

    require 'shinq/rails'
    require File.expand_path("#{target}/config/environment.rb")
  else
    require target
  end
end

#optionsObject



76
77
78
# File 'lib/shinq/cli.rb', line 76

def options
  Shinq.configuration
end

#parse_options(args) ⇒ Object



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

def parse_options(args)
  opts = {}
  parser = OptionParser.new do |opt|
    opt.on('-d', '--daemon', 'Daemonize process') do |v|
      opts[:daemonize] = v
    end

    opt.on('-w', '--worker value', 'Name of worker class') do |v|
      opts[:worker_name] = v
    end

    opt.on('-p', '--process VALUE', 'Number of workers') do |v|
      opts[:process] = v.to_i
    end

    opt.on('--queue-timeout VALUE', 'Waiting queue time(sec). use function of queue_wait(Q4M)') do |v|
      opts[:queue_timeout] = v.to_i
    end

    opt.on('--db-config VALUE', 'Specify database configuration file') do |v|
      raise OptionParseError, "#{v} does not exist" unless File.exist?(v)
      opts[:db_config] = YAML.load_file(v)
    end

    opt.on('--queue-database VALUE', 'Name of queue database') do |v|
      raise OptionParseError, "#{v}'s settings does not exist" unless opts[:db_config][v]
      opts[:queue_db] = v
    end

    opt.on('-r', '--require VALUE', 'Add require path') do |v|
      opts[:require] = v
    end

    opt.on('-l', '--max-lifecycle VALUE', 'Refork process when loopnumber is over specify number') do |v|
      opts[:lifecycle] = v.to_i
    end

    opt.on('-s', '--statistics VALUE', 'Display queue statistics interval time(sec)') do |v|
      opts[:statistics] = v.to_i
    end

    opt.on('-v', '--version', 'Print version') do |v|
      puts "Shinq #{Shinq::VERSION}"
      exit(0)
    end
  end

  parser.parse!(args)
  opts
end

#runObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/shinq/cli.rb', line 95

def run
  klass = !options.statistics.nil? && options.statistics ? Shinq::Statistics : Shinq::Launcher

  se = ServerEngine.create(nil, klass, {
    daemonize: options.daemonize,
    worker_type: 'process',
    pid_file: 'shinq.pid',
    workers: options.process,
    logger: options.daemonize ? Shinq.logger : nil
  })

  se.run
end

#setup_option(args) ⇒ Object



19
20
21
22
23
# File 'lib/shinq/cli.rb', line 19

def setup_option(args)
  opts = parse_options(args)

  Shinq.configuration = Shinq::Configuration.new(opts)
end