Sha256: d889c141a4c40c798f685ab0577d15cd243c3b6b605d0583c189b5c984025487

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

require 'asir/transport'

module Asir
  class Transport
    # Conduit service support.
    module Conduit
      attr_accessor :conduit_options, :conduit_pid

      def start_conduit! options = nil
        opts = @conduit_options ||= {}
        opts.update(:fork => true)
        opts.update(options) if options
        _log { "start_conduit! #{self}" } if @verbose >= 1
        in_fork = opts[:fork]
        raise "already running #{@conduit_pid} #{@conduit_cmd}" if @conduit_pid
        if in_fork
          @conduit_pid = ::Process.fork do
          _log { "start_conduit! #{self} starting pid=#{$$.inspect}" } if @verbose >= 2
            _start_conduit!
            raise "Could not exec"
          end
          _log { "start_conduit! #{self} started pid=#{@conduit_pid.inspect}" } if @verbose >= 2
          if pid_file = (@conduit_options || EMPTY_HASH)[:pid_file]
            File.open(pid_file, "w") { | fh | fh.puts @conduit_pid }
          end
        else
          _start_conduit!
        end
        self
      end

      def conduit_pid
        if ! @conduit_pid and pid_file = (@conduit_options || EMPTY_HASH)[:pid_file]
          @conduit_pid = (File.read(pid_file).to_i rescue nil)
        end
        @conduit_pid
      end

      def stop_conduit! opts = nil
        if conduit_pid
          _log { "stop_conduit! #{self} pid=#{@conduit_pid.inspect}" } if @verbose >= 1
          ::Process.kill( (opts && opts[:signal]) || 'TERM', @conduit_pid)
          if pid_file = (@conduit_options || EMPTY_HASH)[:pid_file]
            ::File.unlink(pid_file) rescue nil
          end
          ::Process.waitpid @conduit_pid
        end
        self
      ensure
        @conduit_pid = nil
      end

      def _start_conduit!
        raise Error::SubclassResponsibility
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
asir-1.2.2 lib/asir/transport/conduit.rb
asir-1.2.1 lib/asir/transport/conduit.rb
asir-1.2.0 lib/asir/transport/conduit.rb