class Procrastinator::TaskStore::SimpleCommaStore

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.

@author Robin Miller

Constants

DEFAULT_FILE

Default filename

EXT

CSV file extension

HEADERS

Ordered list of CSV column headers

READ_CONVERTER

CSV Converter lambda

@see CSV

Attributes

path[R]

Public Class Methods

new(file_path = DEFAULT_FILE) click to toggle source
# File lib/procrastinator/task_store/simple_comma_store.rb, line 47
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.expand_path

   freeze
end

Public Instance Methods

create(queue:, run_at:, expire_at: nil, data: '', initial_run_at: nil) click to toggle source

Saves a task to the CSV file.

@param queue [String] queue name @param run_at [Time, nil] time to run the task at @param initial_run_at [Time, nil] first time to run the task at. Defaults to run_at. @param expire_at [Time, nil] time to expire the task

# File lib/procrastinator/task_store/simple_comma_store.rb, line 81
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) click to toggle source

Removes an existing task from the CSV file.

@param id [Integer] task ID number

# File lib/procrastinator/task_store/simple_comma_store.rb, line 118
def delete(id)
   CSVFileTransaction.new(@path).write do |existing_data|
      generate(existing_data.reject { |task| task[:id] == id })
   end
end
generate(data) click to toggle source

Generates a CSV string from the given data.

@param data [Array] list of data to convert into CSV @return [String] Generated CSV string

# File lib/procrastinator/task_store/simple_comma_store.rb, line 128
def generate(data)
   lines = data.collect do |d|
      Task::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 = {}) click to toggle source

Parses the CSV file for data matching the given filter, or all if no filter provided.

@param filter [Hash] Specified attributes to match. @return [Array<Hash>]

# File lib/procrastinator/task_store/simple_comma_store.rb, line 65
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) click to toggle source

Updates an existing task in the CSV file.

@param id [Integer] task ID number @param data [Hash] new data to save

# File lib/procrastinator/task_store/simple_comma_store.rb, line 103
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