-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathtask.go
53 lines (44 loc) · 1.34 KB
/
task.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
package catalog
import (
"context"
"errors"
"strings"
nanoid "github.com/matoous/go-nanoid/v2"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/kv"
"google.golang.org/protobuf/reflect/protoreflect"
)
const (
taskIDNanoLength = 20
tasksPrefix = "tasks"
)
type taskStep struct {
Name string
Func func(ctx context.Context) error
}
func TaskPath(key string) string {
return kv.FormatPath(tasksPrefix, key)
}
func NewTaskID(prefix string) string {
return prefix + nanoid.Must(taskIDNanoLength)
}
func IsTaskID(prefix, taskID string) bool {
return len(taskID) == len(prefix)+taskIDNanoLength && strings.HasPrefix(taskID, prefix)
}
func UpdateTaskStatus(ctx context.Context, kvStore kv.Store, repository *graveler.RepositoryRecord, taskID string, statusMsg protoreflect.ProtoMessage) error {
err := kv.SetMsg(ctx, kvStore, graveler.RepoPartition(repository), []byte(TaskPath(taskID)), statusMsg)
if err != nil {
return err
}
return nil
}
func GetTaskStatus(ctx context.Context, kvStore kv.Store, repository *graveler.RepositoryRecord, taskID string, statusMsg protoreflect.ProtoMessage) error {
_, err := kv.GetMsg(ctx, kvStore, graveler.RepoPartition(repository), []byte(TaskPath(taskID)), statusMsg)
if err != nil {
if errors.Is(err, kv.ErrNotFound) {
return graveler.ErrNotFound
}
return err
}
return nil
}