Class: Chimp::ChimpDaemon

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/right_chimp/daemon/ChimpDaemon.rb

Defined Under Namespace

Classes: AdminServlet, DisplayServlet, GenericServlet, GroupServlet, JobServlet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChimpDaemon

Returns a new instance of ChimpDaemon.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 14

def initialize
  @verbose     = false
  @debug       = false
  @port        = 9055
  @concurrency = 50
  @delay       = 0
  @retry_count = 0
  @threads     = []
  @running     = false
  @queue       = ChimpQueue.instance
  @chimp_queue = Queue.new
end

Instance Attribute Details

#chimp_queueObject

Returns the value of attribute chimp_queue.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def chimp_queue
  @chimp_queue
end

#concurrencyObject

Returns the value of attribute concurrency.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def concurrency
  @concurrency
end

#debugObject

Returns the value of attribute debug.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def debug
  @debug
end

#delayObject

Returns the value of attribute delay.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def delay
  @delay
end

#dry_runObject

Returns the value of attribute dry_run.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def dry_run
  @dry_run
end

#logfileObject

Returns the value of attribute logfile.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def logfile
  @logfile
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def port
  @port
end

#queueObject (readonly)

Returns the value of attribute queue.



10
11
12
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 10

def queue
  @queue
end

#retry_countObject

Returns the value of attribute retry_count.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def retry_count
  @retry_count
end

#runningObject (readonly)

Returns the value of attribute running.



10
11
12
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 10

def running
  @running
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#install_signal_handlersObject

Trap signals to exit cleanly



159
160
161
162
163
164
165
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 159

def install_signal_handlers
  ['INT', 'TERM'].each do |signal|
    trap(signal) do
      self.quit
    end
  end
end

#parse_command_lineObject

Parse chimpd command line options



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
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 44

def parse_command_line
  begin
    opts = GetoptLong.new(
      [ '--logfile', '-l',      GetoptLong::REQUIRED_ARGUMENT ],
      [ '--verbose', '-v',      GetoptLong::NO_ARGUMENT ],
      [ '--quiet',   '-q',      GetoptLong::NO_ARGUMENT ],
      [ '--concurrency', '-c',  GetoptLong::REQUIRED_ARGUMENT ],
      [ '--delay', '-d',        GetoptLong::REQUIRED_ARGUMENT ],
      [ '--retry', '-y',        GetoptLong::REQUIRED_ARGUMENT ],
      [ '--port', '-p',         GetoptLong::REQUIRED_ARGUMENT ],
      [ '--exit', '-x', 				GetoptLong::NO_ARGUMENT ]
    )

    opts.each do |opt, arg|
      case opt
        when '--logfile', '-l'
          @logfile = arg
          Log.logger = Logger.new(@logfile)
        when '--concurrency', '-c'
          @concurrency = arg.to_i
        when '--delay', '-d'
          @delay = arg.to_i
        when '--retry', '-y'
          @retry_count = arg.to_i
        when '--verbose', '-v'
          @verbose = true
        when '--quiet',   '-q'
          @quiet = true
        when '--port', '-p'
          @port = arg
        when '--exit', '-x'
        	uri = "http://localhost:#{@port}/admin"
			response = RestClient.post uri, { 'shutdown' => true }.to_yaml
			exit 0
      end
    end
  rescue GetoptLong::InvalidOption => ex
    puts "Syntax: chimpd [--logfile=<name>] [--concurrency=<c>] [--delay=<d>] [--retry=<r>] [--port=<p>] [--verbose]"
    exit 1
  end

  #
  # Set up logging/verbosity
  #
  Chimp.set_verbose(@verbose, @quiet)

  if not @verbose
  	ENV['REST_CONNECTION_LOG'] = "/dev/null"
  	ENV['RESTCLIENT_LOG'] = "/dev/null"
  end

  if @quiet
    Log.threshold = Logger::WARN
  end

end

#quitObject

Quit by waiting for all chimp jobs to finish, not allowing new jobs on the queue, and killing the web server.

TODO: call @queue.quit, but with a short timeout?



173
174
175
176
177
178
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 173

def quit
  @running = false
  @server.shutdown
  sleep 5
  exit 0
end

#runObject

Main entry point for chimpd command line application



30
31
32
33
34
35
36
37
38
39
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 30

def run
  install_signal_handlers
  parse_command_line

  puts "chimpd #{VERSION} launching with #{@concurrency} workers"
  spawn_queue_runner
  spawn_webserver
  spawn_chimpd_submission_processor
  run_forever
end

#run_foreverObject

Process requests forever until we’re killed



147
148
149
150
151
152
153
154
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 147

def run_forever
  @running = true
  while @running
    @threads.each do |t|
      t.join(5)
    end
  end
end

#spawn_chimpd_submission_processorObject

Spawn threads to process submitted requests



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
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 183

def spawn_chimpd_submission_processor
  n = @concurrency/4
  n = 10 if n < 10
  Log.debug "Logging into API..."

  #
  # There is a race condition logging in with rest_connection.
  # As a workaround, do a tag query first thing when chimpd starts.
  #
  begin
    c = Chimp.new
    c.interactive = false
    c.quiet = true
    c.tags = ["bogus:tag=true"]
    c.run
  rescue StandardError
  end

  Log.debug "Spawning #{n} submission processing threads"
  (1..n).each do |n|
    @threads ||=[]
    @threads << Thread.new {
      while true
        begin
          queued_request = @chimp_queue.pop
          group = queued_request.group
          queued_request.interactive = false

          tasks = queued_request.process
          tasks.each do |task|
            ChimpQueue.instance.push(group, task)
          end

        rescue StandardError => ex
          Log.error "submission processor: group=\"#{group}\" script=\"#{queued_request.script}\": #{ex}"
        end
      end
    }
  end
end

#spawn_queue_runnerObject

Spawn the ChimpQueue threads



104
105
106
107
108
109
110
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 104

def spawn_queue_runner
  @queue.max_threads = @concurrency
  @queue.delay = @delay
  @queue.retry_count = @retry_count
  @queue.start
  @running = true
end

#spawn_webserverObject

Spawn a WEBrick Web server



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 115

def spawn_webserver
  opts = {
    :BindAddress  => "localhost",
    :Port         => @port,
    :MaxClients   => 500,
    :RequestTimeout => 120,
    :DoNotReverseLookup => true
  }

  if not @verbose
    opts[:Logger] = WEBrick::Log.new("/dev/null")
    opts[:AccessLog] = [nil, nil]
  end

  @server = ::WEBrick::HTTPServer.new(opts)
  @server.mount('/',         DisplayServlet)
  @server.mount('/display',  DisplayServlet)
  @server.mount('/job',      JobServlet)
  @server.mount('/group',    GroupServlet)
  @server.mount('/admin',    AdminServlet)

  #
  # WEBrick threads
  #
  @threads << Thread.new(1001) do
    @server.start
  end
end