Created
November 20, 2011 08:12
-
-
Save Paxa/1379969 to your computer and use it in GitHub Desktop.
Simple background processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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