Class: Raises::SpoolStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/raises/spool_storage.rb

Constant Summary collapse

MAX_NOTICES =
1_000
MAX_BYTES =
100 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(directory, warn:, now:) ⇒ SpoolStorage

Returns a new instance of SpoolStorage.



12
13
14
15
16
17
# File 'lib/raises/spool_storage.rb', line 12

def initialize(directory, warn:, now:)
  @directory = File.expand_path(directory)
  @warn = warn
  @now = now
  prepare
end

Instance Method Details

#delete(path) ⇒ Object



51
52
53
54
55
# File 'lib/raises/spool_storage.rb', line 51

def delete(path)
  File.delete(path)
rescue Errno::ENOENT
  nil
end

#each_due(limit:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/raises/spool_storage.rb', line 30

def each_due(limit:)
  processed = 0
  files.each do |path|
    break if processed >= limit

    File.open(path, File::RDWR) do |file|
      next unless file.flock(File::LOCK_EX | File::LOCK_NB)

      envelope = parse(file.read, path)
      next unless envelope
      next if envelope.fetch("next_attempt_at", 0).to_f > @now.call.to_f

      processed += 1
      yield path, envelope
    end
  rescue Errno::ENOENT
    next
  end
  processed
end

#rewrite(path, envelope) ⇒ Object



57
58
59
# File 'lib/raises/spool_storage.rb', line 57

def rewrite(path, envelope)
  atomic_write(path, JSON.generate(envelope))
end

#store(notice) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/raises/spool_storage.rb', line 19

def store(notice)
  prepare
  raw = JSON.generate("version" => 1, "attempts" => 0, "next_attempt_at" => @now.call.to_f, "notice" => notice)
  with_directory_lock do
    return :full unless capacity_for?(raw)

    atomic_write(File.join(@directory, filename), raw)
  end
  :stored
end