Module: Polyseerio::Agent::Executor

Defined in:
lib/agent/executor.rb

Overview

Agent executor functions.

Class Method Summary collapse

Class Method Details

.setup(client, options = {}) ⇒ Object

Setsup a client’s agent.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/agent/executor.rb', line 13

def self.setup(client, options = {})
  Polyseerio.log 'debug', 'Setting up client agent.'

  options = Polyseerio::Helper.defaults(options, DEFAULT_CONFIG)

  Concurrent::Promise.new do
    if options[:attach]
      name = Helper.resolve_name(options)

      # Resolve the instance.
      # TODO: better handle failure to upsert.
      instance = Resource::Routine.upsert(
        client.Instance,
        name: name
      ).execute.value

      # Set the client's instance.
      client.instance = instance

      # Create a setup handler.
      handler_options = Helper.extract_handler_options options
      setup_handler = Helper.setup_with_handler(
        Polyseerio::Agent::Handler::MAP,
        client,
        handler_options
      )

      # Gather setup operations.
      setups = handler_options.map { |key, _| setup_handler.call(key) }

      # Perform setups.
      Concurrent::Promise.zip(*setups).execute.value

      # Start monitoring.
      Polyseerio.log 'debug', 'Attaching instance to Polyseer.io'
      instance.attach.execute.value

      instance
    end
  end
end

.teardown(client, instance) ⇒ Object

Tearsdown a client’s agent.



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

def self.teardown(client, instance)
  Concurrent::Promise.new do
    teardown_handler('event', client)

    instance.detach.excecute.value
  end
end