-
Notifications
You must be signed in to change notification settings - Fork 1
/
murder.go
111 lines (99 loc) · 2.78 KB
/
murder.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
106
107
108
109
110
111
package murder
// Murder :
// Orchestra for queueing systems
// Provides availability via introducing multiple queues, locking and clearing
type Murder struct {
crow Crow
queueSize int
lockTTL int
workerGroupID string
queueAge int
}
// Add :
// Create a job in any queue
func (m *Murder) Add(obj interface{}) {
size := m.crow.QueueSize(m.workerGroupID)
ageConfigured := m.AgeConfigured()
var age int
if (ageConfigured) {
age = m.crow.QueueTimeSinceCreation(m.workerGroupID)
}
if size >= m.queueSize || (ageConfigured && age > m.queueAge) {
queueName := newUUID()
m.crow.MoveToReady(m.workerGroupID, queueName)
}
m.crow.AddToQueue(m.workerGroupID, obj, ageConfigured)
}
// Lock :
// Lock a queue returning a lock key that is needed for acknowledging the processing of the queue
// If no queue is ready to process, returns empty string and false
func (m *Murder) Lock() (string, bool) {
queues := m.crow.GetReadyQueues(m.workerGroupID)
for _, q := range queues {
if !m.crow.IsLocked(q) { // Queue is unlocked and can be processed
lockKey := newUUID()
ok := m.crow.CreateLockKey(q, lockKey, m.lockTTL)
if ok {
return lockKey, true
}
}
}
return "", false
}
// Get :
// Get contents of a queue given its lock key
// Ensuring the worker locked the queue and acquired the lock key
func (m *Murder) Get(lockKey string) []string {
q, ok := m.crow.FindQueueByKey(lockKey)
if ok {
return m.crow.GetQueueContents(q)
}
return []string{}
}
// Ack :
// Acknowledge processing of a queue lock extending its time to kill
// Useful for long running jobs
func (m *Murder) Ack(lockKey string) {
m.crow.ExtendLockKey(lockKey, m.lockTTL)
}
// Mark :
// Mark a locked queue as done, and its jobs disposable
func (m *Murder) Mark(lockKey string) {
q, ok := m.crow.FindQueueByKey(lockKey)
if ok {
for {
err := m.crow.ClearQueue(q, m.workerGroupID)
if err != nil {
continue
}
m.crow.RemoveLockKey(lockKey) // for cleaning up
break
}
}
}
// Unlock :
// Unlock a queue, but not marking it as done
// Useful when a worker knows it is being killed and won't be able to finish the job
func (m *Murder) Unlock(lockKey string) {
m.crow.RemoveLockKey(lockKey)
}
func (m *Murder) AgeConfigured() bool {
return (m.queueAge > 0)
}
// NewMurder :
// Returns a new instance of murder with the given options
func NewMurder(bulkSize, TTL int, crow Crow, groupID string) *Murder {
return &Murder{
crow: crow,
queueSize: bulkSize,
lockTTL: TTL,
workerGroupID: groupID,
}
}
// MurderWithAge:
// Returns a new instance of murder with extra option of queue age
func NewMurderWithAge(bulkSize, TTL int, crow Crow, groupID string, queueAge int) *Murder {
murder := NewMurder(bulkSize, TTL, crow, groupID)
murder.queueAge = queueAge
return murder
}