-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
250 lines (224 loc) · 6.79 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"sort"
"os"
"strings"
"strconv"
"time"
"net/http"
"io"
log "github.com/Sirupsen/logrus"
"github.com/coreos/etcd/client"
"golang.org/x/net/context"
"errors"
"encoding/json"
"os/exec"
"syscall"
)
type Task struct {
TaskName string `json:"TaskName"`
TaskSuffix string `json:"TaskSuffix"`
TaskPath string `json:"TaskPath"`
TaskArgs []string`json:"TaskArgs"`
EtcdUrl string `json:"EtcdUrl"`
UpdateCheckUrl string `json:"UpdateCheckUrl"`
CurrentVersion int `json:"CurrentVersion,omitempty"`
CurrentProcess *os.Process `json:"CurrentProcess,omitempty"`
}
func (this *Task) StartDeamon() {
// find exist process
pId, pName, err := FindProcessIdByName(this.TaskName)
if err != nil {
log.Errorf("Find Process start with %s error : %s", this.TaskName, err.Error())
}
if strings.Contains(pName, "-") {
this.CurrentVersion, err = strconv.Atoi(strings.TrimSuffix(strings.Split(pName, "-")[1], fmt.Sprintf(".%s", this.TaskSuffix)))
if err != nil {
log.Errorf("Get version from process %s error : %s, will treat as an old one.", pName, err.Error())
}
log.Debugf("Found process %d %s(ver %d) with", pId, pName, this.CurrentVersion)
}
this.CurrentProcess, err = os.FindProcess(pId)
if err != nil {
log.Debugf("Find process by pid %d error : %s", pId, err.Error())
// start first time
err := this.Run()
if err != nil {
log.Errorf("Exec error : %s", err.Error())
this.CurrentProcess = nil
return
}
}
for true {
state, err := this.CurrentProcess.Wait()
if err != nil {
log.Errorf("Get Process state error : %s", err.Error())
}
if state.Exited() {
//process terminated
log.Debugf("Process %s(ver %d) terminated. try to start...", this.TaskName, this.CurrentVersion)
err = this.Run()
if err != nil {
log.Errorf("Exec error : %s", err.Error())
this.CurrentProcess = nil
return
}
}
}
}
func (this *Task) ConnectEtcd() error {
//
log.Debugf("CheckUpdate for %s", this.TaskName)
cfg := client.Config{
Endpoints: []string{this.EtcdUrl},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Errorf(err.Error())
}
kapi := client.NewKeysAPI(c)
watcher := kapi.Watcher("update", &client.WatcherOptions{AfterIndex:0, Recursive:false})
log.Debugf("Watching etcd(%s) endpoint", this.EtcdUrl)
for true {
response, err := watcher.Next(context.Background())
if err != nil {
log.Debugf("Etcd watcher error : %s", err.Error())
} else {
log.Debugf("Etcd got update with value : %s", response.Node.Value)
var data struct {
AppName string `json:"appname"`
Version int `json:"version"`
Md5Sum string `json:"md5sum"`
UpdateUrl string `json:"updateurl"`
}
err := json.Unmarshal([]byte(response.Node.Value), &data)
if err != nil {
log.Errorf("Update data error : %s", err.Error())
}
log.Debugf("Update data : %+v", data)
if data.AppName != this.TaskName {
log.Errorf("App name not correct local:%s update:%s)", this.TaskName, data.AppName)
} else {
//check version
log.Debugf("Current version : %d , latest version : %d", this.CurrentVersion, data.Version)
if data.Version <= this.CurrentVersion {
log.Debugf("Is updated.")
continue
}
//make new file
filePath := fmt.Sprintf("%s/%s-%d.%s", this.TaskPath, this.TaskName, data.Version, this.TaskSuffix)
log.Debugf("Create file (%s)", filePath)
file, err := os.Create(filePath)
if err != nil {
log.Errorf("Create file (%s) error : %s", filePath, err.Error())
file.Close()
continue
}
//download the latest binary file
log.Debugf("Start download from %s", data.UpdateUrl)
resp, err := http.Get(data.UpdateUrl)
log.Debugf("Status code : %d ", resp.StatusCode)
io.Copy(file, resp.Body)
if err != nil {
log.Errorf("Write file (%s) error : %s", filePath, err.Error())
file.Close()
continue
}
log.Debugf("Download succed!")
//check md5 sum
md5sum, err := FileHashMd5(filePath)
if err != nil {
log.Errorf("Get local md5 sum error :%s", err.Error())
file.Close()
continue
}
if md5sum != data.Md5Sum {
log.Errorf("Check md5 sum error local:%s expected:%s", md5sum, data.Md5Sum)
file.Close()
continue
}
file.Close()
//kill current process and the daemon will start latest version
log.Debugf("Stopping the current process...")
if this.CurrentProcess == nil {
this.StartDeamon()
} else {
this.CurrentProcess.Kill()
}
}
}
}
log.Debugf("Deamon(%s) update end. Must something wrong when checking update from etcd server.", this.TaskName)
return nil
}
func (this *Task) Run() error {
//find latest one
list, err := ListVersionsInPath(this.TaskPath, this.TaskName, this.TaskSuffix)
if err != nil {
log.Errorf("Open task path error : %s", err.Error())
return errors.New(fmt.Sprintf("Find excute(%s) failed", this.TaskName))
}
if len(list) == 0 {
log.Errorf("Task %s not found in directory : %s", this.TaskName, this.TaskPath)
return errors.New(fmt.Sprintf("Find excute(%s) failed", this.TaskName))
}
sort.Ints(list)
err = errors.New("Not started!")
retryTimes := 0
for err != nil {
tarVer := list[len(list) - retryTimes - 1]
//exec
execStr := fmt.Sprintf("%s%s-%d.%s", this.TaskPath, this.TaskName, tarVer, this.TaskSuffix)
log.Debugf("Will start process : %s", execStr)
attr := &os.ProcAttr{
Files: []*os.File{nil, nil, nil},
Sys: &syscall.SysProcAttr{
CreationFlags:syscall.CREATE_NEW_PROCESS_GROUP,
},
}
argv := append([]string{execStr}, this.TaskArgs...)
this.CurrentProcess, err = os.StartProcess(execStr, argv, attr)
this.CurrentVersion = tarVer
if err != nil {
retryTimes = retryTimes + 1
if retryTimes == len(list) - 1 {
return errors.New(fmt.Sprintf("All excute(%s) failed", this.TaskName))
}
}
}
//suc
return nil
}
/**
A high level excute with exec package
*/
func (this *Task) Exec1() error {
//find latest one
list, err := ListVersionsInPath(this.TaskPath, this.TaskName, this.TaskSuffix)
if err != nil {
return errors.New(fmt.Sprintf("Open task path error : %s", err.Error()))
}
if len(list) == 0 {
return errors.New(fmt.Sprintf("Task %s not found in directory : %s", this.TaskName, this.TaskPath))
}
sort.Ints(list)
latestVer := list[len(list) - 1]
//exec
execStr := fmt.Sprintf("%s%s-%d.%s", this.TaskPath, this.TaskName, latestVer, this.TaskSuffix)
cmd := exec.Command(execStr, this.TaskArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
this.CurrentVersion = latestVer
this.CurrentProcess = cmd.Process
cmd.Wait()
return err
}