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.



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

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

Instance Method Details

#bootstrapObject



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

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

#initialize_shinqObject



109
110
111
112
# File 'lib/shinq/cli.rb', line 109

def initialize_shinq
  Shinq::Client.fetch_column_names(table_name: Shinq.configuration.worker_name.pluralize)
  Shinq.configuration.worker_class # check if worker_class is constantizable before running ServerEngine
end

#optionsObject



90
91
92
# File 'lib/shinq/cli.rb', line 90

def options
  Shinq.configuration
end

#parse_options(args) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shinq/cli.rb', line 27

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('--graceful-kill-timeout VALUE', 'Seconds to SIGQUIT workers even during performing jobs') do |v|
      opts[:graceful_kill_timeout] = 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('--[no-]abort-on-error', 'Abort ') do |v|
      opts[:abort_on_error] = v
    end

    opt.on('--sleep-sec-on-error N', Integer, 'Allow worker to sleep(sec) after exception') do |v|
      opts[:sleep_sec_on_error] = v
    end

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

  parser.parse!(args)
  opts
end

#runObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shinq/cli.rb', line 114

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,
    worker_graceful_kill_timeout: options.graceful_kill_timeout,
    logger: options.daemonize ? Shinq.logger : nil,
  })

  se.run
end

#setup_option(args) ⇒ Object



21
22
23
24
25
# File 'lib/shinq/cli.rb', line 21

def setup_option(args)
  opts = parse_options(args)

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