Class: GemeraldBeanstalk::Tube
- Inherits:
-
Object
- Object
- GemeraldBeanstalk::Tube
- Defined in:
- lib/gemerald_beanstalk/tube.rb
Instance Attribute Summary collapse
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#reservations ⇒ Object
readonly
Returns the value of attribute reservations.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #adjust_stats_key(key, adjustment = 1) ⇒ Object
- #cancel_reservation(connection) ⇒ Object
- #deactivate ⇒ Object
- #deactivated? ⇒ Boolean
- #delete(job) ⇒ Object
- #ignore ⇒ Object
-
#initialize(name) ⇒ Tube
constructor
A new instance of Tube.
- #next_job(state = :ready, action = :reserve) ⇒ Object
- #next_reservation ⇒ Object
- #pause(delay, *args) ⇒ Object
- #paused? ⇒ Boolean
- #put(job) ⇒ Object
- #ready? ⇒ Boolean
- #reserve(connection) ⇒ Object
- #should_deactivate? ⇒ Boolean
- #stats ⇒ Object
- #stop_use ⇒ Object
- #use ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(name) ⇒ Tube
Returns a new instance of Tube.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gemerald_beanstalk/tube.rb', line 45 def initialize(name) @name = name @jobs = GemeraldBeanstalk::Jobs.new @reservations = [] @state = :ready @stats = ThreadSafe::Cache.new @stats[:'cmd-delete'] = 0 @stats[:'cmd-pause-tube'] = 0 @stats[:'using'] = 0 @stats[:'waiting'] = 0 @stats[:'watching'] = 0 end |
Instance Attribute Details
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
3 4 5 |
# File 'lib/gemerald_beanstalk/tube.rb', line 3 def jobs @jobs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/gemerald_beanstalk/tube.rb', line 3 def name @name end |
#reservations ⇒ Object (readonly)
Returns the value of attribute reservations.
3 4 5 |
# File 'lib/gemerald_beanstalk/tube.rb', line 3 def reservations @reservations end |
Instance Method Details
#active? ⇒ Boolean
6 7 8 |
# File 'lib/gemerald_beanstalk/tube.rb', line 6 def active? return !self.deactivated? end |
#adjust_stats_key(key, adjustment = 1) ⇒ Object
11 12 13 |
# File 'lib/gemerald_beanstalk/tube.rb', line 11 def adjust_stats_key(key, adjustment = 1) @stats[key] = [@stats[key] + adjustment, 0].max end |
#cancel_reservation(connection) ⇒ Object
16 17 18 |
# File 'lib/gemerald_beanstalk/tube.rb', line 16 def cancel_reservation(connection) return @reservations.delete(connection) end |
#deactivate ⇒ Object
21 22 23 24 25 |
# File 'lib/gemerald_beanstalk/tube.rb', line 21 def deactivate return false if @state == :deactivated || self.name == 'default' @state = :deactivated return true end |
#deactivated? ⇒ Boolean
28 29 30 |
# File 'lib/gemerald_beanstalk/tube.rb', line 28 def deactivated? return @state == :deactivated end |
#delete(job) ⇒ Object
33 34 35 36 |
# File 'lib/gemerald_beanstalk/tube.rb', line 33 def delete(job) adjust_stats_key(:'cmd-delete') return @jobs.delete(job) end |
#ignore ⇒ Object
39 40 41 42 |
# File 'lib/gemerald_beanstalk/tube.rb', line 39 def ignore adjust_stats_key(:'watching', -1) deactivate if should_deactivate? end |
#next_job(state = :ready, action = :reserve) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gemerald_beanstalk/tube.rb', line 59 def next_job(state = :ready, action = :reserve) return nil if paused? && action == :reserve best_candidate = nil @jobs.each do |candidate| next if candidate.state != state best_candidate = candidate if best_candidate.nil? || candidate < best_candidate end return best_candidate end |
#next_reservation ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gemerald_beanstalk/tube.rb', line 72 def next_reservation reservation = nil while ready? && @reservations.any? && reservation.nil? reservation = @reservations[0] break if reservation.waiting? @reservations.shift reservation = nil end return reservation end |
#pause(delay, *args) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/gemerald_beanstalk/tube.rb', line 85 def pause(delay, *args) return false unless ready? @state = :paused adjust_stats_key(:'cmd-pause-tube') @pause_delay = delay.to_i @paused_at = Time.now.to_f @resume_at = @paused_at + @pause_delay return true end |
#paused? ⇒ Boolean
96 97 98 99 100 101 102 103 |
# File 'lib/gemerald_beanstalk/tube.rb', line 96 def paused? return false unless @state == :paused return true if @resume_at > Time.now.to_f @state = :ready @pause_delay = @paused_at = @resume_at = nil return false end |
#put(job) ⇒ Object
106 107 108 |
# File 'lib/gemerald_beanstalk/tube.rb', line 106 def put(job) @jobs.enqueue(job) end |
#ready? ⇒ Boolean
111 112 113 |
# File 'lib/gemerald_beanstalk/tube.rb', line 111 def ready? return @state == :ready end |
#reserve(connection) ⇒ Object
116 117 118 |
# File 'lib/gemerald_beanstalk/tube.rb', line 116 def reserve(connection) @reservations << connection end |
#should_deactivate? ⇒ Boolean
121 122 123 |
# File 'lib/gemerald_beanstalk/tube.rb', line 121 def should_deactivate? return @jobs.length == 0 && @stats[:'watching'] == 0 && @stats[:'using'] == 0 end |
#stats ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/gemerald_beanstalk/tube.rb', line 126 def stats job_stats = @jobs.counts_by_state # Need to call paused in advance to update state pause_time_left = paused? ? (@resume_at - Time.now.to_f).to_i : 0 return { 'name' => @name, 'current-jobs-urgent' => job_stats['current-jobs-urgent'], 'current-jobs-ready' => job_stats['current-jobs-ready'], 'current-jobs-reserved' => job_stats['current-jobs-reserved'], 'current-jobs-delayed' => job_stats['current-jobs-delayed'], 'current-jobs-buried' => job_stats['current-jobs-buried'], 'total-jobs' => @jobs.total_jobs, 'current-using' => @stats[:'using'], 'current-watching' => @stats[:'watching'], 'current-waiting' => @reservations.length, 'cmd-delete' => @stats[:'cmd-delete'], 'cmd-pause-tube' => @stats[:'cmd-pause-tube'], 'pause' => @pause_delay || 0, 'pause-time-left' => pause_time_left, } end |
#stop_use ⇒ Object
149 150 151 152 |
# File 'lib/gemerald_beanstalk/tube.rb', line 149 def stop_use adjust_stats_key(:'using', -1) deactivate if should_deactivate? end |
#use ⇒ Object
160 161 162 |
# File 'lib/gemerald_beanstalk/tube.rb', line 160 def use adjust_stats_key(:'using') end |
#watch ⇒ Object
155 156 157 |
# File 'lib/gemerald_beanstalk/tube.rb', line 155 def watch adjust_stats_key(:'watching') end |