Sha256: 151dfb9a45c77b6ee506bab27e1191eb52374794cca8781555dda2b432a99425

Contents?: true

Size: 1.72 KB

Versions: 17

Compression:

Stored size: 1.72 KB

Contents

module Nanite
  class Agent

    attr_accessor :init_block

    def load_actors_with_daemon_kit_changes( &block )
      actors = @options[:actors]
      Dir["#{DaemonKit.root}/lib/actors/*.rb"].each do |actor|
        next if actors && !actors.include?( File.basename(actor, '.rb') )
        Nanite::Log.info( "[setup] loading #{actor}" )
        require actor
      end

      self.init_block.call( self )
    end

    alias_method :load_actors_without_daemon_kit_changes, :load_actors
    alias_method :load_actors, :load_actors_with_daemon_kit_changes
  end
end

module DaemonKit
  module Nanite
    # Pull support into a daemon for being a nanite agent.
    class Agent

      @@instance = nil

      class << self

        def instance
          @instance ||= new
        end

        private :new

        def run(&block)
          instance.run(&block)
        end

      end

      def initialize
        @config = DaemonKit::Config.load( 'nanite' ).to_h( true )

        config_agent
      end

      def run(&block)
        # Ensure graceful shutdown of the connection to the broker
        DaemonKit.trap('INT') { ::EM.stop }
        DaemonKit.trap('TERM') { ::EM.stop }
        ::Nanite::Log.logger = DaemonKit.logger

        # Start our mapper
        mapper_thread = Thread.new do
          EM.run do
            agent = ::Nanite::Agent.new( @config )
            agent.init_block = block
            agent.run
          end
        end

        mapper_thread.join
      end

      private

      # Make sure to fine tune the agent config to be DK friendly
      def config_agent
        @config[:root] = DAEMON_ROOT
        @config[:daemonize] = false
        @config[:actors_dir] = File.join(DAEMON_ROOT, 'lib', 'actors')
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
daemon-kit-0.2.3 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.2.1 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.2.0 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8.2 lib/daemon_kit/nanite/agent.rb
amqp-daemon-kit-1.0.1 lib/daemon_kit/nanite/agent.rb
amqp-daemon-kit-0.1.8.2 lib/daemon_kit/nanite/agent.rb
amqp-daemon-kit-0.1.8.1 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8.1 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8rc3 lib/daemon_kit/nanite/agent.rb
textgoeshere-daemon-kit-0.1.8.31 lib/daemon_kit/nanite/agent.rb
textgoeshere-daemon-kit-0.1.8rc3 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8rc2 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8rc1 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.8pre lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.7.12 lib/daemon_kit/nanite/agent.rb
daemon-kit-0.1.7.11 lib/daemon_kit/nanite/agent.rb