playov2-js-utilities 0.3.8 → 0.3.13
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.
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
Read this and associated queue configs before deploying a new queue
|
|
3
|
+
https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues.tasks#resource:-task
|
|
4
|
+
*/
|
|
2
5
|
|
|
3
6
|
const { CloudTasksClient } = require('@google-cloud/tasks');
|
|
7
|
+
const { v4: uuidv4 } = require('uuid');
|
|
4
8
|
|
|
5
9
|
const PLAYO_LOGGER = require("@playo/logger");
|
|
6
10
|
const LOGGER = new PLAYO_LOGGER("playo-message-publisher");
|
|
7
11
|
|
|
8
|
-
// Instantiates a client.
|
|
9
12
|
const client = new CloudTasksClient();
|
|
10
13
|
|
|
11
14
|
const PROJECT = process.env.GCP_PROJECT;
|
|
12
15
|
const LOCATION = process.env.GCP_LOCATION;
|
|
13
16
|
|
|
17
|
+
const MAX_HTTP_TASK_TTL = 1800000 // milliseconds - half an hour
|
|
18
|
+
|
|
14
19
|
if (!PROJECT || !LOCATION) {
|
|
15
20
|
console.warn('You need to pass - GCP_PROJECT, GCP_LOCATION in env for message queue to work!');
|
|
16
21
|
}
|
|
@@ -28,9 +33,11 @@ if (!PROJECT || !LOCATION) {
|
|
|
28
33
|
* @param {Object} messageProcessingProperties - {
|
|
29
34
|
* delay
|
|
30
35
|
* }
|
|
36
|
+
* @param {String} taskId -used as de-duplication key
|
|
37
|
+
* @param {Date} ttl - utc date format
|
|
31
38
|
* @param {String} requestId
|
|
32
39
|
*/
|
|
33
|
-
async function createHttpTask(queueId, payload, messageProcessingProperties, requestId) {
|
|
40
|
+
async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, ttl, requestId) {
|
|
34
41
|
|
|
35
42
|
try {
|
|
36
43
|
const project = PROJECT;
|
|
@@ -41,7 +48,14 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
41
48
|
|
|
42
49
|
const parent = client.queuePath(project, location, queue);
|
|
43
50
|
|
|
44
|
-
const task = {
|
|
51
|
+
const task = {
|
|
52
|
+
httpRequest: payload,
|
|
53
|
+
name: ''
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
task.name = getTaskId(project, location, queue, taskId);
|
|
57
|
+
|
|
58
|
+
task.dispatchDeadline = _getHTTPTaskTTL(ttl); // nano seconds
|
|
45
59
|
|
|
46
60
|
if (payload.body) {
|
|
47
61
|
task.httpRequest.body = Buffer.from(payload.body).toString('base64');
|
|
@@ -52,19 +66,49 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
52
66
|
task.scheduleTime = {
|
|
53
67
|
seconds: delay + Date.now() / 1000,
|
|
54
68
|
};
|
|
55
|
-
}
|
|
69
|
+
};
|
|
56
70
|
|
|
57
|
-
// Send create task request.
|
|
58
71
|
LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
|
|
59
72
|
|
|
60
73
|
const request = { parent: parent, task: task };
|
|
61
74
|
const [response] = await client.createTask(request);
|
|
62
|
-
|
|
75
|
+
|
|
76
|
+
LOGGER.info(requestId, { name: response.name, response }, `Added task to queue ${queueId}}`)
|
|
63
77
|
} catch (err) {
|
|
64
78
|
LOGGER.error(requestId, err, err.message)
|
|
65
79
|
}
|
|
66
80
|
};
|
|
67
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Returns a unique string for name of a task in task queues, to be used for task de-duplication purpose.
|
|
84
|
+
* @param {String} projectId
|
|
85
|
+
* @param {String} locationId
|
|
86
|
+
* @param {String} queueId
|
|
87
|
+
* @param {String} taskId
|
|
88
|
+
* @returns {String}
|
|
89
|
+
*/
|
|
90
|
+
const getTaskId = (projectId, locationId, queueId, taskId = uuidv4()) => {
|
|
91
|
+
return `projects/${projectId}/locations/${locationId}/queues/${queueId}/tasks/${taskId}`;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Returns ttl for http tasks in nanoseconds
|
|
96
|
+
* https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues.tasks#resource:-task
|
|
97
|
+
* @param {Date} ttl
|
|
98
|
+
*/
|
|
99
|
+
const _getHTTPTaskTTL = (ttl) => {
|
|
100
|
+
|
|
101
|
+
const currentTimestamp = new Date().getTime()
|
|
102
|
+
const providedTTLTimestamp = new Date(ttl).getTime();
|
|
103
|
+
|
|
104
|
+
if (providedTTLTimestamp > currentTimestamp) {
|
|
105
|
+
return Math.min(providedTTLTimestamp, MAX_HTTP_TASK_TTL) * 1000000
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return MAX_HTTP_TASK_TTL * 1000000;
|
|
109
|
+
}
|
|
110
|
+
|
|
68
111
|
module.exports = {
|
|
69
|
-
createHttpTask
|
|
112
|
+
createHttpTask,
|
|
113
|
+
getTaskId
|
|
70
114
|
};
|
|
@@ -212,8 +212,8 @@ const host_cancelled_activity = {
|
|
|
212
212
|
* @param {String} date
|
|
213
213
|
*/
|
|
214
214
|
const activity_reminder = {
|
|
215
|
-
heading: '
|
|
216
|
-
text: 'You have a {{sport_name}} game coming up
|
|
215
|
+
heading: 'Activity Reminder',
|
|
216
|
+
text: 'You have a {{sport_name}} game coming up at {{date}}. Gear up for some fun!',
|
|
217
217
|
buttons: [],
|
|
218
218
|
notificationId: notificationIds.ACTIVITY_REMINDER
|
|
219
219
|
};
|
package/lib/request.js
CHANGED
|
@@ -17,10 +17,14 @@ const playoResponseHandler = (resp, requestId) => {
|
|
|
17
17
|
if (status === 200) {
|
|
18
18
|
const { requestStatus } = data;
|
|
19
19
|
LOGGER.info(requestId, data, `Api call to url to ${url} succeeded with status ${status}`);
|
|
20
|
-
|
|
20
|
+
if (!requestStatus) {
|
|
21
|
+
data['requestStatus'] = 1; // assume request status of 1 if any service doesn't provide requestStatus.
|
|
22
|
+
}
|
|
23
|
+
return data;
|
|
21
24
|
} else {
|
|
22
25
|
LOGGER.error(requestId, data, `Api call to url to ${url} failed with status : ${status}`);
|
|
23
|
-
|
|
26
|
+
data['requestStatus'] = 0;
|
|
27
|
+
return Promise.reject(data);
|
|
24
28
|
}
|
|
25
29
|
};
|
|
26
30
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playov2-js-utilities",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.13",
|
|
4
4
|
"description": "Private package for JS utility functions",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"@playo/logger": "^0.9.18",
|
|
26
26
|
"axios": "^0.24.0",
|
|
27
27
|
"cron": "^1.8.2",
|
|
28
|
-
"handlebars": "^4.7.7"
|
|
28
|
+
"handlebars": "^4.7.7",
|
|
29
|
+
"uuid": "^8.3.2"
|
|
29
30
|
}
|
|
30
31
|
}
|