Class: Lepus::ProcessRegistry::FileBackend

Inherits:
Object
  • Object
show all
Includes:
Backend
Defined in:
lib/lepus/process_registry/file_backend.rb

Overview

File-based backend for process registry storage. Stores process data in a file using Marshal serialization. This is the default backend for apps not using the web dashboard.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Backend

#update

Constructor Details

#initialize(path: nil) ⇒ FileBackend

Returns a new instance of FileBackend.



17
18
19
# File 'lib/lepus/process_registry/file_backend.rb', line 17

def initialize(path: nil)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/lepus/process_registry/file_backend.rb', line 15

def path
  @path
end

Instance Method Details

#add(process, metrics: {}) ⇒ Object



29
30
31
32
33
# File 'lib/lepus/process_registry/file_backend.rb', line 29

def add(process, metrics: {})
  transaction do |data|
    data[process.id] = process.to_h
  end
end

#allObject



50
51
52
# File 'lib/lepus/process_registry/file_backend.rb', line 50

def all
  read.keys.map { |id| find(id) }
end

#clearObject



60
61
62
63
64
# File 'lib/lepus/process_registry/file_backend.rb', line 60

def clear
  return unless path

  write({})
end

#countObject



54
55
56
57
58
# File 'lib/lepus/process_registry/file_backend.rb', line 54

def count
  return 0 unless path

  read.size
end

#delete(process) ⇒ Object



35
36
37
38
39
# File 'lib/lepus/process_registry/file_backend.rb', line 35

def delete(process)
  transaction do |data|
    data.delete(process.id)
  end
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/lepus/process_registry/file_backend.rb', line 46

def exists?(id)
  read.key?(id)
end

#find(id) ⇒ Object



41
42
43
44
# File 'lib/lepus/process_registry/file_backend.rb', line 41

def find(id)
  raw = read.fetch(id) { raise(Lepus::Process::NotFoundError.new(id)) }
  Lepus::Process.coerce(raw)
end

#startObject



21
22
23
# File 'lib/lepus/process_registry/file_backend.rb', line 21

def start
  @path ||= Pathname.new(Dir.tmpdir).join("lepus_process_registry.store")
end

#stopObject



25
26
27
# File 'lib/lepus/process_registry/file_backend.rb', line 25

def stop
  path.delete if path&.exist?
end