Module: Swift::Adapter::Synchrony

Included in:
Mysql, Postgres
Defined in:
lib/swift/adapter/synchrony.rb

Defined Under Namespace

Classes: Mysql, Postgres

Instance Method Summary collapse

Instance Method Details

#execute(*args) {|res| ... } ⇒ Object

Execute a command asynchronously and pause the Fiber until the command finishes.

Examples:

EM.run do
  3.times.each do |n|
    EM.synchrony do
      db     = Swift.setup(:default, Swift::Adapter::Synchrony::Postgres, db: "swift_test")
      result = db.execute("select pg_sleep(3 - #{n}), #{n + 1} as qid")

      p result.first
      EM.stop if n == 0
    end
  end
end

Yields:

  • (res)

See Also:

  • Swift::Adapter::Synchrony.[Swift[Swift::Adapter]


24
25
26
27
28
29
30
31
32
# File 'lib/swift/adapter/synchrony.rb', line 24

def execute *args
  res = ::EM::Synchrony.sync super(*args)
  if res.kind_of?(Error)
    res.set_backtrace caller.reject {|subject| subject =~ %r{swift/fiber_connection_pool}}
    raise res
  end
  yield res if block_given?
  res
end

#transaction(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/swift/adapter/synchrony.rb', line 34

def transaction &block
  Swift.scopes.push(self)
  execute('begin')
  res = yield(self)
  execute('commit')
  res
rescue => e
  execute('rollback')
  raise e
ensure
  Swift.scopes.pop
end