-
Notifications
You must be signed in to change notification settings - Fork 21
/
deliverer.go
165 lines (148 loc) · 4.99 KB
/
deliverer.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
package worker
import (
"fmt"
"github.com/MG-RAST/AWE/lib/cache"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core"
e "github.com/MG-RAST/AWE/lib/errors"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"github.com/MG-RAST/AWE/lib/shock"
"net/http"
"os"
"strings"
"time"
)
func deliverer(control chan int) {
fmt.Printf("deliverer launched, client=%s\n", core.Self.Id)
defer fmt.Printf("deliverer exiting...\n")
for {
err := deliverer_run(control)
if err != nil {
logger.Error("(deliverer) deliverer_run returned: %s", err.Error())
}
}
control <- ID_DELIVERER //we are ending
}
func deliverer_run(control chan int) (err error) { // TODO return all errors
logger.Debug(3, "deliverer_run")
// this makes sure new work is only requested when deliverer is done
defer func() { <-chanPermit }()
workunit := <-fromProcessor
if Client_mode == "offline" {
return
}
work_id := workunit.Workunit_Unique_Identifier
var work_str string
work_str, err = work_id.String()
if err != nil {
return
}
logger.Debug(3, "(deliverer_run) work_id: %s", work_str)
work_state, ok, err := workmap.Get(work_id)
if err != nil {
logger.Error("error: %s", err.Error())
return
}
if !ok {
logger.Error("(deliverer) work id %s not found", work_id)
return
}
if work_state == ID_DISCARDED {
workunit.SetState(core.WORK_STAT_DISCARDED, "workmap indicated discarded")
logger.Event(event.WORK_DISCARD, "workid="+work_str)
} else {
workmap.Set(work_id, ID_DELIVERER, "deliverer")
perfstat := workunit.WorkPerf
// post-process for works computed successfully: push output data to Shock
move_start := time.Now().UnixNano()
logger.Debug(3, "(deliverer_run) work.State: %s", workunit.State)
if workunit.State == core.WORK_STAT_COMPUTED {
shock_client := &shock.ShockClient{Host: workunit.ShockHost, Token: workunit.Info.DataToken, Debug: false}
data_moved, err := cache.UploadOutputData(workunit, shock_client)
if err != nil {
workunit.SetState(core.WORK_STAT_ERROR, "UploadOutputData failed")
logger.Error("(deliverer_run) UploadOutputData returns workid=" + work_str + ", err=" + err.Error())
workunit.Notes = append(workunit.Notes, "[deliverer#UploadOutputData]"+err.Error())
} else {
workunit.SetState(core.WORK_STAT_DONE, "")
perfstat.OutFileSize = data_moved
}
}
move_end := time.Now().UnixNano()
perfstat.DataOut = float64(move_end-move_start) / 1e9
perfstat.Deliver = int64(move_end / 1e9)
perfstat.ClientResp = perfstat.Deliver - perfstat.Checkout
perfstat.ClientId = core.Self.Id
// notify server the final process results; send perflog, stdout, and stderr if needed
// detect e.ClientNotFound
do_retry := true
retry_count := 0
for do_retry {
response, err := core.NotifyWorkunitProcessedWithLogs(workunit, perfstat, conf.PRINT_APP_MSG)
if err != nil {
logger.Error("(deliverer_run) workid=%s NotifyWorkunitProcessedWithLogs returned: %s", work_str, err.Error())
workunit.Notes = append(workunit.Notes, "[deliverer]"+err.Error())
// keep retry
} else {
error_message := strings.Join(response.Error, ",")
if strings.Contains(error_message, e.ClientNotFound) { // TODO need better method than string search. Maybe a field awe_status.
//mark this work in Current_work map as false, something needs to be done in the future
//to clean this kind of work that has been proccessed but its result can't be sent to server!
//core.Self.Current_work_false(work.Id) //server doesn't know this yet
do_retry = false
}
if response.Status == http.StatusOK {
// success, work delivered
logger.Debug(1, "work delivered successfully")
do_retry = false
} else {
logger.Error("(deliverer) response.Status not ok, workid=%s, err=%s", work_str, error_message)
}
}
if do_retry {
time.Sleep(time.Second * 60)
retry_count += 1
} else {
if retry_count > 100 { // TODO 100 ?
break
}
break
}
}
}
work_path, err := workunit.Path()
if err != nil {
return
}
// now final status report sent to server, update some local info
if workunit.State == core.WORK_STAT_DONE {
logger.Event(event.WORK_DONE, "workid="+work_str)
core.Self.Increment_total_completed()
if conf.AUTO_CLEAN_DIR && workunit.Cmd.Local == false {
go removeDirLater(work_path, conf.CLIEN_DIR_DELAY_DONE)
}
} else {
if workunit.State == core.WORK_STAT_DISCARDED {
logger.Event(event.WORK_DISCARD, "workid="+work_str)
} else {
logger.Event(event.WORK_RETURN, "workid="+work_str)
}
core.Self.Increment_total_failed(true)
if conf.AUTO_CLEAN_DIR && workunit.Cmd.Local == false {
go removeDirLater(work_path, conf.CLIEN_DIR_DELAY_FAIL)
}
}
// cleanup
err = core.Self.Current_work.Delete(work_id, true)
if err != nil {
logger.Error("Could not remove work_id %s", work_id)
}
workmap.Delete(work_id)
core.Self.Busy = false
return
}
func removeDirLater(path string, duration time.Duration) (err error) {
time.Sleep(duration)
return os.RemoveAll(path)
}