-
Notifications
You must be signed in to change notification settings - Fork 179
/
task.go
86 lines (73 loc) · 2.25 KB
/
task.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
package infura
import (
"encoding/json"
"github.com/okex/exchain/x/infura/types"
)
type TaskConst int
const (
TaskStatusInvalid TaskConst = 0 + iota
TaskStatusSuccess
TaskStatusStatusFail
)
const (
// Phase 1 task status
TaskPhase1NextActionRestart TaskConst = 100 + iota
TaskPhase1NextActionJumpNextBlock
TaskPhase1NextActionNewTask
TaskPhase1NextActionReturnTask
TaskPhase1NextActionUnknown
// Phase 2 task status
TaskPhase2NextActionRestart TaskConst = 200 + iota
TaskPhase2NextActionJumpNextBlock
)
var TaskConstDesc = map[TaskConst]string{
TaskStatusInvalid: "STREAM_TASK_STATUS_INVALID",
TaskStatusSuccess: "STREAM_TASK_STATUS_SUCCESS",
TaskStatusStatusFail: "STREAM_TASK_STATUS_FAIL",
TaskPhase1NextActionRestart: "STREAM_TASK_PHRASE1_NEXT_ACTION_RESTART",
TaskPhase1NextActionJumpNextBlock: "STREAM_TASK_PHRASE1_NEXT_ACTION_JUMP_NEXT_BLK",
TaskPhase1NextActionNewTask: "STREAM_TASK_PHRASE1_NEXT_ACTION_NEW_TASK",
TaskPhase1NextActionReturnTask: "STREAM_TASK_PHRASE1_NEXT_ACTION_RERUN_TASK",
TaskPhase1NextActionUnknown: "STREAM_TASK_PHRASE1_NEXT_ACTION_UNKNOWN",
TaskPhase2NextActionRestart: "STREAM_TASK_PHRASE2_NEXT_ACTION_RESTART",
TaskPhase2NextActionJumpNextBlock: "STREAM_TASK_PHRASE2_NEXT_ACTION_JUMP_NEXT_BLK",
}
type Task struct {
Height int64 `json:"height"`
Done bool `json:"done"`
UpdatedAt int64 `json:"updatedAt"`
Data types.IStreamData `json:"-"`
}
func newTask(blockHeight int64, cache *Cache) *Task {
return &Task{
Height: blockHeight,
Done: false,
Data: getStreamData(cache),
}
}
func getStreamData(cache *Cache) types.IStreamData {
return types.StreamData{
TransactionReceipts: cache.GetTransactionReceipts(),
Block: cache.GetBlock(),
Transactions: cache.GetTransactions(),
ContractCodes: cache.GetContractCodes(),
}
}
func parseTaskFromJSON(s string) (*Task, error) {
st := Task{}
e := json.Unmarshal([]byte(s), &st)
return &st, e
}
func (t *Task) toJSON() string {
r, err := json.Marshal(t)
if err != nil {
panic(err)
}
return string(r)
}
func (t *Task) GetStatus() TaskConst {
if t.Done {
return TaskStatusSuccess
}
return TaskStatusStatusFail
}