playov2-js-utilities 0.3.11 → 0.3.15
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,11 +1,14 @@
|
|
|
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;
|
|
@@ -22,15 +25,17 @@ if (!PROJECT || !LOCATION) {
|
|
|
22
25
|
* url: String,
|
|
23
26
|
* headers: Object,
|
|
24
27
|
* body: Object, // not needed if GET request
|
|
28
|
+
* ttl: Optional, date in utc format, consumer will check if the ttl has been exceeded or not and then will respond accordingly
|
|
25
29
|
* ...
|
|
26
30
|
*
|
|
27
31
|
* }
|
|
28
32
|
* @param {Object} messageProcessingProperties - {
|
|
29
33
|
* delay
|
|
30
34
|
* }
|
|
35
|
+
* @param {String} taskId -used as de-duplication key
|
|
31
36
|
* @param {String} requestId
|
|
32
37
|
*/
|
|
33
|
-
async function createHttpTask(queueId, payload, messageProcessingProperties, requestId) {
|
|
38
|
+
async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, requestId) {
|
|
34
39
|
|
|
35
40
|
try {
|
|
36
41
|
const project = PROJECT;
|
|
@@ -41,7 +46,12 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
41
46
|
|
|
42
47
|
const parent = client.queuePath(project, location, queue);
|
|
43
48
|
|
|
44
|
-
const task = {
|
|
49
|
+
const task = {
|
|
50
|
+
httpRequest: payload,
|
|
51
|
+
name: ''
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
task.name = getTaskId(project, location, queue, taskId);
|
|
45
55
|
|
|
46
56
|
if (payload.body) {
|
|
47
57
|
task.httpRequest.body = Buffer.from(payload.body).toString('base64');
|
|
@@ -52,19 +62,32 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
52
62
|
task.scheduleTime = {
|
|
53
63
|
seconds: delay + Date.now() / 1000,
|
|
54
64
|
};
|
|
55
|
-
}
|
|
65
|
+
};
|
|
56
66
|
|
|
57
|
-
// Send create task request.
|
|
58
67
|
LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
|
|
59
68
|
|
|
60
69
|
const request = { parent: parent, task: task };
|
|
61
70
|
const [response] = await client.createTask(request);
|
|
62
|
-
|
|
71
|
+
|
|
72
|
+
LOGGER.info(requestId, { name: response.name, response }, `Added task to queue ${queueId}}`)
|
|
63
73
|
} catch (err) {
|
|
64
74
|
LOGGER.error(requestId, err, err.message)
|
|
65
75
|
}
|
|
66
76
|
};
|
|
67
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Returns a unique string for name of a task in task queues, to be used for task de-duplication purpose.
|
|
80
|
+
* @param {String} projectId
|
|
81
|
+
* @param {String} locationId
|
|
82
|
+
* @param {String} queueId
|
|
83
|
+
* @param {String} taskId
|
|
84
|
+
* @returns {String}
|
|
85
|
+
*/
|
|
86
|
+
const getTaskId = (projectId, locationId, queueId, taskId = uuidv4()) => {
|
|
87
|
+
return `projects/${projectId}/locations/${locationId}/queues/${queueId}/tasks/${taskId}`;
|
|
88
|
+
};
|
|
89
|
+
|
|
68
90
|
module.exports = {
|
|
69
|
-
createHttpTask
|
|
91
|
+
createHttpTask,
|
|
92
|
+
getTaskId
|
|
70
93
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playov2-js-utilities",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
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
|
}
|