-
Notifications
You must be signed in to change notification settings - Fork 63
/
path_existence_checking_runner.go
58 lines (50 loc) · 1.81 KB
/
path_existence_checking_runner.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
package runner
import (
"context"
"os"
runner_pb "github.com/buildbarn/bb-remote-execution/pkg/proto/runner"
"github.com/buildbarn/bb-storage/pkg/util"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/emptypb"
)
type pathExistenceCheckingRunner struct {
base runner_pb.RunnerServer
readinessCheckingPathnames []string
}
// NewPathExistenceCheckingRunner creates a decorator of RunnerServer
// that is only healthy when certain paths on disk exist.
func NewPathExistenceCheckingRunner(base runner_pb.RunnerServer, readinessCheckingPathnames []string) runner_pb.RunnerServer {
return &pathExistenceCheckingRunner{
base: base,
readinessCheckingPathnames: readinessCheckingPathnames,
}
}
func (r *pathExistenceCheckingRunner) checkPathExistence(ctx context.Context) error {
for _, path := range r.readinessCheckingPathnames {
if _, err := os.Stat(path); err != nil {
return util.StatusWrapfWithCode(err, codes.Unavailable, "Path %#v", path)
}
}
return nil
}
func (r *pathExistenceCheckingRunner) CheckReadiness(ctx context.Context, request *runner_pb.CheckReadinessRequest) (*emptypb.Empty, error) {
if err := r.checkPathExistence(ctx); err != nil {
return nil, err
}
return r.base.CheckReadiness(ctx, request)
}
func (r *pathExistenceCheckingRunner) Run(ctx context.Context, request *runner_pb.RunRequest) (*runner_pb.RunResponse, error) {
response, err := r.base.Run(ctx, request)
if err != nil {
return nil, err
}
if response.ExitCode != 0 {
// Execution failues may be caused by files
// disappearing. Suppress the results in case the
// readiness check fails.
if err := r.checkPathExistence(ctx); err != nil {
return nil, util.StatusWrap(err, "One or more required files disappeared during execution")
}
}
return response, nil
}