-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (202 loc) · 5.7 KB
/
index.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
const dotenv = require('dotenv');
var bullQueue = require('bull')
const uuidv4 = require('uuid/v4');
const EventEmitter = require('events');
const getElasticSearchClient = require("./getElasticSearchClient");
dotenv.config({ path: process.env.ENV_PATH || '.env' });
var { REDIS_HOST = 'localhost', REDIS_PORT = 6379, ELASTIC_INDEX_NAME = "", ELASTIC_INDEX_TYPE = "", ELASTIC_SEARCH_URL = "" } = process.env;
/* example To Use package.json - 'bull': '^3.7.0', 'ioredis': '^4.14.1',
let tElasticSearch = {
URL: ELASTIC_SEARCH_URL,
indexName: ELASTIC_INDEX_NAME,
indexType: ELASTIC_INDEX_TYPE
}
let tRedisConfig = {
redis: {
port: REDIS_PORT,
host: REDIS_HOST,
db: REDIS_DB
}
}
this.mQueueToElastic = new queuetoelastic("PUSH_TO_ELASTIC_SEARCH",tRedisConfig,tElasticSearch);
import queuetoelastic from 'queuetoelastic';
mSimpleQueue.pause();
mSimpleQueue.addDataTOQueue(`xyz`,{Hello: 1234});
mSimpleQueue.addDataTOQueue(`xyz2`,{Hello: 1234});
mSimpleQueue.addDataTOQueue(`xyz3`,{Hello: 1234});
mSimpleQueue.addDataTOQueue(`xyz4`,{Hello: 1234});
{collection: "tmpCollection", insertObject: { tmp: 1234, hello: "asdfasdf" } }
setTimeout(() => {
mSimpleQueue.resume();
}, 5 * 1000);
mSimpleQueue.attachQueue((job, done) => {
console.log(job.data.data);
done();
})
*/
class queuetoelastic extends EventEmitter {
constructor(tQueueName,tRedisObject,tElasticSearch) {
super()
this.mActiveStatus = false;
this.mQueueName = tQueueName;
this.mForceStop = false;
if(typeof tQueueName === "undefined") {
throw new Error("You have t send Queue Name..");
}
if(typeof tRedisObject === "undefined") {
throw new Error("Redis Object Not Found.");
}
if(tElasticSearch && tElasticSearch.indexName && tElasticSearch.indexType )
{
this.elasticSearchURL = tElasticSearch.URL;
this.elasticSearchIndexName = tElasticSearch.indexName;
this.elasticSearchType = tElasticSearch.indexType;
}
else
{
throw new Error("Elastic Search Config Not Define.");
}
this.es = await getElasticSearchClient(this.elasticSearchURL);
console.log(`the Queue Name is ${this.mQueueName}`);
this.jobQueue = new bullQueue(this.mQueueName, tRedisObject);
this.listenerQEvents();
this.attachQueue(() => {});
}
listenerQEvents()
{
var _this = this;
this.jobQueue.on('completed', function(job, result){
console.log(`Job Completed..`);
job.remove()
})
this.jobQueue.on('error', function(error) {
// An error occured.
_this.mActiveStatus = false;
})
this.jobQueue.on('paused', function(){
console.log(`get paused`);
_this.mActiveStatus = false;
})
this.jobQueue.on('resumed', function(job){
console.log(`get resumed`);
_this.mActiveStatus = true;
})
this.jobQueue.on('active', function(job, jobPromise){
console.log(`get Active`);
})
}
attachQueue(cb)
{
var _this = this;
this.jobQueue.process(async function(job, done) {
// cb(job,done);
// console.log(JSON.stringify(job.data.data, null, 2));
let tData = {}
try {
tData = JSON.parse(job.data.data)
} catch (error) {
tData = job.data.data;
}
// console.log(JSON.stringify(tData, null, 2));
try {
let tMP = await _this.es.bulk({
body: [
{ index: { _index: _this.elasticSearchIndexName, _type: _this.elasticSearchType, _id: uuidv4() } },
tData
]
});
console.log(JSON.stringify(tMP, null, 2));
} catch (e) {
console.log(e);
}
done()
});
}
async clearQueue()
{
var _this = this;
if(this.jobQueue)
{
var clean = this.jobQueue.clean.bind(this.jobQueue, 0);
this.jobQueue.pause()
.then(clean('completed'))
.then(clean('active'))
.then(clean('delayed'))
.then(clean('failed'))
.then(function () {
_this.jobQueue.empty().then(() => {
console.info(`Queue Clear..`);;
})
}).catch((e) => {
console.error(e);
});
}
}
async count(res)
{
if(this.jobQueue)
{
this.jobQueue.count().then((count) => {
console.error(count);
}).catch((e) => {
console.error(`Queue Count get Failed e`);
})
}
}
async Stop()
{
var _this = this;
if(this.jobQueue)
{
var clean = this.jobQueue.clean.bind(this.jobQueue, 0);
this.jobQueue.pause()
.then(clean('completed'))
.then(clean('active'))
.then(clean('delayed'))
.then(clean('failed'))
.then(function () {
_this.jobQueue.empty().then(() => {
console.info(`Queue Stoped`);
})
}).then(function () {
_this.jobQueue.close().then(() => {
console.info(`Queue Stoped`);
})
});
}
}
async pause()
{
if(this.jobQueue)
{
this.jobQueue.pause().then(function(){
console.info(`Queue Pause!!`);
}).catch((e) => {
console.error(`Pause Queue Failed e`);
})
}
}
async resume()
{
if(this.jobQueue)
{
this.jobQueue.resume().then(function(){
console.info(`Queue Resume..`);
}).catch((e) => {
console.error(`Resime Error e`);
})
}
}
addDataTOQueue(_id,data)
{
if(this.jobQueue)
{
this.jobQueue.add({ "_id" : _id, "data" : data});
}
else
{
console.log("Queue is not started...");
}
}
}
module.exports = queuetoelastic;