Skip to content

Instantly share code, notes, and snippets.

@Paxa
Created November 20, 2011 08:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paxa/1379969 to your computer and use it in GitHub Desktop.
Save Paxa/1379969 to your computer and use it in GitHub Desktop.
Simple background processing
# config/application.rb
def Rails.bg_runner(code)
ruby_cmd = code.gsub(/("|\s)/) {|m| "\\#{m}" }
cmd = %{cd #{Rails.root} && ./script/background #{Rails.env} "#{ruby_cmd}"}
Rails.logger.info "IN BACKGROUND #{cmd}"
Rails.logger.info system(cmd)
end
#!/usr/bin/env ruby
require "pathname"
raise "no env specified" unless ARGV.first
`echo '#{ARGV.inspect}' >> /tmp/1`
if !fork
Process.setsid
STDIN.reopen '/dev/null'
STDOUT.reopen '/tmp/1', 'a'
STDERR.reopen STDOUT
dir = Pathname.new(__FILE__).dirname.join("..").expand_path
env = ARGV.shift
code = ARGV.first.to_s.gsub(/('|\s)/) {|m| "\\#{m}" }
command = %{RAILS_ENV=#{env} cd #{dir} && bundle exec rails runner '#{code}'}
puts command
`#{command}`
else
puts "ok"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment