forked from wellle/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
61 lines (49 loc) · 1.44 KB
/
cleaner.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
package rmq
import "fmt"
type Cleaner struct {
connection Connection
}
func NewCleaner(connection Connection) *Cleaner {
return &Cleaner{connection: connection}
}
func (cleaner *Cleaner) Clean() error {
cleanerConnection, ok := cleaner.connection.(*redisConnection)
if !ok {
return nil
}
connectionNames := cleanerConnection.GetConnections()
for _, connectionName := range connectionNames {
connection := cleanerConnection.hijackConnection(connectionName)
if connection.Check() {
continue // skip active connections!
}
if err := CleanConnection(connection); err != nil {
return err
}
}
return nil
}
func CleanConnection(connection *redisConnection) error {
queueNames := connection.GetConsumingQueues()
for _, queueName := range queueNames {
queue, ok := connection.OpenQueue(queueName).(*redisQueue)
if !ok {
return fmt.Errorf("rmq cleaner failed to open queue %s", queueName)
}
CleanQueue(queue)
}
if !connection.Close() {
return fmt.Errorf("rmq cleaner failed to close connection %s", connection)
}
if err := connection.CloseAllQueuesInConnection(); err != nil {
return fmt.Errorf("rmq cleaner failed to close all queues %s %s", connection, err)
}
// log.Printf("rmq cleaner cleaned connection %s", connection)
return nil
}
func CleanQueue(queue *redisQueue) {
returned := queue.ReturnAllUnacked()
queue.CloseInConnection()
_ = returned
// log.Printf("rmq cleaner cleaned queue %s %d", queue, returned)
}