-
Notifications
You must be signed in to change notification settings - Fork 95
/
task.go
67 lines (53 loc) · 1.77 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package store
import (
"github.com/Dataman-Cloud/swan/src/manager/raft/types"
"github.com/boltdb/bolt"
)
func withCreateTaskBucketIfNotExists(tx *bolt.Tx, appID, slotID, taskID string, fn func(bkt *bolt.Bucket) error) error {
bkt, err := createBucketIfNotExists(tx, bucketKeyStorageVersion, bucketKeyApps, []byte(appID),
bucketKeySlots, []byte(slotID), bucketKeyTasks, []byte(taskID))
if err != nil {
return err
}
return fn(bkt)
}
func WithTaskBucket(tx *bolt.Tx, appID, slotID, taskID string, fn func(bkt *bolt.Bucket) error) error {
bkt := GetTaskBucket(tx, appID, taskID, slotID)
if bkt == nil {
return ErrTaskUnknown
}
return fn(bkt)
}
func GetTasksBucket(tx *bolt.Tx, appID, slotID string) *bolt.Bucket {
return getBucket(tx, bucketKeyStorageVersion, bucketKeyApps, []byte(appID),
bucketKeySlots, []byte(slotID), bucketKeyTasks)
}
func GetTaskBucket(tx *bolt.Tx, appID, slotID, taskID string) *bolt.Bucket {
return getBucket(tx, bucketKeyStorageVersion, bucketKeyApps, []byte(appID),
bucketKeySlots, []byte(slotID), bucketKeyTasks, []byte(taskID))
}
func createTask(tx *bolt.Tx, task *types.Task) error {
return withCreateTaskBucketIfNotExists(tx, task.AppID, task.SlotID, task.ID, func(bkt *bolt.Bucket) error {
p, err := task.Marshal()
if err != nil {
return err
}
return bkt.Put(BucketKeyData, p)
})
}
func updateTask(tx *bolt.Tx, task *types.Task) error {
return WithTaskBucket(tx, task.AppID, task.SlotID, task.ID, func(bkt *bolt.Bucket) error {
p, err := task.Marshal()
if err != nil {
return err
}
return bkt.Put(BucketKeyData, p)
})
}
func removeTask(tx *bolt.Tx, appID, slotID, taskID string) error {
tasksBkt := GetTasksBucket(tx, appID, slotID)
if tasksBkt == nil {
return nil
}
return tasksBkt.DeleteBucket([]byte(taskID))
}