Sha256: f36f6c2e6287f9123ee5ad46ef8a4653a029b8e9b0e1c194cd2c73ceed324322

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
# License::   GNU General Public License (GPL).
# Revision::  $Id: fork.rb 221 2005-05-09 12:40:57Z ertai $

module Commands

  module Runners

    class Fork < Exec
      concrete

      #
      # Hooks declaration
      #

      hook_declare :exception_raised_during_exec
      hook_declare :already_killed
      hook_declare :waitpid
      hook_declare :son
      hook_declare :kill

      #
      # Methods
      #

      def run_impl ( aCommand, data )
        data.pid = Kernel.fork do
          begin
            hook_trigger :son, aCommand, data
            super
          rescue Exception => ex
            hook_trigger :exception_raised_during_exec, ex
          end
        end
        hook_trigger :waitpid, data
      end


      def abort ( data )
        if data.pid
          begin
            hook_trigger :kill, data
            hook_trigger :waitpid, data
          rescue Errno::ESRCH
            hook_trigger :already_killed, data.pid
          end
        end
        super
      end


      def waitpid ( data )
        Process.waitpid(data.pid)
        data.status = $?
      end


      def exception_raised_during_exec ( anException )
        exit! 127
      end


      def kill ( data )
        Process.kill('-KILL', -data.pid)
      end

    end # class Fork



    if $0 == __FILE__
      require 'test/unit'

      class ForkTest < Test::Unit::TestCase

        def setup
          assert_nothing_raised { @runner = Fork.new }
        end

        def test_0_initialize
        end

        def test_simple
          TempPath.new do |tmp|
            cmd = %Q[ruby -e "File.open('#{tmp}', 'w') { |f| f.puts :foo }"]
            @runner.run(cmd.to_cmd)
          end
        end

      end # class Fork

    end

  end # module Runners

end # module Commands

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vcs-0.2.148 ruby_ex/commands/runners/fork.rb