Module: Bluth::TimingBelt

Includes:
Familia
Defined in:
lib/bluth/timingbelt.rb

Defined Under Namespace

Modules: Notch

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.lengthObject (readonly)

Returns the value of attribute length.



31
32
33
# File 'lib/bluth/timingbelt.rb', line 31

def length
  @length
end

.notchcacheObject (readonly)

Returns the value of attribute notchcache.



31
32
33
# File 'lib/bluth/timingbelt.rb', line 31

def notchcache
  @notchcache
end

Class Method Details

.add(data, notch = nil) ⇒ Object



92
93
94
95
# File 'lib/bluth/timingbelt.rb', line 92

def add data, notch=nil
  notch ||= Bluth::TimingBelt.notch 1
  notch.add data
end

.collect(mins = length, filter = nil, time = now, &blk) ⇒ Object



56
57
58
59
60
# File 'lib/bluth/timingbelt.rb', line 56

def collect mins=length, filter=nil, time=now, &blk
  ret = []
  each(mins, filter, time) { |notch| ret << blk.call(notch) }
  ret
end

.each(mins = length, filter = nil, time = now, &blk) ⇒ Object

mins: the number of minutes to look ahead.



45
46
47
48
49
50
# File 'lib/bluth/timingbelt.rb', line 45

def each mins=length, filter=nil, time=now, &blk
  mins.times { |idx|
    notch = Bluth::TimingBelt.notch idx, filter, time
    blk.call notch
  }
end

.find(v, mins = length, filter = nil, time = now) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/bluth/timingbelt.rb', line 32

def find v, mins=length, filter=nil, time=now
  raise ArgumentError, "value cannot be nil" if v.nil?
  select(mins, filter, time) do |notch| 
    notch.member?(v)
  end
end

.next_empty_notch(filter = nil, time = now) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/bluth/timingbelt.rb', line 85

def next_empty_notch filter=nil, time=now
  length.times { |min| 
    possible = notch min+1, filter, time  # add 1 so we don't start at 0
    return possible if possible.empty?
  }
  nil
end

.notch(mins = 0, filter = nil, time = now) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bluth/timingbelt.rb', line 67

def notch mins=0, filter=nil, time=now
  cache_key = [now(mins, time).to_i, filter].join(':')
  key = rediskey(stamp(mins, time), filter)
  @notchcache ||= {}
  if @notchcache[cache_key].nil?
    @notchcache[cache_key] ||= Familia::Set.new key, 
        :ttl => 2*60*60, # 2 hours
        :extend => Bluth::TimingBelt::Notch, 
        :db => Bluth::TimingBelt.db
    @notchcache[cache_key].stamp = stamp(mins, time) 
    @notchcache[cache_key].filter = filter
    @notchcache[cache_key].time = now(mins, time)
  end
  @notchcache[cache_key]
end

.now(mins = 0, time = Time.now.utc) ⇒ Object



61
62
63
# File 'lib/bluth/timingbelt.rb', line 61

def now mins=0, time=Time.now.utc
  time + (mins*60)  # time wants it in seconds
end

.pop(minutes = 2, filter = nil, time = now) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bluth/timingbelt.rb', line 96

def pop minutes=2, filter=nil, time=now
  gob = nil
  priority = Bluth::TimingBelt.priority minutes, filter, time
  begin
    gobid, notch = nil, nil
    priority.each { |n| gobid, notch = n.pop, n.name; break unless gobid.nil? }
    unless gobid.nil?
      Familia.ld "FOUND #{gobid} id #{notch}" if Familia.debug?
      gob = Bluth::Gob.from_redis gobid
      raise Bluth::Buster, "No such gob object: #{gobid}" if gob.nil?
      Bluth::Queue.running << gob.jobid
      gob.current_queue = :running
      gob.save
    end
  rescue => ex
    Familia.info ex.message
    Familia.ld ex.backtrace if Familia.debug?
  end
  gob
end

.priority(minutes = 2, filter = nil, time = now) ⇒ Object



82
83
84
# File 'lib/bluth/timingbelt.rb', line 82

def priority minutes=2, filter=nil, time=now
  (0..minutes).to_a.reverse.collect { |min| notch(min*-1, filter, time) }
end

.range(rng, filter = nil, time = now, &blk) ⇒ Object



38
39
40
41
42
43
# File 'lib/bluth/timingbelt.rb', line 38

def range rng, filter=nil, time=now, &blk
  rng.to_a.each { |idx|
    notch = Bluth::TimingBelt.notch idx, filter, time
    blk.call notch
  }
end

.select(mins = length, filter = nil, time = now, &blk) ⇒ Object



51
52
53
54
55
# File 'lib/bluth/timingbelt.rb', line 51

def select mins=length, filter=nil, time=now, &blk
  ret = []
  each(mins, filter, time) { |notch| ret << notch if blk.call(notch) }
  ret
end

.stamp(mins = 0, time = now) ⇒ Object



64
65
66
# File 'lib/bluth/timingbelt.rb', line 64

def stamp mins=0, time=now
  (time + (mins*60)).strftime('%H:%M')
end