Class: Procrastinator::TaskStore::SimpleCommaStore
- Inherits:
-
Object
- Object
- Procrastinator::TaskStore::SimpleCommaStore
- Defined in:
- lib/procrastinator/task_store/simple_comma_store.rb
Overview
Simple Task I/O adapter that writes task information (ie. TaskMetaData attributes) to a CSV file.
SimpleCommaStore is not designed for efficiency or large loads (10,000+ tasks). For critical production environments, it is strongly recommended to use a more robust storage mechanism like a proper database.
Defined Under Namespace
Classes: CSVFileTransaction
Constant Summary collapse
- HEADERS =
Ordered list of CSV column headers
[:id, :queue, :run_at, :initial_run_at, :expire_at, :attempts, :last_fail_at, :last_error, :data].freeze
- TIME_FIELDS =
Columns that store time information
[:run_at, :initial_run_at, :expire_at, :last_fail_at].freeze
- EXT =
CSV file extension
'csv'- DEFAULT_FILE =
Default filename
Pathname.new("procrastinator-tasks.#{ EXT }").freeze
- READ_CONVERTER =
CSV Converter lambda
proc do |value, field_info| if field_info.header == :data value elsif TIME_FIELDS.include? field_info.header value.empty? ? nil : Time.parse(value) else begin Integer(value) rescue ArgumentError value end end end
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#create(queue:, run_at:, expire_at: nil, data: '', initial_run_at: nil) ⇒ Object
Saves a task to the CSV file.
-
#delete(id) ⇒ Object
Removes an existing task from the CSV file.
-
#generate(data) ⇒ String
Generates a CSV string from the given data.
-
#initialize(file_path = DEFAULT_FILE) ⇒ SimpleCommaStore
constructor
A new instance of SimpleCommaStore.
-
#read(filter = {}) ⇒ Array<Hash>
Parses the CSV file for data matching the given filter, or all if no filter provided.
-
#update(id, data) ⇒ Object
Updates an existing task in the CSV file.
Constructor Details
#initialize(file_path = DEFAULT_FILE) ⇒ SimpleCommaStore
Returns a new instance of SimpleCommaStore.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 50 def initialize(file_path = DEFAULT_FILE) @path = Pathname.new(file_path) if @path.directory? || @path.to_s.end_with?('/') @path /= DEFAULT_FILE elsif @path.extname.empty? @path = @path.dirname / "#{ @path.basename }.csv" end @path = @path. freeze end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
48 49 50 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 48 def path @path end |
Instance Method Details
#create(queue:, run_at:, expire_at: nil, data: '', initial_run_at: nil) ⇒ Object
Saves a task to the CSV file.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 84 def create(queue:, run_at:, expire_at: nil, data: '', initial_run_at: nil) CSVFileTransaction.new(@path).write do |tasks| max_id = tasks.collect { |task| task[:id] }.max || 0 new_data = { id: max_id + 1, queue: queue, run_at: run_at, initial_run_at: initial_run_at || run_at, expire_at: expire_at, attempts: 0, data: data } generate(tasks + [new_data]) end end |
#delete(id) ⇒ Object
Removes an existing task from the CSV file.
121 122 123 124 125 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 121 def delete(id) CSVFileTransaction.new(@path).write do |existing_data| generate(existing_data.reject { |task| task[:id] == id }) end end |
#generate(data) ⇒ String
Generates a CSV string from the given data.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 131 def generate(data) lines = data.collect do |d| TIME_FIELDS.each do |field| d[field] = d[field]&.iso8601 end CSV.generate_line(d, headers: HEADERS, force_quotes: true).strip end lines.unshift(HEADERS.join(',')) lines.join("\n") << "\n" end |
#read(filter = {}) ⇒ Array<Hash>
Parses the CSV file for data matching the given filter, or all if no filter provided.
68 69 70 71 72 73 74 75 76 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 68 def read(filter = {}) CSVFileTransaction.new(@path).read do |existing_data| existing_data.select do |row| filter.keys.all? do |key| row[key] == filter[key] end end end end |
#update(id, data) ⇒ Object
Updates an existing task in the CSV file.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/procrastinator/task_store/simple_comma_store.rb', line 106 def update(id, data) CSVFileTransaction.new(@path).write do |tasks| task_data = tasks.find do |task| task[:id] == id end task_data&.merge!(data) generate(tasks) end end |