-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
68 lines (57 loc) · 1.36 KB
/
info.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
package runner
import (
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"time"
"github.com/google/uuid"
)
var defaultOlderThan = time.Hour * 24 * 7
type resultSaver interface {
SaveTaskResult(TaskResult) error
}
type resultCleaner interface {
CleanTaskResult(olderThan time.Duration) error
}
type TaskResult struct {
Name string
UID string
Err error
RawResult []byte
Started time.Time
Finished time.Time
}
func NewTaskResult() *TaskResult {
uid := uuid.New().String()
return &TaskResult{Started: time.Now(), UID: uid}
}
func (r *TaskResult) String() string {
var result string
if r.RawResult != nil {
result = string(r.RawResult)
}
return fmt.Sprintf("Job: %s\nUID:%s\nDur:%f\nRes:%s\n\n", r.Name, r.UID, r.Duration().Seconds(), result)
}
func (r *TaskResult) Duration() time.Duration {
return r.Finished.Sub(r.Started)
}
func ProceedTaskResult(results chan TaskResult, saver resultSaver) {
for info := range results {
//save info
log.Infof("%+v", info)
if err := saver.SaveTaskResult(info); err != nil {
err := errors.Wrap(err, "SaveTaskResult")
log.Error(err)
}
}
}
func CleanTaskResult(cleaner resultCleaner) {
ticker := time.NewTicker(time.Hour)
for {
<-ticker.C
if err := cleaner.CleanTaskResult(defaultOlderThan); err != nil {
err := errors.Wrap(err, "CleanTaskResult")
log.Error(err)
}
}
}