@autofleet/node-common 1.1.25 → 1.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/events/index.js +2 -2
- package/index.js +2 -2
- package/logger/index.js +8 -17
- package/package.json +3 -5
- package/queue/index.js +105 -0
- package/monitor/index.js +0 -16
package/events/index.js
CHANGED
|
@@ -11,8 +11,8 @@ const publish = (eventType, eventObj, debug = false) => {
|
|
|
11
11
|
return 'Missing project/topic';
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
if (envv === 'development' && !debug) {
|
|
15
|
-
return 'Not publishing in dev';
|
|
14
|
+
if ((envv === 'development' || envv === 'test') && !debug) {
|
|
15
|
+
return 'Not publishing in dev/test';
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const pubsubClient = new PubSub({ projectId });
|
package/index.js
CHANGED
|
@@ -3,8 +3,7 @@ const Logger = require('./logger');
|
|
|
3
3
|
const Settings = require('./settings');
|
|
4
4
|
const client = require('./delorean-client');
|
|
5
5
|
const Router = require('./router');
|
|
6
|
-
|
|
7
|
-
require('./monitor');
|
|
6
|
+
const Queue = require('./queue');
|
|
8
7
|
|
|
9
8
|
module.exports = {
|
|
10
9
|
Network,
|
|
@@ -14,4 +13,5 @@ module.exports = {
|
|
|
14
13
|
client,
|
|
15
14
|
},
|
|
16
15
|
Router,
|
|
16
|
+
Queue,
|
|
17
17
|
};
|
package/logger/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
require('dotenv').config();
|
|
2
1
|
const winston = require('winston');
|
|
3
|
-
const { LoggingWinston } = require('@google-cloud/logging-winston');
|
|
4
2
|
|
|
5
3
|
const { createLogger } = winston;
|
|
6
|
-
|
|
4
|
+
require('dotenv').config();
|
|
7
5
|
|
|
8
6
|
const { env } = process;
|
|
9
7
|
|
|
@@ -13,7 +11,8 @@ const infoEnvNmaes = [
|
|
|
13
11
|
'test',
|
|
14
12
|
];
|
|
15
13
|
|
|
16
|
-
const getLevel = () => {
|
|
14
|
+
const getLevel = (logLevel) => {
|
|
15
|
+
if (logLevel) return logLevel;
|
|
17
16
|
if (env.LOG_LEVEL) return env.LOG_LEVEL;
|
|
18
17
|
if (infoEnvNmaes.includes(env.NODE_ENV)) return 'info';
|
|
19
18
|
if (env.NODE_ENV === 'development') return 'debug';
|
|
@@ -21,8 +20,7 @@ const getLevel = () => {
|
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
const getFormat = () => {
|
|
24
|
-
if (env.
|
|
25
|
-
// Runing on cluster
|
|
23
|
+
if (env.NODE_ENV === 'producation') {
|
|
26
24
|
return winston.format.json();
|
|
27
25
|
}
|
|
28
26
|
|
|
@@ -34,20 +32,13 @@ const getFormat = () => {
|
|
|
34
32
|
};
|
|
35
33
|
|
|
36
34
|
const createLoggerInstance = (level, transports) => {
|
|
37
|
-
const
|
|
35
|
+
const logger = createLogger({
|
|
36
|
+
level,
|
|
38
37
|
format: getFormat(),
|
|
39
|
-
transports: [
|
|
40
|
-
new winston.transports.Console(),
|
|
41
|
-
],
|
|
38
|
+
transports: [new winston.transports.Console()],
|
|
42
39
|
exceptionHandlers: transports,
|
|
43
40
|
exitOnError: false,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
if (env.AF_SERVICE_NAME) {
|
|
47
|
-
options.transports.push(loggingWinston);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const logger = createLogger(options);
|
|
41
|
+
});
|
|
51
42
|
|
|
52
43
|
return logger;
|
|
53
44
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/node-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.26",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"coverage": "jest --coverage --forceExit --runInBand",
|
|
@@ -22,9 +22,6 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://gitlab.com/AutoFleet/node-common#README",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@google-cloud/logging-winston": "^0.9.0",
|
|
26
|
-
"@google-cloud/monitoring": "^0.6.0",
|
|
27
|
-
"@google-cloud/profiler": "^0.2.0",
|
|
28
25
|
"@google-cloud/pubsub": "^0.19.0",
|
|
29
26
|
"axios": "^0.18.0",
|
|
30
27
|
"axios-retry": "^3.1.0",
|
|
@@ -34,10 +31,11 @@
|
|
|
34
31
|
"jest": "^22.4.3",
|
|
35
32
|
"mock-socket": "^7.1.0",
|
|
36
33
|
"node-cache": "^4.2.0",
|
|
34
|
+
"node-resque": "^5.4.1",
|
|
37
35
|
"portfinder": "^1.0.13",
|
|
38
36
|
"qs": "^6.5.2",
|
|
39
37
|
"timekeeper": "^2.1.2",
|
|
40
|
-
"winston": "^3.0.0",
|
|
38
|
+
"winston": "^3.0.0-rc4",
|
|
41
39
|
"ws": "^5.2.1"
|
|
42
40
|
},
|
|
43
41
|
"devDependencies": {
|
package/queue/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const NodeResque = require('node-resque');
|
|
2
|
+
const logger = require('../logger')();
|
|
3
|
+
|
|
4
|
+
const redisBaseSettings = {
|
|
5
|
+
pkg: 'ioredis',
|
|
6
|
+
host: '127.0.0.1',
|
|
7
|
+
password: '',
|
|
8
|
+
port: 6379,
|
|
9
|
+
database: 0,
|
|
10
|
+
namespace: 'resque',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
class AfQueue {
|
|
14
|
+
static redisParams(params) {
|
|
15
|
+
return Object.assign({}, redisBaseSettings, params);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
this.inited = false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async exitHandler() {
|
|
23
|
+
process.stdin.resume();
|
|
24
|
+
|
|
25
|
+
if (this.existing) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.existing = true;
|
|
30
|
+
logger.info('Will remove AfQueue');
|
|
31
|
+
if (this.scheduler) {
|
|
32
|
+
await this.scheduler.end();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (this.queue) {
|
|
36
|
+
await this.queue.end();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (this.worker) {
|
|
40
|
+
this.worker.on('end', process.exit);
|
|
41
|
+
await this.worker.end();
|
|
42
|
+
} else {
|
|
43
|
+
process.exit();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async init(connectionDetails, workerSettings, shouldCreateQueue = true) {
|
|
48
|
+
if (this.inited) {
|
|
49
|
+
throw new Error('AfQueue: Already inited');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const redisConf = Object.assign({}, redisBaseSettings, connectionDetails);
|
|
53
|
+
this.inited = true;
|
|
54
|
+
const promiseArr = [];
|
|
55
|
+
if (shouldCreateQueue) {
|
|
56
|
+
promiseArr.push(this.startScheduler(redisConf));
|
|
57
|
+
promiseArr.push(this.startQueue(redisConf));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (workerSettings) {
|
|
61
|
+
promiseArr.push(this.startWorker(redisConf, workerSettings));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
process.on('SIGINT', this.exitHandler.bind(this));
|
|
65
|
+
process.on('SIGINT', this.exitHandler.bind(this));
|
|
66
|
+
process.on('SIGTERM', this.exitHandler.bind(this));
|
|
67
|
+
|
|
68
|
+
return Promise.all(promiseArr);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async getQueue() {
|
|
72
|
+
if (!this.queue) {
|
|
73
|
+
await this.startQueue();
|
|
74
|
+
}
|
|
75
|
+
return this.queue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async startScheduler(connectionDetails) {
|
|
79
|
+
this.scheduler = new NodeResque.Scheduler({
|
|
80
|
+
connection: AfQueue.redisParams(connectionDetails),
|
|
81
|
+
});
|
|
82
|
+
await this.scheduler.connect();
|
|
83
|
+
this.scheduler.start();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async startQueue(connectionDetails) {
|
|
87
|
+
this.queue = new NodeResque.Queue({
|
|
88
|
+
connection: AfQueue.redisParams(connectionDetails),
|
|
89
|
+
});
|
|
90
|
+
await this.queue.connect();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async startWorker(connectionDetails, { queues, jobs }) {
|
|
94
|
+
this.worker = new NodeResque.Worker({
|
|
95
|
+
connection: AfQueue.redisParams(connectionDetails),
|
|
96
|
+
queues,
|
|
97
|
+
}, jobs);
|
|
98
|
+
|
|
99
|
+
await this.worker.connect();
|
|
100
|
+
this.worker.start();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
module.exports = new AfQueue();
|
package/monitor/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const monitoring = require('@google-cloud/monitoring');
|
|
2
|
-
const profiler = require('@google-cloud/profiler');
|
|
3
|
-
|
|
4
|
-
const projectId = process.env.PROJECT_ID;
|
|
5
|
-
const afServiceName = process.env.AF_SERVICE_NAME;
|
|
6
|
-
|
|
7
|
-
if (projectId) {
|
|
8
|
-
const client = new monitoring.MetricServiceClient(); // eslint-disable-line
|
|
9
|
-
|
|
10
|
-
profiler.start({
|
|
11
|
-
serviceContext: {
|
|
12
|
-
service: afServiceName,
|
|
13
|
-
version: '1.0.0',
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
}
|