Skip to content
Kward Search API index

Class: Kward::Workers::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/workers/store.rb

Overview

JSON-backed metadata store for worker records.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: File.join(ConfigFiles.config_dir, "workers.json")) ⇒ Store

Returns a new instance of Store.



9
10
11
12
# File 'lib/kward/workers/store.rb', line 9

def initialize(path: File.join(ConfigFiles.config_dir, "workers.json"))
  @path = path
  @mutex = Mutex.new
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/kward/workers/store.rb', line 14

def path
  @path
end

Instance Method Details

#archive(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kward/workers/store.rb', line 35

def archive(id)
  record = nil
  update_records do |records|
    index = records.index { |item| item["id"] == id.to_s }
    raise ArgumentError, "Unknown worker: #{id}" unless index

    record = records[index].merge("status" => "archived")
    records[index] = record
  end
  record
end

#find(id) ⇒ Object



31
32
33
# File 'lib/kward/workers/store.rb', line 31

def find(id)
  read_records.find { |record| record["id"] == id.to_s }
end

#list(include_archived: false) ⇒ Object



25
26
27
28
29
# File 'lib/kward/workers/store.rb', line 25

def list(include_archived: false)
  records = read_records
  records = records.reject { |record| record["status"] == "archived" } unless include_archived
  records.sort_by { |record| record["created_at"].to_s }
end

#upsert(worker) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/kward/workers/store.rb', line 16

def upsert(worker)
  record = worker.respond_to?(:to_h) ? worker.to_h : worker.to_h
  update_records do |records|
    index = records.index { |item| item["id"] == record["id"] }
    index ? records[index] = record : records << record
  end
  record
end