Sha256: 65af4418d6ddc881459a94c20b4817b45bbdc4fe9399e477d3a28249a4ab015a

Contents?: true

Size: 1.94 KB

Versions: 47

Compression:

Stored size: 1.94 KB

Contents

module Vedeu

  module Distributed

    # @example
    #   Vedeu::TestApplication.build
    #
    class Subprocess

      # @param (see #initialize)
      def self.execute!(application)
        new(application).execute!
      end

      # Returns a new instance of Vedeu::Distributed::Subprocess.
      #
      # @param application [Vedeu::TestApplication]
      # @return [Vedeu::Distributed::Subprocess]
      def initialize(application)
        @application = application
        @pid         = nil
      end

      # :nocov:
      # @return [Array]
      def execute!
        file_open && file_write && file_close

        @pid = fork do
          exec(file_path)
        end

        Process.detach(@pid)

        self
      end
      # :nocov:

      # Sends the KILL signal to the process.
      #
      # @return [void]
      def kill
        Process.kill('KILL', pid)

        file_unlink
      end

      protected

      # @!attribute [r] application
      # @return [Vedeu::TestApplication]
      attr_reader :application

      # @!attribute [r] pid
      # @return [Fixnum]
      attr_reader :pid

      private

      # @return [String]
      def command
        "ruby #{file_path}".freeze
      end

      # @return [Fixnum] The number of bytes written.
      def file_write
        file.write(application)
      end

      # @return [NilClass]
      def file_close
        file.close
      end

      # @return [Fixnum] The number of files removed; 1.
      def file_unlink
        File.unlink(file_name)
      end

      # return [String]
      def file_path
        file.path
      end

      # @return [File]
      def file_open
        @file ||= File.new(file_name, 'w', 0755)
      end
      alias_method :file, :file_open

      # @return [String]
      def file_name
        "/tmp/foo_#{timestamp}".freeze
      end

      # return [Fixnum]
      def timestamp
        @timestamp ||= Time.now.to_i
      end

    end # Subprocess

  end # Distributed

end # Vedeu

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
vedeu-0.8.0 lib/vedeu/distributed/subprocess.rb
vedeu-0.7.4 lib/vedeu/distributed/subprocess.rb
vedeu-0.7.3 lib/vedeu/distributed/subprocess.rb
vedeu-0.7.2 lib/vedeu/distributed/subprocess.rb
vedeu-0.7.1 lib/vedeu/distributed/subprocess.rb
vedeu-0.7.0 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.71 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.70 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.69 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.68 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.67 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.66 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.65 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.64 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.63 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.62 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.61 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.60 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.59 lib/vedeu/distributed/subprocess.rb
vedeu-0.6.58 lib/vedeu/distributed/subprocess.rb