Class: Bluth::ScheduleWorker

Inherits:
Storable
  • Object
show all
Includes:
WorkerBase, Daemonizable, Familia, Familia::Stamps, Logging
Defined in:
lib/bluth/worker.rb

Class Attribute Summary collapse

Attributes included from Daemonizable

#log_file, #pid_file

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Daemonizable

#change_privilege, #daemonize, included, #on_restart, #pid, #restart

Methods included from Logging

#debug, debug, debug?, #log, log, #log_error, log_error, #silent, #silent=, silent?, #trace, trace, trace?

Methods included from WorkerBase

#current_job, included, #init, #kill, #name, #wid, #working!

Class Attribute Details

.interval(v = nil) ⇒ Object

Returns the value of attribute interval.



315
316
317
# File 'lib/bluth/worker.rb', line 315

def interval
  @interval
end

.scheduleObject

Returns the value of attribute schedule.



315
316
317
# File 'lib/bluth/worker.rb', line 315

def schedule
  @schedule
end

.timeoutObject

Returns the value of attribute timeout.



315
316
317
# File 'lib/bluth/worker.rb', line 315

def timeout
  @timeout
end

Class Method Details

.every(interval = nil, opts = {}, &blk) ⇒ Object



320
321
322
323
324
325
# File 'lib/bluth/worker.rb', line 320

def every interval=nil, opts={}, &blk
  unless interval.nil?
    @every << [interval, opts, blk]
  end
  @every
end

Instance Method Details

#runObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/bluth/worker.rb', line 341

def run
  begin
    EM.run {
      @process_id = $$
      srand(Bluth.salt.to_i(16) ** @process_id)
      Bluth.connect
      Familia.info "Setting interval: #{Worker.interval} sec (queuetimeout: #{Bluth.queuetimeout})"
      Familia.reconnect_all! # Need to reconnect after daemonize
      raise Familia::Problem, "Only 1 scheduler at a time" if !ScheduleWorker.instances.empty?
      self.class.runblock :onstart
      save # persist and make note the scheduler is running
      ScheduleWorker.schedule = Rufus::Scheduler::EmScheduler.start_new
      self.class.every.each do |args|
        interval, opts, blk = *args
        Familia.ld " scheduling every #{interval}: #{opts}" if Familia.debug?
        ScheduleWorker.schedule.every interval, opts, &blk
      end
    }
  rescue => ex
    msg = "#{ex.class}: #{ex.message}"
    puts msg
    Familia.info ex.backtrace
    Familia.trace :EXCEPTION, msg, caller[1] if Familia.debug?
    self.class.runblock :onexit
    destroy!
  rescue Interrupt => ex
    puts $/, "Exiting..."
    self.class.runblock :onexit
    destroy!
  end
end

#run!Object



337
338
339
# File 'lib/bluth/worker.rb', line 337

def run!
  run
end