Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a hook for having just started a process? (making SimpleCov work) #275

Closed
robotdana opened this issue Mar 2, 2020 · 2 comments
Closed

Comments

@robotdana
Copy link

robotdana commented Mar 2, 2020

I recently added simplecov to my project, and found if i wanted simplecov to track what Parallel was doing I had to override the Parallel.worker method to inject SimpleCov.start and a unique command name.

I ended up adding this whole overridden method to my .simplecov file:

module Parallel
   def self.worker(job_factory, options, &block)
     child_read, parent_write = IO.pipe
     parent_read, child_write = IO.pipe

     pid = Process.fork do
       self.worker_number = options[:worker_number]

       begin
         # here begins my additions
         SimpleCov.command_name "Parallel #{worker_number} #{Process.pid}"
         SimpleCov.start
         # here ends my additions

         options.delete(:started_workers).each(&:close_pipes)

         parent_write.close
         parent_read.close

         process_incoming_jobs(child_read, child_write, job_factory, options, &block)
       ensure
         child_read.close
         child_write.close
       end
     end

     child_read.close
     child_write.close

     Worker.new(parent_read, parent_write, pid)
   end
 end

I'd love a way to do this that was expected. (and I'd prefer that way be global rather than every Parrallel.each call.)

Do you have a preference/expectation for how one should do this? I'm happy to make a PR adding an option if you'd like

Perhaps someone could have in their .simplecov something like

Parallel.post_fork = -> { 
  SimpleCov.command_name "Parallel #{Process.pid}"
  SimpleCov.start
}

and we add Parallel.post_fork&.call in the Process.worker method where i added my code?

Thanks :)

@robotdana robotdana changed the title Adding a hook for having just started a process? Adding a hook for having just started a process? (making SimpleCov work) Mar 2, 2020
@grosser
Copy link
Owner

grosser commented Mar 4, 2020

I'd rather have the post_fork as an option if possible, there are already too many global things happening :)

As a workaround you can also do:

Parallel.each([1,2,3,4], in_processes: 2) do |i|
  puts "hey" unless $process_initialized
  $process_initialized = true
end

... or

cpus = 2
Parallel.each_with_index([1,2,3,4], in_processes: cpus) do |_, i|
  puts "hey" if i % cpus == 0
end

FYI try single_cov and forking-test-runner

@robotdana
Copy link
Author

I made a pr with simplecov instead, simplecov-ruby/simplecov#881 i recommend using this if you're here from google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants