Class: Dtmcli::Tcc

Inherits:
Object
  • Object
show all
Defined in:
lib/dtmcli/tcc.rb

Constant Summary collapse

TRANS_TYPE =
'tcc'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dtm_url, gid) ⇒ Tcc

Returns a new instance of Tcc.



33
34
35
36
37
38
# 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

Instance Attribute Details

#dtmObject (readonly)

Returns the value of attribute dtm.



4
5
6
# File 'lib/dtmcli/tcc.rb', line 4

def dtm
  @dtm
end

#dtm_urlObject (readonly)

Returns the value of attribute dtm_url.



4
5
6
# File 'lib/dtmcli/tcc.rb', line 4

def dtm_url
  @dtm_url
end

#gidObject

Returns the value of attribute gid.



3
4
5
# File 'lib/dtmcli/tcc.rb', line 3

def gid
  @gid
end

#id_genObject

Returns the value of attribute id_gen.



3
4
5
# File 'lib/dtmcli/tcc.rb', line 3

def id_gen
  @id_gen
end

Class Method Details

.tcc_global_transaction(dtm_url, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#call_branch(body, try_url, confirm_url, cancel_url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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