-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
160 lines (130 loc) · 3.02 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
155
156
157
158
159
160
package acp
import (
"encoding/hex"
"io"
"io/fs"
"sync"
"time"
)
type jobStatus uint8
const (
jobStatusPending = jobStatus(iota)
jobStatusPreparing
jobStatusCopying
jobStatusFinishing
jobStatusFinished
JobStatusPending = "pending"
JobStatusPreparing = "preparing"
JobStatusCopying = "copying"
JobStatusFinishing = "finishing"
JobStatusFinished = "finished"
)
var (
statusMapping = map[jobStatus]string{
jobStatusPending: JobStatusPending,
jobStatusPreparing: JobStatusPreparing,
jobStatusCopying: JobStatusCopying,
jobStatusFinishing: JobStatusFinishing,
jobStatusFinished: JobStatusFinished,
}
)
type baseJob struct {
copyer *Copyer
src *source
path string
size int64 // length in bytes for regular files; system-dependent for others
mode fs.FileMode // file mode bits
modTime time.Time // modification time
lock sync.Mutex
writeTime time.Time
status jobStatus
targets []string
successTargets []string
failedTargets map[string]error
hash []byte
}
func (j *baseJob) setStatus(s jobStatus) {
j.lock.Lock()
defer j.lock.Unlock()
j.status = s
if s == jobStatusCopying {
j.writeTime = time.Now()
}
j.copyer.submit(&EventUpdateJob{j.report()})
}
func (j *baseJob) setHash(h []byte) {
j.lock.Lock()
defer j.lock.Unlock()
j.hash = h
j.copyer.submit(&EventUpdateJob{j.report()})
}
func (j *baseJob) succes(path string) {
j.lock.Lock()
defer j.lock.Unlock()
j.successTargets = append(j.successTargets, path)
j.copyer.submit(&EventUpdateJob{j.report()})
}
func (j *baseJob) fail(path string, err error) {
j.lock.Lock()
defer j.lock.Unlock()
if j.failedTargets == nil {
j.failedTargets = make(map[string]error, 1)
}
j.failedTargets[path] = err
j.copyer.submit(&EventUpdateJob{j.report()})
}
func (j *baseJob) report() *Job {
return &Job{
Base: j.src.base,
Path: j.src.path,
Status: statusMapping[j.status],
SuccessTargets: j.successTargets,
FailTargets: j.failedTargets,
Size: j.size,
Mode: j.mode,
ModTime: j.modTime,
WriteTime: j.writeTime,
SHA256: hex.EncodeToString(j.hash),
}
}
type writeJob struct {
*baseJob
reader io.ReadCloser
size int64
ch chan struct{}
}
func newWriteJob(job *baseJob, src io.ReadCloser, size int64, needWait bool) *writeJob {
j := &writeJob{
baseJob: job,
reader: src,
size: size,
}
if needWait {
j.ch = make(chan struct{})
}
return j
}
func (wj *writeJob) done() {
wj.reader.Close()
if wj.ch != nil {
close(wj.ch)
}
}
func (wj *writeJob) wait() {
if wj.ch == nil {
return
}
<-wj.ch
}
type Job struct {
Base string `json:"base"`
Path []string `json:"path"`
Status string `json:"status"`
SuccessTargets []string `json:"success_target,omitempty"`
FailTargets map[string]error `json:"fail_target,omitempty"`
Size int64 `json:"size"`
Mode fs.FileMode `json:"mode"`
ModTime time.Time `json:"mod_time"`
WriteTime time.Time `json:"write_time"`
SHA256 string `json:"sha256"`
}