-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
154 lines (136 loc) · 4.76 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
package acclient
import (
"time"
"github.com/gocql/gocql"
"github.com/subiz/header"
"github.com/subiz/idgen"
"github.com/subiz/log"
)
// CREATE TABLE account.job (accid ascii, id ascii, name text, desscription text, category text, timeout_sec bigint, created bigint, force_ended bigint, ended bigint, status text, status_updated bigint, output blob, PRIMARY KEY ((accid, id)));
func GetJob(accid, jobid string) *header.Job {
waitUntilReady()
var name, description, category, status string
var timeout_sec, created, force_ended, ended, status_updated, last_ping_ms int64
output := []byte{}
for {
err := session.Query(`SELECT name, description, category, timeout_sec, created, force_ended, ended, status, status_updated, output, last_ping_ms FROM account.job WHERE accid=? AND id=?`, accid, jobid).Scan(&name, &description, &category, &timeout_sec, &created, &force_ended, &ended, &status, &status_updated, &output, &last_ping_ms)
if err != nil && err.Error() == gocql.ErrNotFound.Error() {
return nil
}
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "jobid": jobid})
time.Sleep(5 * time.Second)
continue
}
if ended == 0 && time.Now().UnixMilli() > created+timeout_sec*1000 {
ended = created + timeout_sec*1000
if force_ended == 0 {
force_ended = ended
}
}
return &header.Job{
AccountId: accid,
Id: jobid,
Name: name,
Description: description,
Category: category,
TimeoutSec: timeout_sec,
Created: created,
ForceEnded: force_ended,
Ended: ended,
Status: status,
StatusUpdated: status_updated,
Output: output,
LastPingMs: last_ping_ms,
}
}
}
func StartJob(accid, name, description, category string, timeoutsec int64) string {
waitUntilReady()
jobid := idgen.NewJobId()
for i := 0; i < 1000; i++ {
err := session.Query(`INSERT INTO account.job(accid, id, name, description, category, timeout_sec, created) VALUES(?,?,?,?,?,?,?) USING TTL 864000`, accid, jobid, name, description, category, timeoutsec, time.Now().UnixMilli()).Exec()
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "name": name, "description": description, "category": category})
time.Sleep(30 * time.Second)
continue
}
break
}
return jobid
}
func UpdateJobStatus(accid, jobid, status string) {
waitUntilReady()
updated := time.Now().UnixMilli()
for i := 0; i < 1000; i++ {
err := session.Query(`INSERT INTO account.job(accid, id, status, status_updated) VALUES(?,?,?,?) USING TTL 864000`, accid, jobid, status, updated).Exec()
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "jobid": jobid, "status": status})
time.Sleep(30 * time.Second)
continue
}
break
}
}
// force end -> status code -5
func ForceEndJob(accid, jobid string) {
waitUntilReady()
ended := time.Now().UnixMilli()
for i := 0; i < 1000; i++ {
err := session.Query(`INSERT INTO account.job(accid, id, force_ended, ended) VALUES(?,?,?,?) USING TTL 864000`, accid, jobid, ended, ended).Exec()
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "job_id": jobid})
time.Sleep(30 * time.Second)
continue
}
break
}
}
func EndJob(accid, jobid, status string, output []byte) {
waitUntilReady()
ended := time.Now().UnixMilli()
for i := 0; i < 1000; i++ {
err := session.Query(`INSERT INTO account.job(accid, id, status, ended, output, last_ping_ms) VALUES(?,?,?,?,?,?) USING TTL 864000`, accid, jobid, status, ended, output, ended).Exec()
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "jobid": jobid, "status": status})
time.Sleep(30 * time.Second)
continue
}
break
}
}
// return ended or job status
func PingJob(accid, jobid string) string {
waitUntilReady()
ping := time.Now().UnixMilli()
var status string
var timeout_sec, created, force_ended, ended, last_ping_ms int64
for i := 0; i < 1000; i++ {
err := session.Query(`SELECT timeout_sec, created, force_ended, ended, status, last_ping_ms FROM account.job WHERE accid=? AND id=?`, accid, jobid).Scan(&timeout_sec, &created, &force_ended, &ended, &status, &last_ping_ms)
if err != nil && err.Error() == gocql.ErrNotFound.Error() {
return "ended"
}
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "jobid": jobid})
time.Sleep(5 * time.Second)
continue
}
if ended == 0 && time.Now().UnixMilli() > created+timeout_sec*1000 {
ended = created + timeout_sec*1000
if force_ended == 0 {
force_ended = ended
}
}
err = session.Query(`INSERT INTO account.job(accid, id, last_ping_ms) VALUES(?,?,?) USING TTL 864000`, accid, jobid, ping).Exec()
if err != nil {
log.ERetry(err, log.M{"account_id": accid, "jobid": jobid})
time.Sleep(30 * time.Second)
continue
}
break
}
if ended > 0 {
return "ended"
}
return status
}