@@ -12,6 +12,7 @@ import (
12
12
"fmt"
13
13
"time"
14
14
15
+ "github.com/google/uuid"
15
16
"github.com/tarantool/go-tarantool"
16
17
)
17
18
@@ -20,6 +21,12 @@ type Queue interface {
20
21
// Exists checks tube for existence.
21
22
// Note: it uses Eval, so user needs 'execute universe' privilege.
22
23
Exists () (bool , error )
24
+ // Identify to a shared session.
25
+ // In the queue the session has a unique UUID and many connections may
26
+ // share one logical session. Also, the consumer can reconnect to the
27
+ // existing session during the ttr time.
28
+ // To get the UUID of the current session, call the Queue.Identify(nil).
29
+ Identify (u * uuid.UUID ) (uuid.UUID , error )
23
30
// Create creates new tube with configuration.
24
31
// Note: it uses Eval, so user needs 'execute universe' privilege
25
32
// Note: you'd better not use this function in your application, cause it is
@@ -29,6 +36,8 @@ type Queue interface {
29
36
// Note: you'd better not use this function in your application, cause it is
30
37
// administrative task to create or delete queue.
31
38
Drop () error
39
+ // ReleaseAll forcibly returns all taken tasks to a ready state.
40
+ ReleaseAll () error
32
41
// Put creates new task in a tube.
33
42
Put (data interface {}) (* Task , error )
34
43
// PutWithOpts creates new task with options different from tube's defaults.
@@ -64,6 +73,8 @@ type Queue interface {
64
73
Kick (count uint64 ) (uint64 , error )
65
74
// Delete the task identified by its id.
66
75
Delete (taskId uint64 ) error
76
+ // State returns a current queue state.
77
+ State () (State , error )
67
78
// Statistic returns some statistic about queue.
68
79
Statistic () (interface {}, error )
69
80
}
@@ -79,11 +90,15 @@ type cmd struct {
79
90
take string
80
91
drop string
81
92
peek string
93
+ touch string
82
94
ack string
83
95
delete string
84
96
bury string
85
97
kick string
86
98
release string
99
+ releaseAll string
100
+ identify string
101
+ state string
87
102
statistics string
88
103
}
89
104
@@ -173,6 +188,36 @@ func (q *queue) Exists() (bool, error) {
173
188
return exist , nil
174
189
}
175
190
191
+ // Identify to a shared session.
192
+ // In the queue the session has a unique UUID and many connections may share
193
+ // one logical session. Also, the consumer can reconnect to the existing
194
+ // session during the ttr time.
195
+ // To get the UUID of the current session, call the Queue.Identify(nil).
196
+ func (q * queue ) Identify (u * uuid.UUID ) (uuid.UUID , error ) {
197
+ // Unfortunately we can't use go-tarantool/uuid here:
198
+ // https://github.com/tarantool/queue/issues/182
199
+ var args []interface {}
200
+ if u == nil {
201
+ args = []interface {}{}
202
+ } else {
203
+ if bytes , err := u .MarshalBinary (); err != nil {
204
+ return uuid.UUID {}, err
205
+ } else {
206
+ args = []interface {}{bytes }
207
+ }
208
+ }
209
+
210
+ if resp , err := q .conn .Call17 (q .cmds .identify , args ); err == nil {
211
+ if us , ok := resp .Data [0 ].(string ); ok {
212
+ return uuid .FromBytes ([]byte (us ))
213
+ } else {
214
+ return uuid.UUID {}, fmt .Errorf ("unexpected response: %v" , resp .Data )
215
+ }
216
+ } else {
217
+ return uuid.UUID {}, err
218
+ }
219
+ }
220
+
176
221
// Put data to queue. Returns task.
177
222
func (q * queue ) Put (data interface {}) (* Task , error ) {
178
223
return q .put (data )
@@ -251,6 +296,12 @@ func (q *queue) Drop() error {
251
296
return err
252
297
}
253
298
299
+ // ReleaseAll forcibly returns all taken tasks to a ready state.
300
+ func (q * queue ) ReleaseAll () error {
301
+ _ , err := q .conn .Call17 (q .cmds .releaseAll , []interface {}{})
302
+ return err
303
+ }
304
+
254
305
// Look at a task without changing its state.
255
306
func (q * queue ) Peek (taskId uint64 ) (* Task , error ) {
256
307
qd := queueData {q : q }
@@ -260,6 +311,10 @@ func (q *queue) Peek(taskId uint64) (*Task, error) {
260
311
return qd .task , nil
261
312
}
262
313
314
+ func (q * queue ) _touch (taskId uint64 , increment time.Duration ) (string , error ) {
315
+ return q .produce (q .cmds .touch , taskId , increment .Seconds ())
316
+ }
317
+
263
318
func (q * queue ) _ack (taskId uint64 ) (string , error ) {
264
319
return q .produce (q .cmds .ack , taskId )
265
320
}
@@ -312,6 +367,22 @@ func (q *queue) Delete(taskId uint64) error {
312
367
return err
313
368
}
314
369
370
+ // State returns a current queue state.
371
+ func (q * queue ) State () (State , error ) {
372
+ resp , err := q .conn .Call17 (q .cmds .state , []interface {}{})
373
+ if err != nil {
374
+ return UnknownState , err
375
+ }
376
+
377
+ if respState , ok := resp .Data [0 ].(string ); ok {
378
+ if state , ok := strToState [respState ]; ok {
379
+ return state , nil
380
+ }
381
+ return UnknownState , fmt .Errorf ("unknown state: %v" , resp .Data [0 ])
382
+ }
383
+ return UnknownState , fmt .Errorf ("unexpected response: %v" , resp .Data )
384
+ }
385
+
315
386
// Return the number of tasks in a queue broken down by task_state, and the
316
387
// number of requests broken down by the type of request.
317
388
func (q * queue ) Statistic () (interface {}, error ) {
@@ -333,11 +404,15 @@ func makeCmd(q *queue) {
333
404
take : "queue.tube." + q .name + ":take" ,
334
405
drop : "queue.tube." + q .name + ":drop" ,
335
406
peek : "queue.tube." + q .name + ":peek" ,
407
+ touch : "queue.tube." + q .name + ":touch" ,
336
408
ack : "queue.tube." + q .name + ":ack" ,
337
409
delete : "queue.tube." + q .name + ":delete" ,
338
410
bury : "queue.tube." + q .name + ":bury" ,
339
411
kick : "queue.tube." + q .name + ":kick" ,
340
412
release : "queue.tube." + q .name + ":release" ,
413
+ releaseAll : "queue.tube." + q .name + ":release_all" ,
414
+ identify : "queue.identify" ,
415
+ state : "queue.state" ,
341
416
statistics : "queue.statistics" ,
342
417
}
343
418
}
0 commit comments