-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
189 lines (157 loc) · 4.11 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package bridge_core
import (
"fmt"
"math/big"
"time"
"github.com/axieinfinity/bridge-core/models"
"github.com/axieinfinity/bridge-core/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)
type Job struct {
ID int32
Type int
Message []interface{}
RetryCount int32
NextTry int32
MaxTry int32
BackOff int32
Listener Listener
}
func (job Job) Hash() common.Hash {
return common.BytesToHash([]byte(fmt.Sprintf("j-%d-%d-%d", job.ID, job.RetryCount, job.NextTry)))
}
type BaseJob struct {
utilsWrapper utils.Utils
id int32
jobType int
retryCount int
maxTry int
nextTry int64
backOff int
data []byte
tx Transaction
subscriptionName string
listener Listener
fromChainID *big.Int
createdAt time.Time
}
func NewBaseJob(listener Listener, job *models.Job, transaction Transaction) (*BaseJob, error) {
chainId, err := hexutil.DecodeBig(job.FromChainId)
if err != nil {
return nil, err
}
return &BaseJob{
jobType: job.Type,
retryCount: job.RetryCount,
maxTry: 20,
nextTry: time.Now().Unix() + int64(job.RetryCount*5),
backOff: 5,
data: common.Hex2Bytes(job.Data),
tx: transaction,
subscriptionName: job.SubscriptionName,
listener: listener,
utilsWrapper: utils.NewUtils(),
fromChainID: chainId,
id: int32(job.ID),
createdAt: time.Unix(job.CreatedAt, 0),
}, nil
}
func (e *BaseJob) FromChainID() *big.Int {
return e.fromChainID
}
func (e *BaseJob) GetID() int32 {
return e.id
}
func (e *BaseJob) GetType() int {
return e.jobType
}
func (e *BaseJob) GetRetryCount() int {
return e.retryCount
}
func (e *BaseJob) GetNextTry() int64 {
return e.nextTry
}
func (e *BaseJob) GetMaxTry() int {
return e.maxTry
}
func (e *BaseJob) GetData() []byte {
return e.data
}
func (e *BaseJob) GetValue() *big.Int {
return e.tx.GetValue()
}
func (e *BaseJob) GetBackOff() int {
return e.backOff
}
func (e *BaseJob) Process() ([]byte, error) {
// save event data to database
if e.jobType == ListenHandler {
subscription, ok := e.listener.GetSubscriptions()[e.subscriptionName]
if ok {
if err := e.listener.GetStore().GetEventStore().Save(&models.Event{
EventName: subscription.Handler.Name,
TransactionHash: e.tx.GetHash().Hex(),
FromChainId: hexutil.EncodeBig(e.FromChainID()),
CreatedAt: time.Now().Unix(),
}); err != nil {
log.Error(fmt.Sprintf("[%sListenJob][Process] error while storing event to database", e.listener.GetName()), "err", err)
}
}
return e.data, nil
}
return nil, nil
}
func (e *BaseJob) String() string {
return fmt.Sprintf("{Type:%v, Subscription:%v, RetryCount: %v}", e.GetType(), e.GetSubscriptionName(), e.GetRetryCount())
}
func (e *BaseJob) Hash() common.Hash {
return common.BytesToHash([]byte(fmt.Sprintf("j-%d-%d-%d", e.id, e.retryCount, e.nextTry)))
}
func (e *BaseJob) IncreaseRetryCount() {
e.retryCount++
}
func (e *BaseJob) UpdateNextTry(nextTry int64) {
e.nextTry = nextTry
}
func (e *BaseJob) GetListener() Listener {
return e.listener
}
func (e *BaseJob) GetSubscriptionName() string {
return e.subscriptionName
}
func (e *BaseJob) GetTransaction() Transaction {
return e.tx
}
func (e *BaseJob) Utils() utils.Utils {
return e.utilsWrapper
}
func (e *BaseJob) Save(status string) error {
job := &models.Job{
Listener: e.listener.GetName(),
SubscriptionName: e.subscriptionName,
Type: e.jobType,
RetryCount: e.retryCount,
Status: status,
Data: common.Bytes2Hex(e.data),
Transaction: e.tx.GetHash().Hex(),
CreatedAt: time.Now().Unix(),
FromChainId: hexutil.EncodeBig(e.fromChainID),
}
if e.id > 0 {
job.ID = int(e.id)
}
if err := e.listener.GetStore().GetJobStore().Save(job); err != nil {
return err
}
e.id = int32(job.ID)
e.createdAt = time.Unix(job.CreatedAt, 0)
return nil
}
func (e *BaseJob) SetID(id int32) {
e.id = id
}
func (e *BaseJob) CreatedAt() time.Time {
return e.createdAt
}