Class: Fractor::QueuePersister::MarshalPersister

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

Overview

Marshal file persister. Uses Ruby's Marshal for binary serialization. Note: Marshal is not secure and should not be used with untrusted data.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MarshalPersister

Initialize a Marshal persister.

Parameters:

  • path (String)

    Path to the Marshal file



197
198
199
# File 'lib/fractor/queue_persister.rb', line 197

def initialize(path)
  @path = path
end

Instance Method Details

#clearBoolean

Clear the Marshal file.

Returns:

  • (Boolean)

    true if cleared successfully



234
235
236
237
238
239
240
# File 'lib/fractor/queue_persister.rb', line 234

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 using Marshal.

Returns:

  • (Array<Hash>, nil)

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



221
222
223
224
225
226
227
228
229
# File 'lib/fractor/queue_persister.rb', line 221

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

  data = File.binread(@path)
  Marshal.load(data)
rescue StandardError => e
  warn "Failed to load from #{@path}: #{e.message}"
  nil
end

#save(items) ⇒ Boolean

Save work items using Marshal.

Parameters:

Returns:

  • (Boolean)

    true if saved successfully



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fractor/queue_persister.rb', line 205

def save(items)
  ensure_directory_exists

  serialized = items.map { |work| serialize_work(work) }
  data = Marshal.dump(serialized)

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