-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCountWorker.js
More file actions
128 lines (121 loc) · 4.33 KB
/
CountWorker.js
File metadata and controls
128 lines (121 loc) · 4.33 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const assert = require('assert');
const async = require('async');
const { BucketInfo } = require('arsenal').models;
const monitoring = require('../utils/monitoring');
const { deserializeBigInts, serializeBigInts } = require('./utils/utils');
class CountWorker {
constructor(params) {
this.log = params.log;
assert(typeof params.sendFn === 'function');
this._sendFn = params.sendFn;
assert(typeof params.client === 'object');
this.client = params.client;
this.clientSetup = this.clientSetup.bind(this);
this.countItems = this.countItems.bind(this);
this.clientTeardown = this.clientTeardown.bind(this);
this.handleMessage = this.handleMessage.bind(this);
}
clientSetup(callback) {
if (this.client.client
&& this.client.client.isConnected()) {
this.log.debug('mongoclient is connected...skipping setup');
return callback();
}
return this.client.setup(callback);
}
countItems(bucketInfoObj, callback) {
if (!this.client.client) {
return callback(new Error('NotConnected'));
}
// 'fromObj' expects that the website configuration is an instance of
// WebsiteConfiguration as it is not used in CountItems, we nullify it.
if (bucketInfoObj._websiteConfiguration) {
Object.assign(bucketInfoObj, { _websiteConfiguration: null });
}
const bucketInfo = BucketInfo.fromObj(bucketInfoObj.bucketInfo || bucketInfoObj);
const bucketName = bucketInfo.getName();
this.log.info(`${process.pid} handling ${bucketName}`);
return async.waterfall([
next => this.client._getIsTransient(bucketInfo, this.log, next),
(isTransient, next) => this.client.getObjectMDStats(bucketName, bucketInfo, isTransient, this.log, next),
], (err, results) => {
monitoring.bucketsCount.inc({ status: err ? 'error' : 'success' });
callback(err, results);
});
}
clientTeardown(callback) {
if (!this.client.client) {
return callback();
}
this.log.debug('mongoclient is connected...closing client');
return this.client.close(callback);
}
handleMessage(data) {
if (data.owner !== 'scality') {
return;
}
switch (data.type) {
case 'count':
this.countItems(deserializeBigInts(data.bucketInfo), (err, results) => {
if (err) {
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'count',
status: 'failed',
error: err.message,
});
}
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'count',
status: 'passed',
results: serializeBigInts(results),
});
});
break;
case 'setup':
this.clientSetup(err => {
if (err) {
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'setup',
status: 'failed',
error: err.message,
});
}
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'setup',
status: 'passed',
});
});
break;
case 'teardown':
this.clientTeardown(err => {
if (err) {
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'teardown',
status: 'failed',
error: err.message,
});
}
return this._sendFn({
id: data.id,
owner: 'scality',
type: 'teardown',
status: 'passed',
});
});
break;
default:
break;
}
}
}
module.exports = CountWorker;