Class: BackgroundLite::DiskHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/background_lite/core_ext/handlers/disk_handler.rb

Overview

Stores the serialized message on disk. This handler is probably most useful as a fallback handler.

Constant Summary collapse

@@dirname =

The directory in which the serialized messages should be stored.

nil

Class Method Summary collapse

Class Method Details

.handle(object, method, args, options = {}) ⇒ Object

Marshals the message and the locals into a file in the folder specified by dirname.



11
12
13
14
15
16
# File 'lib/background_lite/core_ext/handlers/disk_handler.rb', line 11

def self.handle(object, method, args, options = {})
  filename = "background_#{Time.now.to_f.to_s}"
  File.open("#{dirname}/#{filename}", 'w') do |file|
    file.print(Marshal.dump([object, method, args]))
  end
end

.recover(handler) ⇒ Object

Replays all marshalled background tasks in the order in which they were stored into the folder specified by dirname.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/background_lite/core_ext/handlers/disk_handler.rb', line 20

def self.recover(handler)
  handler_class = "BackgroundLite::#{handler.to_s.camelize}Handler".constantize
  Dir.entries(dirname).grep(/^background/).sort.each do |filename|
    path = "#{dirname}/#{filename}"
    File.open(path, 'r') do |file|
      object, method, args = Marshal.load(file)
      handler_class.handle(object, method, args)
    end
    FileUtils.rm(path)
  end
end