Class: Fractor::QueuePersister::YAMLPersister

Inherits:
Base
  • Object
show all
Defined in:
lib/fractor/queue_persister.rb

Overview

YAML file persister. Stores queue state as a YAML document.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ YAMLPersister

Initialize a YAML persister.

Parameters:

  • path (String)

    Path to the YAML file



130
131
132
# File 'lib/fractor/queue_persister.rb', line 130

def initialize(path)
  @path = path
end

Instance Method Details

#clearBoolean

Clear the YAML file.

Returns:

  • (Boolean)

    true if cleared successfully



171
172
173
174
175
176
177
# File 'lib/fractor/queue_persister.rb', line 171

def clear
  File.delete(@path) if File.exist?(@path)
  true
rescue StandardError => e
  warn "Failed to clear #{@path}: #{e.message}"
  false
end

#loadArray<Hash>?

Load work items from YAML file.

Returns:

  • (Array<Hash>, nil)

    Serialized work items, or nil if file doesn't exist



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fractor/queue_persister.rb', line 154

def load
  return nil unless File.exist?(@path)

  yaml = File.read(@path)
  return nil if yaml.strip.empty?

  YAML.safe_load(yaml,
                 permitted_classes: [Symbol, Hash, String, Integer, Float, TrueClass,
                                     FalseClass, NilClass])
rescue StandardError => e
  warn "Failed to load from #{@path}: #{e.message}"
  nil
end

#save(items) ⇒ Boolean

Save work items to YAML file.

Parameters:

Returns:

  • (Boolean)

    true if saved successfully



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fractor/queue_persister.rb', line 138

def save(items)
  ensure_directory_exists

  serialized = items.map { |work| serialize_work(work) }
  yaml = YAML.dump(serialized)

  File.write(@path, yaml)
  true
rescue StandardError => e
  warn "Failed to save to #{@path}: #{e.message}"
  false
end