Class: BackgroundQueue::ServerLib::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/background_queue/server_lib/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



16
17
18
19
20
21
22
23
24
# File 'lib/background_queue/server_lib/server.rb', line 16

def initialize
  @running = false
  @stat_mutex = Mutex.new
  @stats = {
    :tasks=>0,
    :run_tasks=>0,
    :running=>0
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/background_queue/server_lib/server.rb', line 7

def config
  @config
end

#error_tasksObject

Returns the value of attribute error_tasks.



10
11
12
# File 'lib/background_queue/server_lib/server.rb', line 10

def error_tasks
  @error_tasks
end

#event_serverObject

Returns the value of attribute event_server.



11
12
13
# File 'lib/background_queue/server_lib/server.rb', line 11

def event_server
  @event_server
end

#jobsObject

Returns the value of attribute jobs.



13
14
15
# File 'lib/background_queue/server_lib/server.rb', line 13

def jobs
  @jobs
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/background_queue/server_lib/server.rb', line 14

def logger
  @logger
end

#task_queueObject

Returns the value of attribute task_queue.



9
10
11
# File 'lib/background_queue/server_lib/server.rb', line 9

def task_queue
  @task_queue
end

#thread_managerObject

Returns the value of attribute thread_manager.



8
9
10
# File 'lib/background_queue/server_lib/server.rb', line 8

def thread_manager
  @thread_manager
end

#workersObject

Returns the value of attribute workers.



12
13
14
# File 'lib/background_queue/server_lib/server.rb', line 12

def workers
  @workers
end

Instance Method Details

#change_stat(stat, delta) ⇒ Object



279
280
281
282
283
# File 'lib/background_queue/server_lib/server.rb', line 279

def change_stat(stat, delta)
  @stat_mutex.synchronize {
    @stats[stat] += delta
  }
end

#check_not_running(options) ⇒ Object



152
153
154
155
156
# File 'lib/background_queue/server_lib/server.rb', line 152

def check_not_running(options)
  proc_id = get_pid(options)
  raise BackgroundQueue::ServerLib::InitError, "Process #{proc_id} already running" unless proc_id.nil?
  nil
end

#daemonize(options) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/background_queue/server_lib/server.rb', line 210

def daemonize(options)
  fork{
    stdin = open '/dev/null', 'r'
    stdout = open '/dev/null', 'w'
    stderr = open '/dev/null', 'w'
    STDIN.reopen stdin
    STDOUT.reopen stdout
    STDERR.reopen stderr
    fork{
      write_pid(options) unless options[:skip_pid]
      run(options)
    } and exit!
  }
end

#get_pid(options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/background_queue/server_lib/server.rb', line 133

def get_pid(options)
  sPid = nil
  begin
    sPid = File.open(get_pid_path(options)) { |f|
      f.read
    }
  rescue
    return nil
  end
  return nil if sPid.nil? || sPid.to_i == 0
  nPid = sPid.to_i
  begin
    Process.kill(0, nPid)
    return nPid
  rescue
    return nil
  end
end

#get_pid_path(options) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/background_queue/server_lib/server.rb', line 125

def get_pid_path(options)
  if options[:pid_file]
    options[:pid_file]
  else
    "/var/run/background_queue.pid"
  end
end

#get_statsObject



285
286
287
288
289
# File 'lib/background_queue/server_lib/server.rb', line 285

def get_stats
  @stat_mutex.synchronize {
     @stats.clone
  }
end

#init_logging(path, level) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/background_queue/server_lib/server.rb', line 108

def init_logging(path, level)
  unless path.nil? || path.to_s.strip.length == 0
    path = resolve_logging_path(path)
    begin
      @logger = Logger.new(path, "daily")
      set_logging_level(@logger, level)
    rescue Exception=>e
      raise BackgroundQueue::ServerLib::InitError, "Error initializing log file #{path}: #{e.message}"
    end
  end
  if @logger.nil?
    #just make a fallback logger...
    @logger = Logger.new($stderr)
    set_logging_level(@logger, "fatal")
  end
end

#kill_pid(options) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/background_queue/server_lib/server.rb', line 175

def kill_pid(options)
  proc_id = get_pid(options)
  begin
    Process.kill(9, proc_id) unless proc_id.nil?
  rescue 
    #dont care... the process may have died already?
  end
end

#load_configuration(path) ⇒ Object



77
78
79
80
# File 'lib/background_queue/server_lib/server.rb', line 77

def load_configuration(path)
  @config = BackgroundQueue::ServerLib::Config.load_file(path)
  true
end

#load_tasks(path) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/background_queue/server_lib/server.rb', line 291

def load_tasks(path)
  return if path.nil?
  if File.exist?(path)
    begin
      File.open(path, 'r') { |io| 
        task_queue.load_from_file(io)
      }
    rescue Exception=>e
      logger.error("Error loading tasks from #{path}: #{e.message}")
      logger.debug(e.backtrace.join("\n"))
    end
  end
end

#process_args(argv) ⇒ Object



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
75
# File 'lib/background_queue/server_lib/server.rb', line 26

def process_args(argv)
  argv = argv.clone
  cmd = argv.shift
  
  if cmd.nil?
    raise BackgroundQueue::ServerLib::InitError, "Usage: server command [options]"
  end
  
  options = {:command=>cmd.nil? ? nil : cmd.downcase.intern}
  
  env_to_load = "development"
  
  OptionParser.new do |opts|
    opts.banner = "Usage: server command [options]"
    case options[:command]
      when :start, :test, :run
        opts.on("-c", "--config PATH", "Configuration Path") do |cp|
          options[:config] = cp
        end
      when :stop
        
      when nil
          
      else
        raise "Invalid Command: #{cmd}"
    end
    opts.on("-l", "--logfile [PATH]", "Logfile Path") do |lf|
      options[:log_file] = lf
    end
    opts.on("-v", "--loglevel [LEVEL]", "Log Level") do |ll|
      options[:log_level] = ll
    end
    opts.on("-p", "--pidfile [PATH]", "Pid file Path (/var/run/background_queue.pid)") do |pf|
      options[:pid_file] = pf
    end
    opts.on("-e", "--environment [RAILS_ENV]", "testing/development/production (development)") do |env|
      env_to_load = env
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end.parse!(argv)
  
  ENV['RAILS_ENV']=env_to_load
  
  raise BackgroundQueue::ServerLib::InitError, "Missing config argument (-c)" if options[:config].nil? && ([:test, :start].include?(options[:command]) )

  options
end

#remove_pid(options) ⇒ Object



195
196
197
198
199
200
# File 'lib/background_queue/server_lib/server.rb', line 195

def remove_pid(options)
  begin
    File.delete(get_pid_path(options))
  rescue 
  end
end

#resolve_logging_path(path) ⇒ Object



82
83
84
# File 'lib/background_queue/server_lib/server.rb', line 82

def resolve_logging_path(path)
  File.expand_path(path)
end

#run(options) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/background_queue/server_lib/server.rb', line 250

def run(options)
  trap_signals
  @running = true
  @thread_manager = BackgroundQueue::ServerLib::ThreadManager.new(self, self.config.connections_per_worker)
  
  @workers = BackgroundQueue::ServerLib::WorkerBalancer.new(self)
  @task_queue = BackgroundQueue::ServerLib::BalancedQueue.new(self)
  
  @thread_manager.start(BackgroundQueue::ServerLib::WorkerThread)
  
  @event_server = BackgroundQueue::ServerLib::EventServer.new(self)
  
  @error_tasks = BackgroundQueue::ServerLib::ErrorTaskList.new(self)
  
  @jobs = BackgroundQueue::ServerLib::JobRegistry.new
  
  load_tasks(config.task_file)
  
  @event_server.start
end

#running?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/background_queue/server_lib/server.rb', line 246

def running?
  @running
end

#save_tasks(path) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/background_queue/server_lib/server.rb', line 305

def save_tasks(path)
  return if path.nil?
  
  begin
    File.open(path, 'w') { |io| 
      task_queue.save_to_file(io)
    }
  rescue Exception=>e
    logger.error("Error saving tasks to #{path}: #{e.message}")
    logger.debug(e.backtrace.join("\n"))
  end
end

#set_logging_level(log, level) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/background_queue/server_lib/server.rb', line 86

def set_logging_level(log, level)
  if level.nil? || level.strip.length == 0
    level = "warn" 
  else
    level = level.to_s.downcase
  end
  case level
  when 'debug'
    log.level = Logger::DEBUG
  when 'info'
    log.level = Logger::INFO
  when 'warn'
    log.level = Logger::WARN
  when 'error'
    log.level = Logger::ERROR
  when 'fatal'
    log.level = Logger::FATAL
  else
    raise BackgroundQueue::ServerLib::InitError, "Unknown logging level: #{level}"
  end
end

#start(options) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/background_queue/server_lib/server.rb', line 225

def start(options)
  begin
    load_configuration(options[:config])
    init_logging(options[:log_file], options[:log_level])
    check_not_running(options) unless options[:skip_pid]
    write_pid(options) unless options[:skip_pid] #this will make sure we can write the pid file... the daemon will write it again
    if options[:command] == :start
      daemonize(options)
    elsif options[:command] == :run
      run(options)
    else
      raise BackgroundQueue::ServerLib::InitError, "Unknown Command: #{options[:command]}"
    end
  rescue BackgroundQueue::ServerLib::InitError=>ie
    STDERR.puts ie.message
  rescue Exception=>e
    STDERR.puts e.message
    STDERR.puts e.backtrace.join("\n")
  end
end

#stop(timeout_secs = 10) ⇒ Object



271
272
273
274
275
276
277
# File 'lib/background_queue/server_lib/server.rb', line 271

def stop(timeout_secs=10)
  @running = false
  @event_server.stop
  @thread_manager.wait(timeout_secs)
  @error_tasks.flush
  save_tasks(config.task_file)
end

#stop_pid(options) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/background_queue/server_lib/server.rb', line 158

def stop_pid(options)
  proc_id = get_pid(options)
  unless proc_id.nil?
    begin
      Process.kill(15, proc_id) 
    rescue 
      #dont care... the process may have died already?
    end
    count = 0
    while get_pid(options) && count < 10
      puts "Waiting..."
      sleep(1)
    end
    kill_pid(options) #make sure
  end
end

#trap_signalsObject



202
203
204
205
206
207
# File 'lib/background_queue/server_lib/server.rb', line 202

def trap_signals
  Signal.trap("TERM") do
    puts "Terminating..."
    self.stop()
  end
end

#write_pid(options) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/background_queue/server_lib/server.rb', line 184

def write_pid(options)
  proc_id = Process.pid
  begin
    File.open(get_pid_path(options), "w") { |f|
      f.write(proc_id.to_s)
    }
  rescue Exception=>e
    raise BackgroundQueue::ServerLib::InitError, "Unable to write to pid file #{get_pid_path(options)}: #{e.message}"
  end
end