This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
forked from flier/curator.go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete.go
105 lines (75 loc) · 2.4 KB
/
delete.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package curator
import (
"github.com/samuel/go-zookeeper/zk"
)
type deleteBuilder struct {
client *curatorFramework
backgrounding backgrounding
deletingChildrenIfNeeded bool
version int32
}
func (b *deleteBuilder) ForPath(givenPath string) error {
adjustedPath := b.client.fixForNamespace(givenPath, false)
if b.backgrounding.inBackground {
go b.pathInBackground(adjustedPath, givenPath)
return nil
} else {
return b.pathInForeground(adjustedPath, givenPath)
}
}
func (b *deleteBuilder) pathInBackground(path string, givenPath string) {
tracer := b.client.ZookeeperClient().StartTracer("deleteBuilder.pathInBackground")
defer tracer.Commit()
err := b.pathInForeground(path, givenPath)
if b.backgrounding.callback != nil {
event := &curatorEvent{
eventType: DELETE,
err: err,
path: b.client.unfixForNamespace(path),
context: b.backgrounding.context,
}
if err != nil {
event.path = givenPath
}
event.name = GetNodeFromPath(event.path)
b.backgrounding.callback(b.client, event)
}
}
func (b *deleteBuilder) pathInForeground(path string, givenPath string) error {
zkClient := b.client.ZookeeperClient()
_, err := zkClient.NewRetryLoop().CallWithRetry(func() (interface{}, error) {
conn, err := zkClient.Conn()
if err == nil {
err = conn.Delete(path, b.version)
if err == zk.ErrNotEmpty && b.deletingChildrenIfNeeded {
err = DeleteChildren(conn, path, true)
}
}
return nil, err
})
return err
}
func (b *deleteBuilder) DeletingChildrenIfNeeded() DeleteBuilder {
b.deletingChildrenIfNeeded = true
return b
}
func (b *deleteBuilder) WithVersion(version int32) DeleteBuilder {
b.version = version
return b
}
func (b *deleteBuilder) InBackground() DeleteBuilder {
b.backgrounding = backgrounding{inBackground: true}
return b
}
func (b *deleteBuilder) InBackgroundWithContext(context interface{}) DeleteBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context}
return b
}
func (b *deleteBuilder) InBackgroundWithCallback(callback BackgroundCallback) DeleteBuilder {
b.backgrounding = backgrounding{inBackground: true, callback: callback}
return b
}
func (b *deleteBuilder) InBackgroundWithCallbackAndContext(callback BackgroundCallback, context interface{}) DeleteBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context, callback: callback}
return b
}