Class: Bluth::ScheduleWorker

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

Overview

TODO: Refactor somehow. When this is subclassed (eg BS::SchduleWorker) the self.object is not created.

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.



266
267
268
# File 'lib/bluth/worker.rb', line 266

def interval
  @interval
end

.scheduleObject

Returns the value of attribute schedule.



266
267
268
# File 'lib/bluth/worker.rb', line 266

def schedule
  @schedule
end

.timeoutObject

Returns the value of attribute timeout.



266
267
268
# File 'lib/bluth/worker.rb', line 266

def timeout
  @timeout
end

Class Method Details

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



271
272
273
274
275
276
# File 'lib/bluth/worker.rb', line 271

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

Instance Method Details

#runObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/bluth/worker.rb', line 292

def run
  begin
    raise Familia::Problem, "Only 1 scheduler at a time" if !ScheduleWorker.instances.empty?
    EM.run {
      @process_id = $$
      srand(Bluth.salt.to_i(16) ** @process_id)
      ScheduleWorker.schedule = Rufus::Scheduler::EmScheduler.start_new
      save # persist and make note the scheduler is running
      self.class.runblock :onstart
      self.class.every.each do |args|
        interval, opts, blk = *args
        Familia.ld " scheduling every #{interval}: #{opts}"
        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



288
289
290
# File 'lib/bluth/worker.rb', line 288

def run!
  run
end