Skip to content

Instantly share code, notes, and snippets.

@Alphabetus
Forked from tompave/async_rake.rb
Created September 19, 2019 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alphabetus/8795521674169e3ffe8f4c426c7e17bd to your computer and use it in GitHub Desktop.
Save Alphabetus/8795521674169e3ffe8f4c426c7e17bd to your computer and use it in GitHub Desktop.
how to run rake tasks asynchronously from controllers by spawning child processes
class ApplicationController < ActionController::Base
def perform_async(task, options = {})
options[:rails_env] ||= Rails.env
env_vars = options.map { |key, value| "#{key.to_s.upcase}='#{value.to_s}'" }
env_vars_string = env_vars.join(' ')
Process.fork {exec("#{env_vars_string} bin/rake #{task}")}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment