Class: PerfectSched::RDBBackend
- Defined in:
- lib/perfectsched/backend/rdb.rb
Constant Summary collapse
- MAX_SELECT_ROW =
32
Instance Method Summary collapse
- #acquire(timeout, now = Time.now.to_i) ⇒ Object
- #add_checked(id, cron, delay, data, next_time, timeout) ⇒ Object
- #delete(id) ⇒ Object
- #finish(token, next_time, timeout) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(uri, table) ⇒ RDBBackend
constructor
A new instance of RDBBackend.
- #list(&block) ⇒ Object
- #modify_checked(id, cron, delay, data) ⇒ Object
- #modify_data_checked(id, data) ⇒ Object
- #modify_sched_checked(id, cron, delay) ⇒ Object
Methods inherited from Backend
#add, #close, #modify, #modify_data, #modify_sched
Constructor Details
#initialize(uri, table) ⇒ RDBBackend
Returns a new instance of RDBBackend.
6 7 8 9 10 11 12 13 |
# File 'lib/perfectsched/backend/rdb.rb', line 6 def initialize(uri, table) super() require 'sequel' @uri = uri @table = table @db = Sequel.connect(@uri) init_db(@uri.split(':',2)[0]) end |
Instance Method Details
#acquire(timeout, now = Time.now.to_i) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/perfectsched/backend/rdb.rb', line 63 def acquire(timeout, now=Time.now.to_i) connect { while true rows = 0 @db.fetch("SELECT id, timeout, next_time, cron, delay, data FROM `#{@table}` WHERE timeout <= ? ORDER BY timeout ASC LIMIT #{MAX_SELECT_ROW};", now) {|row| n = @db["UPDATE `#{@table}` SET timeout=? WHERE id=? AND timeout=?;", timeout, row[:id], row[:timeout]].update salt = timeout if n > 0 return [row[:id],salt], Task.new(row[:id], row[:next_time], row[:cron], row[:delay], row[:data]) end rows += 1 } if rows < MAX_SELECT_ROW return nil end end } end |
#add_checked(id, cron, delay, data, next_time, timeout) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/perfectsched/backend/rdb.rb', line 92 def add_checked(id, cron, delay, data, next_time, timeout) connect { begin n = @db["INSERT INTO `#{@table}` (id, timeout, next_time, cron, delay, data) VALUES (?, ?, ?, ?, ?, ?);", id, timeout, next_time, cron, delay, data].insert return true rescue Sequel::DatabaseError return nil end } end |
#delete(id) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/perfectsched/backend/rdb.rb', line 103 def delete(id) connect { n = @db["DELETE FROM `#{@table}` WHERE id=?;", id].delete return n > 0 } end |
#finish(token, next_time, timeout) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/perfectsched/backend/rdb.rb', line 84 def finish(token, next_time, timeout) connect { id, salt = *token n = @db["UPDATE `#{@table}` SET timeout=?, next_time=? WHERE id=? AND timeout=?;", timeout, next_time, id, salt].update return n > 0 } end |
#get(id) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/perfectsched/backend/rdb.rb', line 110 def get(id) connect { @db.fetch("SELECT id, timeout, next_time, cron, delay, data FROM `#{@table}` WHERE id=?;", id) {|row| return row[:cron], row[:delay], row[:data] } return nil } end |
#list(&block) ⇒ Object
55 56 57 58 59 |
# File 'lib/perfectsched/backend/rdb.rb', line 55 def list(&block) @db.fetch("SELECT id, timeout, next_time, cron, delay, data FROM `#{@table}` ORDER BY timeout ASC") {|row| yield row[:id], row[:cron], row[:delay], row[:data], row[:next_time], row[:timeout] } end |
#modify_checked(id, cron, delay, data) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/perfectsched/backend/rdb.rb', line 119 def modify_checked(id, cron, delay, data) connect { n = @db["UPDATE `#{@table}` SET cron=?, delay=?, data=? WHERE id=?;", cron, delay, data, id].update return n > 0 } end |
#modify_data_checked(id, data) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/perfectsched/backend/rdb.rb', line 133 def modify_data_checked(id, data) connect { n = @db["UPDATE `#{@table}` SET data=? WHERE id=?;", data, id].update return n > 0 } end |
#modify_sched_checked(id, cron, delay) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/perfectsched/backend/rdb.rb', line 126 def modify_sched_checked(id, cron, delay) connect { n = @db["UPDATE `#{@table}` SET cron=?, delay=? WHERE id=?;", cron, delay, id].update return n > 0 } end |