Class: RocketSMS::Scheduler
- Inherits:
-
Object
- Object
- RocketSMS::Scheduler
- Includes:
- Singleton
- Defined in:
- lib/rocket_sms/scheduler.rb
Instance Attribute Summary collapse
-
#log_location ⇒ Object
Returns the value of attribute log_location.
-
#redis_url ⇒ Object
Returns the value of attribute redis_url.
Instance Method Summary collapse
- #configure ⇒ Object
- #detect ⇒ Object
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
- #log(msg, level = 'info') ⇒ Object
- #logger ⇒ Object
- #pick_transceiver ⇒ Object
- #process_payload(msg_payload) ⇒ Object
- #queues ⇒ Object
- #redis ⇒ Object
- #retry_message(message) ⇒ Object
- #schedule(message, did_payload) ⇒ Object
- #set_throughput ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #stop(signal = nil) ⇒ Object
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
8 9 10 11 12 13 14 15 |
# File 'lib/rocket_sms/scheduler.rb', line 8 def initialize @redis_url, @log_location = nil, nil @active = true @fast = false @dids = {} @transceivers = {} @throughput = 0 end |
Instance Attribute Details
#log_location ⇒ Object
Returns the value of attribute log_location.
6 7 8 |
# File 'lib/rocket_sms/scheduler.rb', line 6 def log_location @log_location end |
#redis_url ⇒ Object
Returns the value of attribute redis_url.
6 7 8 |
# File 'lib/rocket_sms/scheduler.rb', line 6 def redis_url @redis_url end |
Instance Method Details
#configure ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rocket_sms/scheduler.rb', line 67 def configure return unless @active @configurator = EM::Timer.new(1){ configure } redis.keys("gateway:transceivers:*") do |keys| if keys tids = keys.map{ |key| key.split(':')[2] }.uniq @transceivers.keys.each{ |k| @transceivers.delete(k) unless tids.include?(k) } tids.each do |tid| redis.hget("gateway:transceivers:#{tid}","status") do |resp| if resp == 'online' redis.hget("gateway:transceivers:#{tid}", "throughput") do |payload| if payload throughput = payload.to_f log "Adding Transceiver #{tid}" if !@transceivers[tid] @transceivers[tid] = throughput set_throughput elsif @transceivers[tid] log "Removing Transceiver #{tid}" @transceivers.delete(tid) set_throughput end end else if @transceivers[tid] log "Removing Transceiver #{tid}" @transceivers.delete(tid) set_throughput end end end end else @transceivers = {} @throughput = 0 end end end |
#detect ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rocket_sms/scheduler.rb', line 109 def detect return unless @active interval = @fast ? 0.001 : 1 @detector = EM::Timer.new(interval){ detect } redis.multi redis.zrange(queues[:mt][:pending], 0, 0, "WITHSCORES") redis.zremrangebyrank(queues[:mt][:pending], 0, 0) redis.exec do |response| if response (payload, score) = response[0] if payload and score @fast = true now = Time.now.to_i if score.to_i <= now process_payload(payload) else redis.zadd(queues[:mt][:pending], score, payload) @fast = false end else @fast = false end end end end |
#log(msg, level = 'info') ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rocket_sms/scheduler.rb', line 25 def log(msg, level = 'info') if EM.reactor_running? EM.defer{ logger.send(level, msg) } else logger.send(level, msg) end end |
#logger ⇒ Object
21 22 23 |
# File 'lib/rocket_sms/scheduler.rb', line 21 def logger @logger ||= Logger.new(@log_location) end |
#pick_transceiver ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/rocket_sms/scheduler.rb', line 189 def pick_transceiver tids = [] @transceivers.each do |k,v| weight = (v.to_f/@throughput*100).to_i weight.times{ tids << k } end tids.flatten! tids.sample end |
#process_payload(msg_payload) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rocket_sms/scheduler.rb', line 135 def process_payload(msg_payload) begin = Message.from_json(msg_payload) if .pass > 5 log "Message #{.id} has exceeded maximum retries. Send it to Failed queue." redis.lpush(queues[:mt][:failure], .to_json) else did_number = .sender redis.get("gateway:dids:#{did_number}") do |payload| if payload log "Scheduling Message #{.id} to be sent through DID #{did_number}" schedule(, payload) else log "The DID #{did_number} for message #{.id} is not configured. Retrying." () end end end rescue log 'Invalid Message.' redis.lpush(queues[:mt][:failure]) end end |
#queues ⇒ Object
33 34 35 |
# File 'lib/rocket_sms/scheduler.rb', line 33 def queues RocketSMS.queues end |
#redis ⇒ Object
17 18 19 |
# File 'lib/rocket_sms/scheduler.rb', line 17 def redis @redis ||= EM::Hiredis.connect(@redis_url) end |
#retry_message(message) ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rocket_sms/scheduler.rb', line 199 def () if .pass > 5 log "Message #{.id} has exceeded maximum retries. Send it to Failed queue." redis.lpush(queues[:mt][:failure], .to_json) else .add_pass score = Time.now.to_i + 15 redis.zadd(queues[:mt][:pending], score, .to_json) end end |
#schedule(message, did_payload) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rocket_sms/scheduler.rb', line 159 def schedule(, did_payload) if @active if @transceivers.keys.empty? or @throughput == 0 () else did = Did.from_json(did_payload) if !@dids[did.number] @dids[did.number] = {} @dids[did.number][:last_send] = Time.now.to_f + 1 end interval = ((did.throughput.to_f)**-1)*1.1 last_send = @dids[did.number][:last_send] if Time.now.to_f - last_send > interval base_time = Time.now.to_f + 1 else base_time = last_send end .send_at = base_time + interval .expires_at = .send_at + 50*interval @dids[did.number][:last_send] = .send_at score = (.send_at * 1000).to_i transceiver_id = pick_transceiver redis.zadd("gateway:transceivers:#{transceiver_id}:dispatch", score, .to_json) end else score = .send_at.to_i + 15 redis.zadd(queues[:mt][:pending], score, .to_json) end end |
#set_throughput ⇒ Object
105 106 107 |
# File 'lib/rocket_sms/scheduler.rb', line 105 def set_throughput @throughput = @transceivers.keys.map{ |tid| @transceivers[tid] }.reduce(&:+).to_f end |
#shutdown ⇒ Object
62 63 64 65 |
# File 'lib/rocket_sms/scheduler.rb', line 62 def shutdown log "Scheduler DOWN." EM.stop end |
#start ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rocket_sms/scheduler.rb', line 37 def start EM.threadpool_size = 128 EM.set_max_timers(100_000) EM.run do log "Starting Scheduler" configure detect # Trap exit-related signals Signal.trap("INT") { |signal| stop(signal) } Signal.trap("TERM") { |signal| stop(signal) } end end |
#stop(signal = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rocket_sms/scheduler.rb', line 50 def stop(signal = nil) if @kill log "Forcing Exit. Check your data for losses." shutdown else log "Stopping. Waiting 5 seconds for pending operations to finish." @kill = true @active = false EM::Timer.new(5){ shutdown } end end |