playov2-js-utilities 0.3.6 → 0.3.7

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 CHANGED
@@ -9,5 +9,6 @@ module.exports = {
9
9
  NotificationTemplates: require('./notification-templates'),
10
10
  NotificationConfig: require('./notification.config'),
11
11
  httpRequest: require('./request'),
12
- Cron: require('./cron')
12
+ Cron: require('./cron'),
13
+ MessagePublisher: require('./message_publisher')
13
14
  };
@@ -0,0 +1,70 @@
1
+
2
+
3
+ const { CloudTasksClient } = require('@google-cloud/tasks');
4
+
5
+ const PLAYO_LOGGER = require("@playo/logger");
6
+ const LOGGER = new PLAYO_LOGGER("playo-message-publisher");
7
+
8
+ // Instantiates a client.
9
+ const client = new CloudTasksClient();
10
+
11
+ const PROJECT = process.env.GCP_PROJECT;
12
+ const LOCATION = process.env.GCP_LOCATION;
13
+
14
+ if (!PROJECT || !LOCATION) {
15
+ console.warn('You need to pass - GCP_PROJECT, GCP_LOCATION in env for message queue to work!');
16
+ }
17
+ /**
18
+ *
19
+ * @param {String} queueId - Name of the queue being targeted
20
+ * @param {Object} payload - {
21
+ * httpMethod: String <POST, GET, PUT>
22
+ * url: String,
23
+ * headers: Object,
24
+ * body: Object, // not needed if GET request
25
+ * ...
26
+ *
27
+ * }
28
+ * @param {Object} messageProcessingProperties - {
29
+ * delay
30
+ * }
31
+ * @param {String} requestId
32
+ */
33
+ async function createHttpTask(queueId, payload, messageProcessingProperties, requestId) {
34
+
35
+ try {
36
+ const project = PROJECT;
37
+ const queue = queueId;
38
+ const location = LOCATION;
39
+
40
+ const { delay = 0 } = messageProcessingProperties
41
+
42
+ const parent = client.queuePath(project, location, queue);
43
+
44
+ const task = { httpRequest: payload };
45
+
46
+ if (payload.body) {
47
+ task.httpRequest.body = Buffer.from(payload.body).toString('base64');
48
+ }
49
+
50
+ if (delay) {
51
+ // The time when the task is scheduled to be attempted.
52
+ task.scheduleTime = {
53
+ seconds: delay + Date.now() / 1000,
54
+ };
55
+ }
56
+
57
+ // Send create task request.
58
+ LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
59
+
60
+ const request = { parent: parent, task: task };
61
+ const [response] = await client.createTask(request);
62
+ LOGGER.info(requestId, { name: response.name }, `Added task to queue ${queueId}}`)
63
+ } catch (err) {
64
+ LOGGER.error(requestId, err, err.message)
65
+ }
66
+ };
67
+
68
+ module.exports = {
69
+ createHttpTask
70
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playov2-js-utilities",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Private package for JS utility functions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "jest": "^27.1.0"
22
22
  },
23
23
  "dependencies": {
24
+ "@google-cloud/tasks": "^2.5.0",
24
25
  "@playo/logger": "^0.9.18",
25
26
  "axios": "^0.24.0",
26
27
  "cron": "^1.8.2"