class Dtmcli::Tcc

Constants

TRANS_TYPE

Attributes

dtm[R]
dtm_url[R]
gid[RW]
id_gen[RW]

Public Class Methods

new(dtm_url, gid) click to toggle source
# File lib/dtmcli/tcc.rb, line 33
def initialize(dtm_url, gid)
  @dtm_url = dtm_url
  @gid = gid
  @id_gen = IdGenerator.new
  @dtm = Dtm.new(dtm_url)
end
tcc_global_transaction(dtm_url) { |tcc| ... } click to toggle source
# File lib/dtmcli/tcc.rb, line 9
def tcc_global_transaction(dtm_url, &block)
  gid = IdGenerator.gen_gid(dtm_url)
  tcc = Tcc.new(dtm_url, gid)

  tbody = {
    gid: gid,
    trans_type: TRANS_TYPE
  }

  begin
    tcc.dtm.prepare(tbody)

    yield tcc if block

    tcc.dtm.submit(tbody)
  rescue => e
    tcc.dtm.abort(tbody)
    return ''
  end

  return tcc.gid
end

Public Instance Methods

call_branch(body, try_url, confirm_url, cancel_url) click to toggle source
# File lib/dtmcli/tcc.rb, line 40
def call_branch(body, try_url, confirm_url, cancel_url)
  branch_id = id_gen.gen_branch_id

  dtm.register_tcc_branch({
    gid: gid,
    branch_id: branch_id,
    trans_type: TRANS_TYPE,
    status: 'prepared',
    data: body.to_json,
    try: try_url,
    confirm: confirm_url,
    cancel: cancel_url
  })

  Proxy.execute(
    :post,
    try_url,
    {
      body: body,
      params: {
        gid: gid,
        trans_type: TRANS_TYPE,
        branch_id: branch_id,
        branch_type: 'try'
      }
    }
  )
end