playov2-js-utilities 0.3.9 → 0.3.14

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;
@@ -28,9 +31,11 @@ if (!PROJECT || !LOCATION) {
28
31
  * @param {Object} messageProcessingProperties - {
29
32
  * delay
30
33
  * }
34
+ * @param {String} taskId -used as de-duplication key
35
+ * @param {Date} ttl - utc date format
31
36
  * @param {String} requestId
32
37
  */
33
- async function createHttpTask(queueId, payload, messageProcessingProperties, requestId) {
38
+ async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, ttl, 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 = { httpRequest: payload };
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
- LOGGER.info(requestId, { name: response.name }, `Added task to queue ${queueId}}`)
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
  };
@@ -212,8 +212,8 @@ const host_cancelled_activity = {
212
212
  * @param {String} date
213
213
  */
214
214
  const activity_reminder = {
215
- heading: '⏰, Activity reminder!',
216
- text: 'You have a {{sport_name}} game coming up on {{date}}. Best of luck!',
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playov2-js-utilities",
3
- "version": "0.3.9",
3
+ "version": "0.3.14",
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
  }