0

I'm attempting to setup a Ruby rake task that I usually split into 2 parts manually (ie. run in two separate tabs) as one parallelized rake task, running as rake parallel:main seen here:

namespace :parallel do
  multitask main: [:job1, :job2]

  task :job1 => :environment do
    companies = (34320..34330).to_a

    companies.each do |c|
      puts "Running on company #{c} - #{Time.now}"

      Rake::Task['parallel:my_task'].invoke("#{c}")
      Rake::Task['parallel:my_task'].reenable
    end
  end

  task :job2 => :environment do
    companies = (34340..34350).to_a

    companies.each do |c|
      puts "Running on company #{c} - #{Time.now}"

      Rake::Task['parallel:my_task'].invoke("#{c}")
      Rake::Task['parallel:my_task'].reenable
    end
  end

  task :my_task, [:c] => :environment do |t, args|
    puts "Processing #{args[:c]} - #{Time.now}"
    sleep 5
  end
end

I can see that the Time.now from within these job tasks are run in parallel, but based on Time.now output from within my_task I can see that the underlying rake task is not run in parallel.

This is what the output looks like:

$ rake parallel:main
Running on company 34320 - 2017-06-15 14:15:17 -0400
Running on company 34340 - 2017-06-15 14:15:17 -0400
Processing 34320 - 2017-06-15 14:15:17 -0400
Processing 34340 - 2017-06-15 14:15:22 -0400
Running on company 34321 - 2017-06-15 14:15:22 -0400
Running on company 34341 - 2017-06-15 14:15:27 -0400
Processing 34321 - 2017-06-15 14:15:27 -0400
Running on company 34342 - 2017-06-15 14:15:32 -0400
Processing 34342 - 2017-06-15 14:15:32 -0400
Running on company 34322 - 2017-06-15 14:15:32 -0400

As you can see, the first Processing happens at 14:15:17, and the second at 14:15:22, based on the sleep. I'd like these to run simultaneously - how can I achieve this?

Manonthemoon
  • 95
  • 1
  • 1
  • 4

1 Answers1

0

Right, I just learnt that invoke executes the dependencies, but it only executes the task if it has not already been invoked (source).

So I changed my code to the following and it works well: Rake::Task['parallel:my_task'].execute :c => c

Manonthemoon
  • 95
  • 1
  • 1
  • 4