1
1
'use strict' ; // eslint-disable-line
2
2
3
3
const async = require ( 'async' ) ;
4
+ const util = require ( 'util' ) ;
4
5
const { errors } = require ( 'arsenal' ) ;
5
6
6
7
const LifecycleTask = require ( './LifecycleTask' ) ;
@@ -39,33 +40,33 @@ class LifecycleTaskV2 extends LifecycleTask {
39
40
* @param {array } remainings - array of { prefix, listType, beforeDate }
40
41
* @param {object } bucketData - bucket data
41
42
* @param {Logger.newRequestLogger } log - logger object
43
+ * @param {function } done - callback(error)
42
44
* @return {undefined }
43
45
*/
44
- _handleRemainingListings ( remainings , bucketData , log ) {
45
- if ( remainings && remainings . length ) {
46
- remainings . forEach ( l => {
47
- const {
48
- prefix,
49
- listType,
50
- beforeDate,
51
- storageClass,
52
- } = l ;
53
-
54
- const entry = Object . assign ( { } , bucketData , {
55
- contextInfo : { requestId : log . getSerializedUids ( ) } ,
56
- details : { beforeDate, prefix, listType, storageClass } ,
57
- } ) ;
46
+ _handleRemainingListings ( remainings , bucketData , log , done ) {
47
+ async . forEach ( remainings || [ ] , ( l , cb ) => {
48
+ const {
49
+ prefix,
50
+ listType,
51
+ beforeDate,
52
+ storageClass,
53
+ } = l ;
54
+
55
+ const entry = Object . assign ( { } , bucketData , {
56
+ contextInfo : { requestId : log . getSerializedUids ( ) } ,
57
+ details : { beforeDate, prefix, listType, storageClass } ,
58
+ } ) ;
58
59
59
- this . _sendBucketEntry ( entry , err => {
60
- if ( ! err ) {
61
- log . debug (
62
- 'sent kafka entry for bucket consumption' , {
63
- method : 'LifecycleTaskV2._getVersionList' ,
64
- } ) ;
65
- }
66
- } ) ;
60
+ this . _sendBucketEntry ( entry , err => {
61
+ if ( ! err ) {
62
+ log . debug (
63
+ 'sent kafka entry for bucket consumption' , {
64
+ method : 'LifecycleTaskV2._getVersionList' ,
65
+ } ) ;
66
+ }
67
+ cb ( ) ;
67
68
} ) ;
68
- }
69
+ } , done ) ;
69
70
}
70
71
71
72
/**
@@ -101,15 +102,19 @@ class LifecycleTaskV2 extends LifecycleTask {
101
102
return process . nextTick ( done ) ;
102
103
}
103
104
105
+ const promises = [ ] ;
106
+
104
107
// re-queue remaining listings only once
105
108
if ( nbRetries === 0 ) {
106
- this . _handleRemainingListings ( remainings , bucketData , log ) ;
109
+ promises . push ( util . promisify ( this . _handleRemainingListings ) . bind ( this ) (
110
+ remainings , bucketData , log ,
111
+ ) ) ;
107
112
}
108
113
109
114
return this . backbeatMetadataProxy . listLifecycle ( listType , params , log ,
110
115
( err , contents , isTruncated , markerInfo ) => {
111
116
if ( err ) {
112
- return done ( err ) ;
117
+ return Promise . all ( promises ) . then ( ( ) => done ( err ) , ( ) => done ( err ) ) ;
113
118
}
114
119
115
120
// re-queue truncated listing only once.
@@ -125,17 +130,22 @@ class LifecycleTaskV2 extends LifecycleTask {
125
130
} ,
126
131
} ) ;
127
132
128
- this . _sendBucketEntry ( entry , err => {
133
+ promises . push ( new Promise ( resolve => this . _sendBucketEntry ( entry , err => {
129
134
if ( ! err ) {
130
135
log . debug (
131
136
'sent kafka entry for bucket consumption' , {
132
- method : 'LifecycleTaskV2._getObjectList' ,
133
- } ) ;
137
+ method : 'LifecycleTaskV2._getObjectList' ,
138
+ } ) ;
134
139
}
135
- } ) ;
140
+ resolve ( ) ; // safe to ignore the error, we will retry lifecycle eventually
141
+ } ) ) ) ;
136
142
}
137
- return this . _compareRulesToList ( bucketData , bucketLCRules ,
138
- contents , log , done ) ;
143
+
144
+ promises . push ( util . promisify ( this . _compareRulesToList ) . bind ( this ) (
145
+ bucketData , bucketLCRules , contents , log ,
146
+ ) ) ;
147
+
148
+ return Promise . all ( promises ) . then ( ( ) => done ( ) , done ) ;
139
149
} ) ;
140
150
}
141
151
@@ -173,15 +183,19 @@ class LifecycleTaskV2 extends LifecycleTask {
173
183
return process . nextTick ( done ) ;
174
184
}
175
185
186
+ const promises = [ ] ;
187
+
176
188
// re-queue remaining listings only once
177
189
if ( nbRetries === 0 ) {
178
- this . _handleRemainingListings ( remainings , bucketData , log ) ;
190
+ promises . push ( util . promisify ( this . _handleRemainingListings ) . bind ( this ) (
191
+ remainings , bucketData , log ,
192
+ ) ) ;
179
193
}
180
194
181
195
return this . backbeatMetadataProxy . listLifecycle ( listType , params , log ,
182
196
( err , contents , isTruncated , markerInfo ) => {
183
197
if ( err ) {
184
- return done ( err ) ;
198
+ return Promise . all ( promises ) . then ( ( ) => done ( err ) , ( ) => done ( err ) ) ;
185
199
}
186
200
187
201
// create Set of unique keys not matching the next marker to
@@ -209,19 +223,21 @@ class LifecycleTaskV2 extends LifecycleTask {
209
223
} ,
210
224
} ) ;
211
225
212
- this . _sendBucketEntry ( entry , err => {
226
+ promises . push ( new Promise ( resolve => this . _sendBucketEntry ( entry , err => {
213
227
if ( ! err ) {
214
228
log . debug (
215
229
'sent kafka entry for bucket consumption' , {
216
- method : 'LifecycleTaskV2._getObjectList ' ,
217
- } ) ;
230
+ method : 'LifecycleTaskV2._getObjectVersions ' ,
231
+ } ) ;
218
232
}
219
- } ) ;
233
+ resolve ( ) ; // safe to ignore the error, we will retry lifecycle eventually
234
+ } ) ) ) ;
220
235
}
221
- return this . _compareRulesToList ( bucketData , bucketLCRules ,
222
- contents , log , err => {
236
+
237
+ promises . push ( new Promise ( ( resolve , reject ) => this . _compareRulesToList (
238
+ bucketData , bucketLCRules , contents , log , err => {
223
239
if ( err ) {
224
- return done ( err ) ;
240
+ return reject ( err ) ;
225
241
}
226
242
227
243
if ( ! isTruncated ) {
@@ -236,8 +252,10 @@ class LifecycleTaskV2 extends LifecycleTask {
236
252
) ;
237
253
}
238
254
239
- return done ( ) ;
240
- } ) ;
255
+ return resolve ( ) ;
256
+ } ) ) ) ;
257
+
258
+ return Promise . all ( promises ) . then ( ( ) => done ( ) , done ) ;
241
259
} ) ;
242
260
}
243
261
0 commit comments