forked from pachyderm/pachyderm
/
gc.go
80 lines (75 loc) · 1.92 KB
/
gc.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
package server
import (
"context"
"time"
"github.com/gogo/protobuf/types"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
"github.com/pachyderm/pachyderm/src/server/pps/persist"
"go.pedge.io/lion/proto"
)
var (
// DefaultGCPolicy is the default GC policy used by a pipeline if one is not
// specified.
DefaultGCPolicy = &ppsclient.GCPolicy{
// a day
Success: &types.Duration{
Seconds: 24 * 60 * 60,
},
// a week
Failure: &types.Duration{
Seconds: 7 * 24 * 60 * 60,
},
}
)
func (a *apiServer) runGC(ctx context.Context, pipelineInfo *ppsclient.PipelineInfo) {
dSuccess, _ := types.DurationFromProto(pipelineInfo.GcPolicy.Success)
dFailure, _ := types.DurationFromProto(pipelineInfo.GcPolicy.Failure)
successTick := time.Tick(dSuccess)
failureTick := time.Tick(dFailure)
// wait blocks until it's time to run GC again
wait := func() {
select {
case <-successTick:
case <-failureTick:
}
}
for {
client, err := a.getPersistClient()
if err != nil {
protolion.Errorf("error getting persist client: %s", err)
wait()
continue
}
jobIDs, err := client.ListGCJobs(ctx, &persist.ListGCJobsRequest{
PipelineName: pipelineInfo.Pipeline.Name,
GcPolicy: pipelineInfo.GcPolicy,
})
if err != nil {
protolion.Errorf("error listing jobs to GC: %s", err)
wait()
continue
}
for _, jobID := range jobIDs.Jobs {
jobID := jobID
go func() {
jobInfo, err := client.InspectJob(ctx, &ppsclient.InspectJobRequest{
Job: &ppsclient.Job{ID: jobID},
})
if err != nil {
protolion.Errorf("error deleting job: %s", err)
return
}
if err := a.deleteJob(ctx, jobInfo); err != nil {
protolion.Errorf("error deleting job: %s", err)
return
}
if _, err := client.GCJob(ctx, &ppsclient.Job{jobID}); err != nil {
protolion.Errorf("error marking job %s as GC-ed: %s", jobID, err)
}
return
}()
}
wait()
}
panic("unreachable")
}