Module: Locd::Agent::System

Included in:
Proxy, RotateLogs
Defined in:
lib/locd/agent/system.rb

Overview

Mixin for common stuff that system agents do different.

System agents are specific types of Locd::Agent built in to Loc'd to handle things like proxying HTTP request site (Proxy), rotating logs so they don't get unwieldily (RotaeLogs), and probably more in the future, like serving a site index, API, etc.

System agents are singular - there's only one of each for a Loc'd label namespace, which is set via the configuration, and Loc'd uses that label namespace, together with the each system agent's .label_name, to form the agent's unique label, which is how it finds the system agents.

This was really just to make it simpler by side-stepping how to handle many proxies and log rotators and such because besides the dev/release situation described above I can't really think of any reason you would want to create and manage multiple of each system agent.

As a consequence, Locd::Agent subclasses that mix System in must define a .label_name class method.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@classes =
Hamster::Set.new

Class Method Summary collapse

Class Method Details

.class_for(plist) ⇒ Class<Locd::Agent>?

Find the concrete Locd::Agent subclass (that has mixed in Locd::Agent::System) for a property list.

Parameters:

  • plist (Hash<String, Object>)

Returns:

  • (Class<Locd::Agent>)

    If the plist is for one of classes, returns that class.

  • (nil)

    If the plist is:

    1. Not a system plist (see plist?)

    2. If none of classes claim it (via their .plist? method).

      Though, really, this one shouldn't happen except in weird version switching situations maybe or something.



82
83
84
85
86
# File 'lib/locd/agent/system.rb', line 82

def self.class_for plist
  return nil unless plist?( plist )
  
  classes.to_a.find_bounded( max: 1 ) { |cls| cls.plist? plist }.first
end

.classesObject



38
39
40
# File 'lib/locd/agent/system.rb', line 38

def self.classes
  @@classes
end

.included(base) ⇒ Object



89
90
91
92
# File 'lib/locd/agent/system.rb', line 89

def self.included base
  base.extend ClassMethods
  @@classes = @@classes.add base
end

.label?(label) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/locd/agent/system.rb', line 43

def self.label? label
  label.is_a?( String ) \
  && label.start_with?( Locd.config[:namespace, :label] )
end

.plist?(plist) ⇒ Boolean

Is a plist for one of this config's system agents?

Parameters:

  • plist (Hash<String, Object>)

Returns:

  • (Boolean)

    true if the plist is for a system agent for this Loc'd config.



56
57
58
59
60
61
# File 'lib/locd/agent/system.rb', line 56

def self.plist? plist
  !!(
    plist.dig( Locd.config[:agent, :config_key], 'is_system' ) \
    && label?( plist['Label'] )
  )
end