@quatrain/worker 0.1.0
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/AbstractQueueAdapter.d.ts +8 -0
- package/lib/AbstractQueueAdapter.js +9 -0
- package/lib/Worker.d.ts +31 -0
- package/lib/Worker.js +141 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/types/QueueAdapterInterface.d.ts +4 -0
- package/lib/types/QueueAdapterInterface.js +2 -0
- package/lib/types/QueueParameters.d.ts +4 -0
- package/lib/types/QueueParameters.js +2 -0
- package/package.json +37 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { QueueParameters } from './types/QueueParameters';
|
|
2
|
+
export declare abstract class AbstractQueueAdapter {
|
|
3
|
+
protected _params: QueueParameters;
|
|
4
|
+
protected _client: any;
|
|
5
|
+
constructor(params: QueueParameters);
|
|
6
|
+
abstract send(data: any, topic: string): Promise<string>;
|
|
7
|
+
abstract listen(topic: string): any;
|
|
8
|
+
}
|
package/lib/Worker.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Core } from '@quatrain/core';
|
|
2
|
+
export declare class Worker extends Core {
|
|
3
|
+
static endpoint: string;
|
|
4
|
+
static log(message: any, src?: string): void;
|
|
5
|
+
/**
|
|
6
|
+
* Execute an external command in a promise
|
|
7
|
+
* @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
|
|
8
|
+
* @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
|
|
9
|
+
* @param string command
|
|
10
|
+
* @param array args
|
|
11
|
+
* @return Promise
|
|
12
|
+
*/
|
|
13
|
+
static execPromise: (command: string, args?: any[], cwd?: string) => Promise<any>;
|
|
14
|
+
static prepare(folder: string): void;
|
|
15
|
+
static makeFolder(folder: string): void;
|
|
16
|
+
static removeFolder(folder: string): void;
|
|
17
|
+
static downloadFile(url: string, filepath: string): Promise<unknown>;
|
|
18
|
+
static safeString(name: string): string;
|
|
19
|
+
static pushEvent(event: string, data?: {}, ts?: number): void;
|
|
20
|
+
static pushFile(filepath: string, folder: string, extension?: string): Promise<{
|
|
21
|
+
ref: string;
|
|
22
|
+
label: string | undefined;
|
|
23
|
+
worker: string;
|
|
24
|
+
size: number;
|
|
25
|
+
} | {
|
|
26
|
+
ref?: undefined;
|
|
27
|
+
label?: undefined;
|
|
28
|
+
worker?: undefined;
|
|
29
|
+
size?: undefined;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
package/lib/Worker.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Worker = void 0;
|
|
16
|
+
const core_1 = require("@quatrain/core");
|
|
17
|
+
const os_1 = __importDefault(require("os"));
|
|
18
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const axios_1 = __importDefault(require("axios"));
|
|
21
|
+
const child_process_1 = require("child_process");
|
|
22
|
+
class Worker extends core_1.Core {
|
|
23
|
+
static log(message, src = 'Worker') {
|
|
24
|
+
super.log(message, src);
|
|
25
|
+
}
|
|
26
|
+
static prepare(folder) {
|
|
27
|
+
Worker.log(`Setting up process folder ${folder}`);
|
|
28
|
+
this.removeFolder(folder);
|
|
29
|
+
fs_1.default.mkdirSync(folder);
|
|
30
|
+
fs_1.default.mkdirSync(folder + path_1.default.sep + 'images');
|
|
31
|
+
fs_1.default.mkdirSync(folder + path_1.default.sep + 'vecto');
|
|
32
|
+
}
|
|
33
|
+
static makeFolder(folder) {
|
|
34
|
+
fs_1.default.mkdirSync(folder);
|
|
35
|
+
}
|
|
36
|
+
static removeFolder(folder) {
|
|
37
|
+
if (fs_1.default.existsSync(folder)) {
|
|
38
|
+
fs_1.default.readdirSync(folder).forEach((element) => {
|
|
39
|
+
const item = path_1.default.join(folder, element);
|
|
40
|
+
if (fs_1.default.lstatSync(item).isDirectory()) {
|
|
41
|
+
this.removeFolder(item);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
fs_1.default.unlinkSync(item);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
fs_1.default.rmdirSync(folder);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
static downloadFile(url, filepath) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
try {
|
|
53
|
+
const writer = fs_1.default.createWriteStream(filepath);
|
|
54
|
+
const response = yield axios_1.default.get(url, {
|
|
55
|
+
responseType: 'stream',
|
|
56
|
+
});
|
|
57
|
+
response.data.pipe(writer);
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
writer.on('finish', resolve);
|
|
60
|
+
writer.on('error', reject);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error(`Download error: ${error.message}`);
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
static safeString(name) {
|
|
70
|
+
return name.replace(/\s+/g, '_');
|
|
71
|
+
}
|
|
72
|
+
static pushEvent(event, data = {}, ts = 0) {
|
|
73
|
+
ts = ts === Date.now() ? Date.now() + 1 : Date.now();
|
|
74
|
+
const payload = Object.assign(Object.assign({ event, worker: `Container ${os_1.default.hostname}`, os: `${os_1.default.type} ${os_1.default.release} (${os_1.default.platform} ${os_1.default.arch})` }, data), { ts });
|
|
75
|
+
axios_1.default
|
|
76
|
+
.put(Worker.endpoint, payload)
|
|
77
|
+
.then((res) => Worker.log(`Event pushed to backend: ${res.statusText}`))
|
|
78
|
+
.catch((err) => Worker.log(`Failed to push event to backend: ${err}`));
|
|
79
|
+
}
|
|
80
|
+
static pushFile(filepath, folder, extension = 'zip') {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
try {
|
|
83
|
+
const filename = filepath.split('/').pop();
|
|
84
|
+
const destination = `${folder}.${extension}`;
|
|
85
|
+
// const storage = new Storage({
|
|
86
|
+
// keyFilename: params.keyFilename,
|
|
87
|
+
// })
|
|
88
|
+
// const bucket = storage.bucket(params.storageBucket)
|
|
89
|
+
// console.log(`Uploading ${filepath} to ${destination}`)
|
|
90
|
+
// const {
|
|
91
|
+
// 0: { metadata },
|
|
92
|
+
// } = await bucket.upload(filepath, {
|
|
93
|
+
// destination,
|
|
94
|
+
// resumable: false,
|
|
95
|
+
// })
|
|
96
|
+
return {
|
|
97
|
+
ref: destination,
|
|
98
|
+
label: filename,
|
|
99
|
+
worker: `Container ${os_1.default.hostname}`,
|
|
100
|
+
size: 0, //parseInt(metadata.size),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
return {};
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.Worker = Worker;
|
|
110
|
+
Worker.endpoint = '';
|
|
111
|
+
/**
|
|
112
|
+
* Execute an external command in a promise
|
|
113
|
+
* @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
|
|
114
|
+
* @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
|
|
115
|
+
* @param string command
|
|
116
|
+
* @param array args
|
|
117
|
+
* @return Promise
|
|
118
|
+
*/
|
|
119
|
+
Worker.execPromise = (command, args = [], cwd = process.cwd()) => {
|
|
120
|
+
Worker.log(`Executing command ${command} in ${cwd} with arguments:`);
|
|
121
|
+
args.forEach((arg) => console.log(`\t${arg}`));
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
const child = (0, child_process_1.spawn)(command, args, { cwd });
|
|
124
|
+
child.stdout.on('data', (data) => {
|
|
125
|
+
console.info(`stdout: ${data}`);
|
|
126
|
+
});
|
|
127
|
+
child.stderr.on('data', (data) => {
|
|
128
|
+
console.info(`stderr: ${data}`);
|
|
129
|
+
});
|
|
130
|
+
child.on('close', (code) => {
|
|
131
|
+
if (code !== 0) {
|
|
132
|
+
console.error(`Command execution failed with code: ${code}`);
|
|
133
|
+
reject(code);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
console.info(`Command execution completed with code: ${code}`);
|
|
137
|
+
resolve(undefined);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
};
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quatrain/worker",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Worker helpers",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"author": "Quatrain Développement SAS <developers@quatrain.com>",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@tsconfig/recommended": "^1.0.1",
|
|
15
|
+
"@types/fs-extra": "^11.0.4",
|
|
16
|
+
"@types/jest": "^27.0.3",
|
|
17
|
+
"@types/object-hash": "^3.0.6",
|
|
18
|
+
"jest": "^27.4.7",
|
|
19
|
+
"jest-node-exports-resolver": "^1.1.6",
|
|
20
|
+
"trace-unhandled": "^2.0.1",
|
|
21
|
+
"ts-jest": "^27.1.2",
|
|
22
|
+
"ts-node": "^10.4.0",
|
|
23
|
+
"typescript": "^5.1.5"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@quatrain/core": "^1.0.0-beta13",
|
|
27
|
+
"axios": "^1.7.7",
|
|
28
|
+
"fs-extra": "^11.2.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "tsc && jest --verbose",
|
|
32
|
+
"build": "tsc --watch",
|
|
33
|
+
"patch": "yarn version --patch --no-git-tag-version",
|
|
34
|
+
"push": "yarn publish --access public",
|
|
35
|
+
"prepublish": "tsc"
|
|
36
|
+
}
|
|
37
|
+
}
|