Class: Legion::Extensions::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/handle.rb

Constant Summary collapse

STATES =
%i[registered loaded starting running stopping stopped failed].freeze
LOADED_STATES =
%i[loaded starting running stopping].freeze
DISPATCHABLE_STATES =
%i[loaded starting running].freeze
RELOAD_STATES =
%i[idle pending updating rolling_back failed].freeze
DISPATCH_BLOCKING_RELOAD_STATES =
%i[updating rolling_back].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Handle

Returns a new instance of Handle.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/handle.rb', line 16

def initialize(**attrs)
  lex_name = attrs.fetch(:lex_name)
  spec = attrs[:spec]
  @lex_name = lex_name.to_s
  @gem_name = (attrs[:gem_name] || lex_name).to_s
  @spec = spec
  @active_version = normalize_version(attrs[:active_version] || spec&.version)
  @latest_installed_version = normalize_version(attrs[:latest_installed_version] || attrs[:installed_version] || @active_version)
  @state = normalize_state(attrs.fetch(:state, :registered))
  @reload_state = normalize_reload_state(attrs.fetch(:reload_state, :idle))
  @hot_reloadable = attrs[:hot_reloadable] == true
  @gem_dir = attrs[:gem_dir] || spec&.gem_dir
  @loaded_features = Array(attrs.fetch(:loaded_features, [])).dup.freeze
  @actors = Array(attrs.fetch(:actors, [])).dup.freeze
  @routes = Array(attrs.fetch(:routes, [])).dup.freeze
  @tools = Array(attrs.fetch(:tools, [])).dup.freeze
  @absorbers = Array(attrs.fetch(:absorbers, [])).dup.freeze
  @runners = Array(attrs.fetch(:runners, [])).dup.freeze
  @loaded_at = attrs.fetch(:loaded_at, Time.now)
  @last_error = attrs[:last_error]
end

Instance Attribute Details

#absorbersObject (readonly)

Returns the value of attribute absorbers.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def absorbers
  @absorbers
end

#active_versionObject (readonly)

Returns the value of attribute active_version.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def active_version
  @active_version
end

#actorsObject (readonly)

Returns the value of attribute actors.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def actors
  @actors
end

#gem_dirObject (readonly)

Returns the value of attribute gem_dir.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def gem_dir
  @gem_dir
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def gem_name
  @gem_name
end

#hot_reloadableObject (readonly)

Returns the value of attribute hot_reloadable.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def hot_reloadable
  @hot_reloadable
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def last_error
  @last_error
end

#latest_installed_versionObject (readonly)

Returns the value of attribute latest_installed_version.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def latest_installed_version
  @latest_installed_version
end

#lex_nameObject (readonly)

Returns the value of attribute lex_name.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def lex_name
  @lex_name
end

#loaded_atObject (readonly)

Returns the value of attribute loaded_at.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def loaded_at
  @loaded_at
end

#loaded_featuresObject (readonly)

Returns the value of attribute loaded_features.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def loaded_features
  @loaded_features
end

#reload_stateObject (readonly)

Returns the value of attribute reload_state.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def reload_state
  @reload_state
end

#routesObject (readonly)

Returns the value of attribute routes.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def routes
  @routes
end

#runnersObject (readonly)

Returns the value of attribute runners.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def runners
  @runners
end

#specObject (readonly)

Returns the value of attribute spec.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def spec
  @spec
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def state
  @state
end

#toolsObject (readonly)

Returns the value of attribute tools.



12
13
14
# File 'lib/legion/extensions/handle.rb', line 12

def tools
  @tools
end

Instance Method Details

#dispatchable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/legion/extensions/handle.rb', line 52

def dispatchable?
  DISPATCHABLE_STATES.include?(state) && !DISPATCH_BLOCKING_RELOAD_STATES.include?(reload_state)
end

#loaded?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/legion/extensions/handle.rb', line 38

def loaded?
  LOADED_STATES.include?(state)
end

#pending_reload?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/legion/extensions/handle.rb', line 46

def pending_reload?
  return false if active_version.nil? || latest_installed_version.nil?

  latest_installed_version > active_version
end

#running?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/legion/extensions/handle.rb', line 42

def running?
  state == :running
end

#to_hObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/handle.rb', line 60

def to_h
  {
    lex_name:                 lex_name,
    gem_name:                 gem_name,
    active_version:           active_version,
    latest_installed_version: latest_installed_version,
    state:                    state,
    reload_state:             reload_state,
    hot_reloadable:           hot_reloadable,
    spec:                     spec,
    gem_dir:                  gem_dir,
    loaded_features:          loaded_features,
    actors:                   actors,
    routes:                   routes,
    tools:                    tools,
    absorbers:                absorbers,
    runners:                  runners,
    loaded_at:                loaded_at,
    last_error:               last_error
  }
end

#with(**attrs) ⇒ Object



56
57
58
# File 'lib/legion/extensions/handle.rb', line 56

def with(**attrs)
  self.class.new(**to_h, **attrs)
end