headius / spoon

A fork/exec replacement for FFI-capable implementations
Apache License 2.0
48 stars 12 forks source link

How can I use spoon to execute a ruby block like fork does? #15

Closed ramarnat closed 11 years ago

ramarnat commented 11 years ago

I am looking to update some code that uses fork so it's compatible with JRuby. The code runs a fork with a block of code, how do I accomplish the same functionality with spoon? I think I have to kick off another jruby instance, but how do I have it hone in on running a particular block?

This is from the aws simple workflow sdk btw.

 class ForkingExecutor 
     def execute(&block)
        @log.error "Here are the pids that are currently running #{@pids}"
        raise RejectedExecutionException if @is_shutdown
        block_on_max_workers
        @log.debug "PARENT BEFORE FORK #{Process.pid}"
        child_pid = fork do
          begin
            @log.debug "CHILD #{Process.pid}"
            # TODO: which signals to ignore?
            # ignore signals in the child
            %w{ TERM INT HUP SIGUSR2 }.each { |signal| Signal.trap(signal, 'SIG_IGN') }
            block.call
            @log.debug "CHILD #{Process.pid} AFTER block.call"
            Process.exit!(0)
          rescue => e
            @log.error e
            @log.error "Definitely dying off right here"
            Process.exit!(1)
          end
        end
        @log.debug "PARENT AFTER FORK #{Process.pid}, child_pid=#{child_pid}"
        @pids << child_pid
      end
end

worker = WorkflowWorker.new(@swf.client, @domain, task_list)
forking_executor = ForkingExecutor.new
forking_executor.execute { worker.start }
headius commented 11 years ago

There's no way to spawn a subprocess using a block of code under JRuby, even with the spoon gem. However, it should be simple to replace this code with a thread that runs the logic and then terminates itself. You'd get the same concurrency without spawning.

ramarnat commented 11 years ago

thanks for the quick reply!

the workers need to run as separate processes, so I think I will follow the pattern laid out by foreman by using a runner as laid out here - https://github.com/ddollar/foreman/pull/140

ramarnat commented 11 years ago

Looking down another path, I found your gist https://gist.github.com/headius/1378616

But it doesn't work in 1.7.4, expected?

ramarnat commented 11 years ago

got further by using java.util.concurrent.ThreadPoolExecutor as you suggested, but now looks like I hit a wall with this JRuby bug - http://jira.codehaus.org/browse/JRUBY-7188

giving up and going back to MRI for now :(