Class: QueueLite::Queue
- Inherits:
-
Object
- Object
- QueueLite::Queue
- Defined in:
- lib/queue_lite/queue.rb
Constant Summary collapse
- READY_STATUS =
"ready"
- LOCKED_STATUS =
"locked"
- FAILED_STATUS =
"failed"
- Task =
Data.define(:id, :data) do def initialize(data: nil, **) super end end
Class Method Summary collapse
Instance Method Summary collapse
- #done(id) ⇒ Object
- #failed(id) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(db) ⇒ Queue
constructor
A new instance of Queue.
- #pop ⇒ Object
- #prepare ⇒ Object
- #put(data) ⇒ Object
Constructor Details
#initialize(db) ⇒ Queue
Returns a new instance of Queue.
25 26 27 |
# File 'lib/queue_lite/queue.rb', line 25 def initialize(db) @db = db end |
Class Method Details
.build(connection_string) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/queue_lite/queue.rb', line 17 def self.build(connection_string) db = SQLite3::Database.new(connection_string) new(db).tap do |instance| instance.prepare end end |
Instance Method Details
#done(id) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/queue_lite/queue.rb', line 68 def done(id) row = db.get_first_row(<<~SQL, [LOCKED_STATUS, id]) UPDATE queue SET status = ? WHERE id = ? RETURNING id, data SQL Task.new(*row) end |
#failed(id) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/queue_lite/queue.rb', line 79 def failed(id) row = db.get_first_row(<<~SQL, [FAILED_STATUS, id]) UPDATE queue SET status = ? WHERE id = ? RETURNING id, data SQL Task.new(*row) end |
#get(id) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/queue_lite/queue.rb', line 90 def get(id) row = db.get_first_row(<<~SQL, [id]) SELECT id, data FROM queue WHERE id = ? LIMIT 1 SQL Task.new(*row) end |
#pop ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/queue_lite/queue.rb', line 51 def pop row = db.get_first_row(<<~SQL, [LOCKED_STATUS, READY_STATUS]) UPDATE queue SET status = ? WHERE rowid = (SELECT rowid FROM queue WHERE status = ? ORDER BY id LIMIT 1) RETURNING id, data SQL return if row.nil? Task.new(*row) end |
#prepare ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/queue_lite/queue.rb', line 29 def prepare db.execute("PRAGMA journal_mode = 'WAL';") db.execute(<<~SQL) CREATE TABLE IF NOT EXISTS queue ( id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT, status TEXT ) SQL end |
#put(data) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/queue_lite/queue.rb', line 42 def put(data) row = db.get_first_row(<<~SQL, [data, READY_STATUS]) INSERT INTO queue(data, status) VALUES(?, ?) RETURNING id, data SQL Task.new(*row) end |