playov2-js-utilities 0.3.39 → 0.3.41
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/lib/index.js +1 -3
- package/package.json +1 -2
- package/lib/models/index.js +0 -3
- package/lib/models/scheduled.tasks.js +0 -40
- package/lib/taskscheduler/index.js +0 -104
package/lib/index.js
CHANGED
|
@@ -11,7 +11,5 @@ module.exports = {
|
|
|
11
11
|
httpRequest: require('./request'),
|
|
12
12
|
Cron: require('./cron'),
|
|
13
13
|
MessagePublisher: require('./message_publisher'),
|
|
14
|
-
middleware: require('./middleware')
|
|
15
|
-
taskScheduler: require('./taskscheduler'),
|
|
16
|
-
Models: require('./models')
|
|
14
|
+
middleware: require('./middleware')
|
|
17
15
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playov2-js-utilities",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.41",
|
|
4
4
|
"description": "Private package for JS utility functions",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
"googleapis": "^109.0.1",
|
|
30
30
|
"handlebars": "^4.7.7",
|
|
31
31
|
"moment": "^2.29.4",
|
|
32
|
-
"mongoose": "^6.7.4",
|
|
33
32
|
"uuid": "^8.3.2"
|
|
34
33
|
}
|
|
35
34
|
}
|
package/lib/models/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
const mongoose = require('mongoose');
|
|
2
|
-
const Schema = mongoose.Schema;
|
|
3
|
-
|
|
4
|
-
// compound index on queuId and taskId and status, so that a task is saved only once per queue
|
|
5
|
-
const scheduledTaskSchema = new Schema({
|
|
6
|
-
httpMethod: {
|
|
7
|
-
type: String,
|
|
8
|
-
enum: ["GET", "POST", "PUT", "DELETE"],
|
|
9
|
-
default: "POST",
|
|
10
|
-
},
|
|
11
|
-
taskId: { type: String, required: true },
|
|
12
|
-
queueId: { type: String, required: true },
|
|
13
|
-
url: {
|
|
14
|
-
type: String,
|
|
15
|
-
required: true,
|
|
16
|
-
},
|
|
17
|
-
ttl: {},
|
|
18
|
-
payload: {
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
activationTime: {
|
|
22
|
-
type: Date, required: true
|
|
23
|
-
},
|
|
24
|
-
cloudProvider: {
|
|
25
|
-
type: String,
|
|
26
|
-
enum: ['GCP', 'AWS'],
|
|
27
|
-
required: true
|
|
28
|
-
},
|
|
29
|
-
status: {
|
|
30
|
-
type: String,
|
|
31
|
-
enum: ['saved', 'executed', 'failed', 'cancelled'],
|
|
32
|
-
default: 'saved',
|
|
33
|
-
required: true
|
|
34
|
-
},
|
|
35
|
-
lastExecutionDetails: {
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
module.exports = mongoose.model('scheduledtasks', scheduledTaskSchema);
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// methods - save future task - producers
|
|
3
|
-
// push future task to queue -- from cron service
|
|
4
|
-
// cancel future task -- producers
|
|
5
|
-
|
|
6
|
-
const SCHEDULED_TASK = require("../models/scheduled.tasks");
|
|
7
|
-
const CloudTask = require("../message_publisher/index");
|
|
8
|
-
const Moment = require("moment");
|
|
9
|
-
const Logger = require('../logger');
|
|
10
|
-
|
|
11
|
-
const MAX_RETRY_COUNT = 5;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @param {String} message
|
|
16
|
-
* @param {String} name - Optional - If "RetryableException", then function will be retried till MAX_RETRY_COUNT
|
|
17
|
-
*/
|
|
18
|
-
function CTException(message, name) {
|
|
19
|
-
const error = new Error(message);
|
|
20
|
-
error.name = name;
|
|
21
|
-
return error;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Currently, for cloudtask -
|
|
26
|
-
* Maximum schedule time for a task - 30 days, that means any task that is to be executed after 30 days
|
|
27
|
-
* can't be scheduled by GCP cloudtask queues.
|
|
28
|
-
* This function will provide a way for every microservices to save a task to mongo scheduledtasks model,
|
|
29
|
-
* from which tasks will be pushed via cron service to cloud task push queue on an every day basis.
|
|
30
|
-
* RetryableException - Network exceptions
|
|
31
|
-
*/
|
|
32
|
-
const saveFutureTask = async (queueId, taskId, payload, eta, count = MAX_RETRY_COUNT, requestId) => {
|
|
33
|
-
try {
|
|
34
|
-
if (!queueId || !taskId || !payload || !eta) {
|
|
35
|
-
throw CTException('Parameter validation failed!', 'ValidationException')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (Moment(eta).isBefore(Moment().add(30, 'days'))) {
|
|
39
|
-
throw CTException('eta can not be before 30 days from now. Schedule directly!', 'ValidationException')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const { url, httpMethod } = payload;
|
|
43
|
-
const taskData = {
|
|
44
|
-
taskId,
|
|
45
|
-
url,
|
|
46
|
-
httpMethod,
|
|
47
|
-
payload,
|
|
48
|
-
queueId,
|
|
49
|
-
activationTime: eta,
|
|
50
|
-
status: 'saved',
|
|
51
|
-
cloudProvider: 'GCP'
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const task = new SCHEDULED_TASK(taskData);
|
|
55
|
-
|
|
56
|
-
await task.save();
|
|
57
|
-
|
|
58
|
-
} catch (err) {
|
|
59
|
-
const errMessage = err && err.message ? err.message : 'Exception';
|
|
60
|
-
// in case there's an exception which shouldn't be retried, such as improper parameter, this throws the error to be
|
|
61
|
-
// handled by the originating calling function
|
|
62
|
-
if (errMessage !== "RetryableException") {
|
|
63
|
-
throw err;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// This is a mechanism to handle unexpected network failure / disconnection to db.
|
|
67
|
-
// This seems lame though, probably should pair with set timeout for exponential back off.
|
|
68
|
-
if (count === 0) {
|
|
69
|
-
Logger.prepareAlertLog(requestId, { func: 'saveFutureTask', queueId, taskId, payload, eta, err }, 'saving future task failed!');
|
|
70
|
-
throw err;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return saveFutureTask(queueId, taskId, payload, eta, count--, requestId);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const deleteScheduledTask = async (queueId, taskId, count = MAX_RETRY_COUNT, requestId) => {
|
|
78
|
-
try {
|
|
79
|
-
if (!queueId || !taskId) {
|
|
80
|
-
throw CTException('Parameter validation failed!', 'ValidationException')
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
await Promise.all([
|
|
84
|
-
SCHEDULED_TASK.findOneAndUpdate({ queueId, taskId }, { $set: { status: 'cancelled' } }),
|
|
85
|
-
CloudTask.deleteHttpTask(queueId, taskId)
|
|
86
|
-
]);
|
|
87
|
-
|
|
88
|
-
} catch (err) {
|
|
89
|
-
if (err.message !== "RetryableException") {
|
|
90
|
-
throw err;
|
|
91
|
-
}
|
|
92
|
-
if (count === 0) {
|
|
93
|
-
Logger.prepareAlertLog(requestId, { func: 'deleteScheduledTask', queueId, taskId, payload, eta, err }, 'saving future task failed!');
|
|
94
|
-
throw err;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return deleteScheduledTask(queueId, taskId, count--, requestId);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
module.exports = {
|
|
102
|
-
saveFutureTask,
|
|
103
|
-
deleteScheduledTask
|
|
104
|
-
}
|