Class: Kward::Workers::QueueStore
- Inherits:
-
Object
- Object
- Kward::Workers::QueueStore
- Defined in:
- lib/kward/workers/queue_store.rb
Overview
JSON-backed queue store for session-backed worker jobs.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #archive(id) ⇒ Object
- #enqueue(title:, session_path:, workspace_root:, id: nil) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(path: File.join(ConfigFiles.config_dir, "worker_queue.json")) ⇒ QueueStore
constructor
A new instance of QueueStore.
- #list(include_archived: false) ⇒ Object
- #next_queued ⇒ Object
- #update_status(id, status, **values) ⇒ Object
- #upsert(job) ⇒ Object
Constructor Details
#initialize(path: File.join(ConfigFiles.config_dir, "worker_queue.json")) ⇒ QueueStore
Returns a new instance of QueueStore.
10 11 12 13 |
# File 'lib/kward/workers/queue_store.rb', line 10 def initialize(path: File.join(ConfigFiles.config_dir, "worker_queue.json")) @path = path @mutex = Mutex.new end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
15 16 17 |
# File 'lib/kward/workers/queue_store.rb', line 15 def path @path end |
Instance Method Details
#archive(id) ⇒ Object
64 65 66 |
# File 'lib/kward/workers/queue_store.rb', line 64 def archive(id) update_status(id, "archived") end |
#enqueue(title:, session_path:, workspace_root:, id: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/kward/workers/queue_store.rb', line 17 def enqueue(title:, session_path:, workspace_root:, id: nil) job = Job.new( id: id || SecureRandom.hex(4), title: title, session_path: session_path, workspace_root: workspace_root, position: next_position ) upsert(job) job end |
#find(id) ⇒ Object
45 46 47 |
# File 'lib/kward/workers/queue_store.rb', line 45 def find(id) read_records.find { |record| record["id"] == id.to_s } end |
#list(include_archived: false) ⇒ Object
39 40 41 42 43 |
# File 'lib/kward/workers/queue_store.rb', line 39 def list(include_archived: false) records = read_records records = records.reject { |record| record["status"] == "archived" } unless include_archived sorted(records) end |
#next_queued ⇒ Object
68 69 70 |
# File 'lib/kward/workers/queue_store.rb', line 68 def next_queued list.find { |record| record["status"] == "queued" } end |
#update_status(id, status, **values) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kward/workers/queue_store.rb', line 49 def update_status(id, status, **values) record = nil update_records do |records| index = records.index { |item| item["id"] == id.to_s } raise ArgumentError, "Unknown worker job: #{id}" unless index job = Job.from_h(records[index]) job.update_status(status, **values) record = job.to_h records[index] = record normalize_positions(records) end record end |
#upsert(job) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/kward/workers/queue_store.rb', line 29 def upsert(job) record = job.respond_to?(:to_h) ? job.to_h : job.to_h update_records do |records| index = records.index { |item| item["id"] == record["id"] } index ? records[index] = record : records << record normalize_positions(records) end record end |