1
+ var _ = require ( 'lodash' ) ;
2
+ var client = require ( '../redis' ) ;
3
+ var queue = require ( './queue' ) ;
4
+
5
+ module . exports = {
6
+
7
+ /**
8
+ * Get a job by id
9
+ * @param {string } qName Queue name
10
+ * @param {string } id Job id
11
+ */
12
+ get : function ( qName , id ) {
13
+ var q = queue . get ( qName ) ;
14
+ return q . getJob ( id ) ;
15
+ } ,
16
+
17
+ /**
18
+ * Remove a job by id
19
+ * @param {string } qName Queue name
20
+ * @param {string } id Job id
21
+ */
22
+ remove : function ( qName , id ) {
23
+ var q = queue . get ( qName ) ;
24
+ return q . getJob ( id ) . then ( function ( job ) {
25
+ return job . remove ( ) ;
26
+ } ) ;
27
+ } ,
28
+
29
+ /**
30
+ * Get the total number of jobs of type
31
+ * @param {string } qName Queue name
32
+ * @param {string } type Job type: {wait|active|delayed|completed|failed}
33
+ * @returns {number } Total number of jobs
34
+ */
35
+ total : function ( qName , type ) {
36
+ var key = 'bull:' + qName + ':' + type ;
37
+ if ( type === 'wait' || type === 'active' ) {
38
+ return client . llenAsync ( key ) ;
39
+ } else if ( type === 'delayed' ) {
40
+ return client . zcardAsync ( key ) ;
41
+ } else if ( type === 'completed' || type === 'failed' ) {
42
+ return client . scardAsync ( key ) ;
43
+ }
44
+ throw new Error ( 'You must provide a valid job type.' ) ;
45
+ } ,
46
+
47
+ /**
48
+ * Fetch a number of jobs of certain type
49
+ * @param {string } qName Queue name
50
+ * @param {string } type Job type: {wait|active|delayed|completed|failed}
51
+ * @param {number } offset Index offset (optional)
52
+ * @param {number } limit Limit of the number of jobs returned (optional)
53
+ * @returns {Promise } A promise that resolves to an array of jobs
54
+ */
55
+ fetch : function ( qName , type , offset , limit ) {
56
+ var q = queue . get ( qName ) ;
57
+ if ( ! ( offset >= 0 ) ) {
58
+ offset = 0 ;
59
+ }
60
+ if ( ! ( limit >= 0 ) ) {
61
+ limit = 30 ;
62
+ }
63
+ if ( type === 'wait' || type === 'active' ) {
64
+ return q . getJobs ( type , 'LIST' , offset , offset + limit - 1 ) ;
65
+ } else if ( type === 'delayed' ) {
66
+ return q . getJobs ( type , 'ZSET' , offset , offset + limit - 1 ) ;
67
+ } else if ( type === 'completed' || type === 'failed' ) {
68
+ var key = 'bull:' + qName + ':' + type ;
69
+ return client . smembersAsync ( key ) . then ( function ( ids ) {
70
+ var _ids = ids . slice ( offset , offset + limit ) ;
71
+ return Promise . all ( _ . map ( _ids , function ( id ) {
72
+ return q . getJob ( id ) ;
73
+ } ) ) ;
74
+ } ) ;
75
+ }
76
+ throw new Error ( 'You must provide a valid job type.' ) ;
77
+ }
78
+
79
+ } ;
0 commit comments