-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.js
239 lines (204 loc) · 7.05 KB
/
list.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict';
var elasticsearch = require('elasticsearch');
var raven = require('raven');
var Q = require('q');
var rp = require('request-promise');
var gcloud = require('google-cloud')({
projectId: 'newsai-1166'
});
// Instantiate a elasticsearch client
var elasticSearchClient = new elasticsearch.Client({
host: 'https://newsai:[email protected]',
// log: 'trace',
rejectUnauthorized: false
});
// Initialize Google Cloud
var topicName = 'process-new-list-upload';
var subscriptionName = 'node-new-list-upload';
var pubsub = gcloud.pubsub();
var contactIdTopicName = 'process-new-contact-upload';
var publicationIdTopicName = 'process-new-publication-upload';
// Instantiate a sentry client
var sentryClient = new raven.Client('https://0366ffd1a51a4fc4881b7e7bfca378d6:[email protected]/137174');
sentryClient.patchGlobal();
// Get a Google Cloud topic
function getTopic(currentTopicName, cb) {
pubsub.createTopic(currentTopicName, function(err, topic) {
// topic already exists.
if (err && err.code === 409) {
return cb(null, pubsub.topic(currentTopicName));
}
return cb(err, topic);
});
}
function addContactIdToPubSubTopicPublish(contactIds) {
var deferred = Q.defer();
getTopic(contactIdTopicName, function(err, topic) {
if (err) {
deferred.reject(new Error(err));
console.error('Error occurred while getting pubsub topic', err);
sentryClient.captureMessage(err);
} else {
topic.publish({
data: {
Id: contactIds,
Method: 'create'
}
}, function(err) {
if (err) {
deferred.reject(new Error(err));
console.error('Error occurred while queuing background task', err);
sentryClient.captureMessage(err);
} else {
deferred.resolve(true);
}
});
}
});
return deferred.promise;
}
function addPublicationIdToPubSubTopicPublish(publicationIds) {
var deferred = Q.defer();
getTopic(publicationIdTopicName, function(err, topic) {
if (err) {
deferred.reject(new Error(err));
console.error('Error occurred while getting pubsub topic', err);
sentryClient.captureMessage(err);
} else {
topic.publish({
data: {
Id: publicationIds,
Method: 'create'
}
}, function(err) {
if (err) {
deferred.reject(new Error(err));
console.error('Error occurred while queuing background task', err);
sentryClient.captureMessage(err);
} else {
deferred.resolve(true);
}
});
}
});
return deferred.promise;
}
function addContactIdToPubSub(contactIds) {
var allPromises = [];
var contactIdsArray = contactIds.split(",");
var filteredContactIds = [];
for (var i = 0; i < contactIdsArray.length; i++) {
if (contactIdsArray[i] !== 0) {
filteredContactIds.push(contactIdsArray[i]);
}
}
var i, j, tempArray, chunk = 75;
for (i = 0, j = filteredContactIds.length; i < j; i += chunk) {
// Break array into a chunk
tempArray = filteredContactIds.slice(i, i + chunk);
var tempString = tempArray.join(',');
// Execute contact sync
var toExecute = addContactIdToPubSubTopicPublish(tempString);
allPromises.push(toExecute);
}
return Q.all(allPromises);
}
function addPublicationIdToPubSub(publicationIds) {
var allPromises = [];
var publicationIdsArray = publicationIds.split(",");
var filteredPublicationIds = [];
for (var i = 0; i < publicationIdsArray.length; i++) {
if (publicationIdsArray[i] !== 0) {
filteredPublicationIds.push(publicationIdsArray[i]);
}
}
var i, j, tempArray, chunk = 75;
for (i = 0, j = filteredPublicationIds.length; i < j; i += chunk) {
// Break array into a chunk
tempArray = filteredPublicationIds.slice(i, i + chunk);
var tempString = tempArray.join(',');
// Execute contact sync
var toExecute = addPublicationIdToPubSubTopicPublish(tempString);
allPromises.push(toExecute);
}
return Q.all(allPromises);
}
// Process a particular Twitter user
function processListUpload(data) {
var deferred = Q.defer();
addContactIdToPubSub(data.ContactId).then(function(contactStatus) {
addPublicationIdToPubSub(data.PublicationId).then(function(publicationStatus) {
deferred.resolve(publicationStatus);
}, function(error) {
console.error(error);
sentryClient.captureMessage(error);
});
}, function(error) {
console.error(error);
sentryClient.captureMessage(error);
});
return deferred.promise;
}
// Subscribe to Pub/Sub for this particular topic
function subscribe(cb) {
var subscription;
// Event handlers
function handleMessage(message) {
cb(null, message);
}
function handleError(err) {
sentryClient.captureMessage(err);
console.error(err);
}
getTopic(topicName, function(err, topic) {
if (err) {
return cb(err);
}
topic.subscribe(subscriptionName, {
autoAck: true,
reuseExisting: true
}, function(err, sub) {
if (err) {
return cb(err);
}
subscription = sub;
// Listen to and handle message and error events
subscription.on('message', handleMessage);
subscription.on('error', handleError);
console.log('Listening to ' + topicName +
' with subscription ' + subscriptionName);
});
});
// Subscription cancellation function
return function() {
if (subscription) {
// Remove event listeners
subscription.removeListener('message', handleMessage);
subscription.removeListener('error', handleError);
subscription = undefined;
}
};
}
// Begin subscription
subscribe(function(err, message) {
// Any errors received are considered fatal.
if (err) {
sentryClient.captureMessage(err);
console.error(err);
throw err;
}
console.log('Received request to process list upload ' + message.data.ListId);
processListUpload(message.data)
.then(function(status) {
rp('https://hchk.io/d01a4dde-670b-4083-b749-e1bc1079d616')
.then(function(htmlString) {
console.log('Completed execution for ' + message.data.ListId);
})
.catch(function(err) {
console.error(err);
});
}, function(error) {
sentryClient.captureMessage(error);
console.error(error);
});
});