Sha256: 9cfb872e369dfe77ef0dc9e39bf1a403659ded7dbfa9abbcb667c8b94f84ccb0

Contents?: true

Size: 1.17 KB

Versions: 8

Compression:

Stored size: 1.17 KB

Contents

class Ridgepole::ExternalSqlExecuter
  def initialize(script, logger)
    @script = script
    @logger = logger
  end

  def execute(sql)
    cmd = Shellwords.join([@script, sql, JSON.dump(ActiveRecord::Base.connection_config)])
    @logger.info("Execute #{@script}")
    script_basename = File.basename(@script)

    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      stdin.close_write
      files = [stdout, stderr]

      begin
        until files.empty?
          ready = IO.select(files)

          if ready
            readable = ready[0]

            readable.each do |f|
              begin
                data = f.read_nonblock(1024)
                next if data.nil?
                data.chomp!

                if f == stderr
                  @logger.warn("[WARNING] #{script_basename}: #{data}")
                else
                  @logger.info("#{script_basename}: #{data}")
                end
              rescue EOFError => e
                files.delete f
              end
            end
          end
        end
      rescue EOFError
      end

      unless wait_thr.value.success?
        raise "`#{@script}` execution failed"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ridgepole-0.6.5.beta14 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta13 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta12 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta11 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta10 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta9 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta8 lib/ridgepole/external_sql_executer.rb
ridgepole-0.6.5.beta7 lib/ridgepole/external_sql_executer.rb