@quatrain/worker 1.1.41 → 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 +8 -0
- package/lib/Worker.js +32 -0
- package/package.json +1 -1
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
|
@@ -77,6 +77,38 @@ class Worker extends core_1.Core {
|
|
|
77
77
|
return false;
|
|
78
78
|
});
|
|
79
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
|
+
}
|
|
80
112
|
/**
|
|
81
113
|
* Global handling function to process received messages
|
|
82
114
|
* @param messageHandler function
|