Class: Sidetiq::Clock

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/sidetiq/clock.rb,
ext/sidetiq_ext/sidetiq_ext.c

Overview

Public: The Sidetiq clock.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClock

:nodoc:



24
25
26
27
# File 'lib/sidetiq/clock.rb', line 24

def initialize # :nodoc:
  super
  @schedules = {}
end

Instance Attribute Details

#schedulesObject (readonly)

Internal: Returns a hash of Sidetiq::Schedule instances.



15
16
17
# File 'lib/sidetiq/clock.rb', line 15

def schedules
  @schedules
end

#threadObject (readonly)

Internal: Returns the clock thread.



18
19
20
# File 'lib/sidetiq/clock.rb', line 18

def thread
  @thread
end

Class Method Details

.method_missing(meth, *args, &block) ⇒ Object

:nodoc:



20
21
22
# File 'lib/sidetiq/clock.rb', line 20

def self.method_missing(meth, *args, &block) # :nodoc:
  instance.__send__(meth, *args, &block)
end

Instance Method Details

#gettimeObject

Public: Returns the current time used by the clock.

Sidetiq::Clock uses ‘clock_gettime()` on UNIX systems and `mach_absolute_time()` on Mac OS X.

Examples

gettime
# => 2013-02-04 12:00:45 +0000

Returns a Time instance.



79
80
81
# File 'lib/sidetiq/clock.rb', line 79

def gettime
  Sidetiq.config.utc ? clock_gettime.utc : clock_gettime
end

#schedule_for(worker) ⇒ Object

Public: Get the schedule for ‘worker`.

worker - A Sidekiq::Worker class

Examples

schedule_for(MyWorker)
# => Sidetiq::Schedule

Returns a Sidetiq::Schedule instances.



39
40
41
# File 'lib/sidetiq/clock.rb', line 39

def schedule_for(worker)
  schedules[worker] ||= Sidetiq::Schedule.new
end

#start!Object

Public: Starts the clock unless it is already running.

Examples

start!
# => Thread

Returns the Thread instance of the clock thread.



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

def start!
  return if ticking?

  Sidetiq.logger.info "Sidetiq::Clock start"

  @thread = Thread.start { clock { tick } }
  @thread.abort_on_exception = true
  @thread.priority = Sidetiq.config.priority
  @thread
end

#stop!Object

Public: Stops the clock if it is running.

Examples

stop!
# => nil

Returns nil.



110
111
112
113
114
115
# File 'lib/sidetiq/clock.rb', line 110

def stop!
  if ticking?
    @thread.kill
    Sidetiq.logger.info "Sidetiq::Clock stop"
  end
end

#tickObject

Public: Issue a single clock tick.

Examples

tick
# => Hash of Sidetiq::Schedule objects

Returns a hash of Sidetiq::Schedule instances.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sidetiq/clock.rb', line 51

def tick
  tick = gettime
  synchronize do
    schedules.each do |worker, sched|
      synchronize_clockworks(worker) do |redis|
        if sched.backfill? && (last = worker.last_scheduled_occurrence) > 0
          last = Sidetiq.config.utc ? Time.at(last).utc : Time.at(last)
          sched.occurrences_between(last + 1, tick).each do |past_t|
            enqueue(worker, past_t, redis)
          end
        end
        enqueue(worker, sched.next_occurrence(tick), redis)
      end if sched.schedule_next?(tick)
    end
  end
end

#ticking?Boolean

Public: Returns the status of the clock.

Examples

ticking?
# => false

start!
ticking?
# => true

Returns true or false.

Returns:

  • (Boolean)


129
130
131
# File 'lib/sidetiq/clock.rb', line 129

def ticking?
  @thread && @thread.alive?
end