Skip to content

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

@robotdana

Description

@robotdana

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions