Class: Resque::Scheduler

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/resque/scheduler.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.muteObject

If set, produces no output



16
17
18
# File 'lib/resque/scheduler.rb', line 16

def mute
  @mute
end

.verboseObject

If true, logs more stuff…



13
14
15
# File 'lib/resque/scheduler.rb', line 13

def verbose
  @verbose
end

Class Method Details

.clear_schedule!Object

Stops old rufus scheduler and creates a new one. Returns the new rufus scheduler



108
109
110
111
112
# File 'lib/resque/scheduler.rb', line 108

def clear_schedule!
  rufus_scheduler.stop
  @rufus_scheduler = nil
  rufus_scheduler
end

.enqueue_from_config(config) ⇒ Object

Enqueues a job based on a config hash



91
92
93
94
95
96
97
98
99
100
# File 'lib/resque/scheduler.rb', line 91

def enqueue_from_config(config)
  args = config['args'] || config[:args]
  klass_name = config['class'] || config[:class]
  params = args.nil? ? [] : Array(args)
  queue = config['queue'] || config[:queue] || Resque.queue_from_class(constantize(klass_name))
  if (config[:just_once] || config['just_once'])
    Resque::Job.destroy(queue, klass_name, *params)
  end
  Resque::Job.create(queue, klass_name, *params)
end

.handle_delayed_itemsObject

Handles queueing delayed items



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/resque/scheduler.rb', line 64

def handle_delayed_items
  item = nil
  begin
    if timestamp = Resque.next_delayed_timestamp
      item = nil
      begin
        handle_shutdown do
          if item = Resque.next_item_for_timestamp(timestamp)
            log "queuing #{item['class']} [delayed]"
            queue = item['queue'] || Resque.queue_from_class(constantize(item['class']))
            Job.create(queue, item['class'], *item['args'])
          end
        end
      # continue processing until there are no more ready items in this timestamp
      end while !item.nil?
    end
  # continue processing until there are no more ready timestamps
  end while !timestamp.nil?
end

.handle_shutdownObject



84
85
86
87
88
# File 'lib/resque/scheduler.rb', line 84

def handle_shutdown
  exit if @shutdown
  yield
  exit if @shutdown
end

.load_schedule!Object

Pulls the schedule from Resque.schedule and loads it into the rufus scheduler instance



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/resque/scheduler.rb', line 47

def load_schedule!
  log! "Schedule empty! Set Resque.schedule" if Resque.schedule.empty?

  Resque.schedule.each do |name, config|
    log! "Scheduling #{name} "
    if !config['cron'].nil? && config['cron'].length > 0
      rufus_scheduler.cron config['cron'] do
        log! "queuing #{config['class']} (#{name})"
        enqueue_from_config(config)
      end
    else
      log! "no cron found for #{config['class']} (#{name}) - skipping"
    end
  end
end

.log(msg) ⇒ Object



132
133
134
135
# File 'lib/resque/scheduler.rb', line 132

def log(msg)
  # add "verbose" logic later
  log!(msg) if verbose
end

.log!(msg) ⇒ Object



128
129
130
# File 'lib/resque/scheduler.rb', line 128

def log!(msg)
  puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} #{msg}" unless mute
end

.poll_sleepObject

Sleeps and returns true



115
116
117
118
119
120
# File 'lib/resque/scheduler.rb', line 115

def poll_sleep
  @sleeping = true
  handle_shutdown { sleep 5 }
  @sleeping = false
  true
end

.register_signal_handlersObject

For all signals, set the shutdown flag and wait for current poll/enqueing to finish (should be almost istant). In the case of sleeping, exit immediately.



39
40
41
42
43
# File 'lib/resque/scheduler.rb', line 39

def register_signal_handlers
  trap("TERM") { shutdown }
  trap("INT") { shutdown }
  trap('QUIT') { shutdown } unless defined? JRUBY_VERSION
end

.rufus_schedulerObject



102
103
104
# File 'lib/resque/scheduler.rb', line 102

def rufus_scheduler
  @rufus_scheduler ||= Rufus::Scheduler.start_new
end

.runObject

Schedule all jobs and continually look for delayed jobs (never returns)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/resque/scheduler.rb', line 19

def run

  # trap signals
  register_signal_handlers

  # Load the schedule into rufus
  load_schedule!

  # Now start the scheduling part of the loop.
  loop do
    handle_delayed_items
    poll_sleep
  end

  # never gets here.
end

.shutdownObject

Sets the shutdown flag, exits if sleeping



123
124
125
126
# File 'lib/resque/scheduler.rb', line 123

def shutdown
  @shutdown = true
  exit if @sleeping
end