Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 = { |key, value| "#{key.to_s.upcase}='#{value.to_s}'" }
env_vars_string = env_vars.join(' ')
Process.fork {exec("#{env_vars_string} bin/rake #{task}")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment