Class: Lepus::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/lepus/process.rb

Defined Under Namespace

Classes: NotFoundError

Constant Summary collapse

ATTRIBUTES =
%i[id name pid hostname kind last_heartbeat_at supervisor_id].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Process

Returns a new instance of Process.



52
53
54
55
# File 'lib/lepus/process.rb', line 52

def initialize(**attributes)
  @attributes = attributes
  @attributes[:id] ||= SecureRandom.uuid
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Class Method Details

.coerce(raw) ⇒ Object



43
44
45
46
47
# File 'lib/lepus/process.rb', line 43

def coerce(raw)
  attrs = raw.transform_keys(&:to_sym)
  attrs[:last_heartbeat_at] = Time.iso8601(attrs[:last_heartbeat_at]) if attrs[:last_heartbeat_at]
  new(**attrs)
end

.prunableObject



37
38
39
40
41
# File 'lib/lepus/process.rb', line 37

def prunable
  ProcessRegistry.all.select do |process|
    process.last_heartbeat_at && process.last_heartbeat_at < Time.now - Lepus.config.process_alive_threshold
  end
end

.prune(excluding: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/lepus/process.rb', line 27

def prune(excluding: nil)
  Lepus.instrument :prune_processes, size: 0 do |payload|
    arr = prunable
    arr.delete(excluding) if excluding
    payload[:size] = arr.size

    arr.each(&:prune)
  end
end

.register(**attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lepus/process.rb', line 14

def register(**attributes)
  attributes[:id] ||= SecureRandom.uuid
  Lepus.instrument :register_process, **attributes do |payload|
    new(**attributes).tap do |process|
      ProcessRegistry.add(process)
      payload[:process_id] = process.id
    end
  rescue Exception => error # rubocop:disable Lint/RescueException
    payload[:error] = error
    raise
  end
end

Instance Method Details

#deregister(pruned: false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lepus/process.rb', line 104

def deregister(pruned: false)
  Lepus.instrument :deregister_process, process: self, pruned: pruned do |payload|
    destroy!

    unless supervised? || pruned
      supervisees.each(&:deregister)
    end
  rescue Exception => error # rubocop:disable Lint/RescueException
    payload[:error] = error
    raise
  end
end

#destroy!Object



95
96
97
98
99
100
101
102
# File 'lib/lepus/process.rb', line 95

def destroy!
  Lepus.instrument :destroy_process, process: self do |payload|
    ProcessRegistry.delete(self)
  rescue Exception => error # rubocop:disable Lint/RescueException
    payload[:error] = error
    raise
  end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


125
126
127
# File 'lib/lepus/process.rb', line 125

def eql?(other)
  other.is_a?(self.class) && other.id == id && other.pid == pid
end

#heartbeat(metrics: {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lepus/process.rb', line 77

def heartbeat(metrics: {})
  now = Time.now
  Lepus.instrument :heartbeat_process, process: self, rss_memory: 0, last_heartbeat_at: now do |payload|
    ProcessRegistry.find(id) # ensure process is still registered

    update_attributes(last_heartbeat_at: now, metrics: metrics)
    payload[:rss_memory] = rss_memory
  rescue Exception => error # rubocop:disable Lint/RescueException
    payload[:error] = error
    raise
  end
end

#last_heartbeat_atObject



69
70
71
# File 'lib/lepus/process.rb', line 69

def last_heartbeat_at
  attributes[:last_heartbeat_at]
end

#pruneObject



117
118
119
# File 'lib/lepus/process.rb', line 117

def prune
  deregister(pruned: true)
end

#rss_memoryObject



73
74
75
# File 'lib/lepus/process.rb', line 73

def rss_memory
  Processes::MEMORY_GRABBER.call(pid)
end

#supervised?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/lepus/process.rb', line 121

def supervised?
  !attributes[:supervisor_id].nil?
end

#to_hObject



57
58
59
60
61
62
63
# File 'lib/lepus/process.rb', line 57

def to_h
  attributes.dup.tap do |hash|
    if hash[:last_heartbeat_at]
      hash[:last_heartbeat_at] = hash[:last_heartbeat_at].iso8601(6)
    end
  end
end

#update_attributes(metrics: {}, **new_attributes) ⇒ Object



90
91
92
93
# File 'lib/lepus/process.rb', line 90

def update_attributes(metrics: {}, **new_attributes)
  @attributes = @attributes.merge(new_attributes)
  ProcessRegistry.update(self, metrics: metrics)
end