@quatrain/worker 1.1.40 → 1.1.42

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/Worker.d.ts CHANGED
@@ -20,6 +20,14 @@ export declare class Worker extends Core {
20
20
  * @returns boolean
21
21
  */
22
22
  static pushEvent(event: string, data?: {}, ts?: number): false | undefined;
23
+ /**
24
+ * Async Push an event to the backend endpoint, if available
25
+ * @param event string
26
+ * @param data
27
+ * @param ts timestamp
28
+ * @returns boolean
29
+ */
30
+ static pushEventAsync(event: string, data?: {}, ts?: number): Promise<boolean | undefined>;
23
31
  /**
24
32
  * Global handling function to process received messages
25
33
  * @param messageHandler function
package/lib/Worker.js CHANGED
@@ -69,6 +69,7 @@ class Worker extends core_1.Core {
69
69
  .patch(Worker.endpoint, payload)
70
70
  .then((res) => {
71
71
  Worker.info(`Event pushed to backend: ${res.statusText}`);
72
+ Worker.info(res.data);
72
73
  return true;
73
74
  })
74
75
  .catch((err) => {
@@ -76,6 +77,38 @@ class Worker extends core_1.Core {
76
77
  return false;
77
78
  });
78
79
  }
80
+ /**
81
+ * Async Push an event to the backend endpoint, if available
82
+ * @param event string
83
+ * @param data
84
+ * @param ts timestamp
85
+ * @returns boolean
86
+ */
87
+ static async pushEventAsync(event, data = {}, ts = 0) {
88
+ if (!this.endpoint) {
89
+ Worker.warn(`Events endpoint is not set, can't send update!`);
90
+ return false;
91
+ }
92
+ try {
93
+ ts = ts === Date.now() ? Date.now() + 1 : Date.now();
94
+ const payload = {
95
+ event,
96
+ worker: `Container ${os_1.default.hostname}`,
97
+ os: `${os_1.default.type} ${os_1.default.release} (${os_1.default.platform} ${os_1.default.arch})`,
98
+ ...data,
99
+ ts,
100
+ };
101
+ const res = await axios_1.default.patch(Worker.endpoint, payload);
102
+ if (res.statusText === 'OK') {
103
+ Worker.info(`Event pushed to backend: ${res.statusText}`);
104
+ return true;
105
+ }
106
+ }
107
+ catch (err) {
108
+ Worker.error(`Failed to push event to backend: ${err}`);
109
+ return false;
110
+ }
111
+ }
79
112
  /**
80
113
  * Global handling function to process received messages
81
114
  * @param messageHandler function
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/worker",
3
- "version": "1.1.40",
3
+ "version": "1.1.42",
4
4
  "description": "Container Worker helpers",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",