Sha256: 08d2a94818b9ecf5a7deae536b566a4d8cd63cc570514d5fcb89f8a89cf146d5

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'populus/remote_runner'
require 'populus/node'

module Populus
  class Accepter
    # TODO: validators
    class Base
      attr_accessor :condition, :runner, :metadata

      def initialize(cond: nil, runs: nil, metadata: {})
        self.condition = cond
        self.runner = runs
        self.metadata = metadata
      end

      def type?(t)
        current_type = self.class.name.downcase
          .split('::')
          .last
        current_type == t
      end

      def accept(data)
        if condition[data]
          Populus.logger.debug "Condition judged true: #{data.inspect}"
          instance_exec(data, &runner)
          return true
        else
          Populus.logger.debug "Condition judged false: #{data.inspect}"
          return false
        end
      end

      def on(hostname, &run_it)
        be = Node.registry[hostname]
        if be
          RemoteRunner.new(be, &run_it)
        else
          Populus.logger.warn "Not found host: #{hostname}. Skip."
        end
      end
    end

    class Event < Base
      def has_name?(name)
        metadata[:name] == name
      end

      def create_thread
        _name = metadata[:name]
        Populus.logger.debug "Create thread: consul watch -type event -name #{_name}"
        Populus::WatchThread.consul_watch('-type', 'event', '-name', _name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
populus-0.0.4 lib/populus/accepter.rb