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.



32
33
34
35
36
# File 'lib/dtmcli/tcc.rb', line 32

def initialize(dtm_url, gid)
  @dtm_url = dtm_url
  @gid = gid
  @id_gen = IdGenerator.new
end

Instance Attribute Details

#dtm_urlObject

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.



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

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
# 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
    Proxy.execute(:post, dtm_url + '/prepare', {body: tbody})
    yield tcc if block

    Proxy.execute(:post, dtm_url + '/submit', {body: tbody})
  rescue => e
    Proxy.execute(:post, dtm_url + '/abort', {body: tbody})
    return ''
  end

  return tcc.gid
end

Instance Method Details

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



38
39
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
68
69
70
71
# File 'lib/dtmcli/tcc.rb', line 38

def call_branch(body, try_url, confirm_url, cancel_url)
  branch_id = id_gen.gen_branch_id

  Proxy.execute(
    :post,
    dtm_url + '/registerTccBranch',
    {
      body: {
        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