This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_actions.go
121 lines (109 loc) · 2.92 KB
/
task_actions.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
package main
import (
"encoding/json"
"github.com/datatogether/task_mgmt/tasks"
"net"
"net/rpc"
)
type TasksRequestAct struct {
ReqAction
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
func (TasksRequestAct) Type() string { return "TASKS_FETCH_REQUEST" }
func (TasksRequestAct) SuccessType() string { return "TASKS_FETCH_SUCCESS" }
func (TasksRequestAct) FailureType() string { return "TASKS_FETCH_FAILURE" }
func (TasksRequestAct) Parse(reqId string, data json.RawMessage) ClientRequestAction {
a := &TasksRequestAct{}
a.RequestId = reqId
a.err = json.Unmarshal(data, a)
return a
}
func (a *TasksRequestAct) Exec() (res *ClientResponse) {
conn, err := net.Dial("tcp", cfg.TasksServiceUrl)
if err != nil {
log.Info(err.Error())
return &ClientResponse{
Type: a.FailureType(),
RequestId: a.RequestId,
Error: err.Error(),
}
return
}
cli := rpc.NewClient(conn)
p := &tasks.TasksListParams{
Limit: a.PageSize,
Offset: (a.Page - 1) * a.PageSize,
}
reply := []*tasks.Task{}
if err := cli.Call("TaskRequests.List", p, &reply); err != nil {
log.Info(err.Error())
return &ClientResponse{
Type: a.FailureType(),
RequestId: a.RequestId,
Error: err.Error(),
}
return
}
return &ClientResponse{
Type: a.SuccessType(),
RequestId: a.RequestId,
Schema: "TASK_ARRAY",
Data: reply,
}
}
type TaskEnqueueAct struct {
ReqAction
Title string `json:"title"`
TaskType string `json:"taskType"`
UserId string `json:"userId"`
Params map[string]interface{} `json:"params"`
}
func (TaskEnqueueAct) Type() string { return "TASK_ENQUEUE_REQUEST" }
func (TaskEnqueueAct) SuccessType() string { return "TASK_ENQUEUE_SUCCESS" }
func (TaskEnqueueAct) FailureType() string { return "TASK_ENQUEUE_FAILURE" }
func (TaskEnqueueAct) Parse(reqId string, data json.RawMessage) ClientRequestAction {
a := &TaskEnqueueAct{}
a.RequestId = reqId
a.err = json.Unmarshal(data, a)
return a
}
func (a *TaskEnqueueAct) Exec() (res *ClientResponse) {
log.Infof("adding task %s: %s", a.TaskType, a.Title)
conn, err := net.Dial("tcp", cfg.TasksServiceUrl)
if err != nil {
log.Info(err.Error())
return &ClientResponse{
Type: a.FailureType(),
RequestId: a.RequestId,
Error: err.Error(),
}
return
}
log.Infof("successfully dialed tasks server")
cli := rpc.NewClient(conn)
p := &tasks.TasksEnqueueParams{
Title: a.Title,
Type: a.TaskType,
UserId: a.UserId,
Params: a.Params,
}
reply := &tasks.Task{}
log.Infof("enquing task")
if err := cli.Call("TaskRequests.Enqueue", p, reply); err != nil {
log.Info(err.Error())
return &ClientResponse{
Type: a.FailureType(),
RequestId: a.RequestId,
Error: err.Error(),
}
return
}
log.Infof("task enqued")
return &ClientResponse{
Type: a.SuccessType(),
RequestId: a.RequestId,
Schema: "TASK",
Data: reply,
}
}