@miso.ai/server-jobs 0.6.6-beta.0 → 0.6.6-beta.10
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/package.json +2 -2
- package/src/index.js +1 -0
- package/src/logs.js +9 -12
- package/src/process.js +30 -0
- package/src/tasks.js +13 -13
- package/src/version.js +1 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
package/src/logs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LOG_LEVEL, getLogLevelValue } from './constants.js';
|
|
2
2
|
|
|
3
|
-
export function
|
|
3
|
+
export function generateShortId() {
|
|
4
4
|
// 4-char random string
|
|
5
5
|
return Math.random().toString(36).slice(2, 6).toLowerCase();
|
|
6
6
|
}
|
|
@@ -64,23 +64,20 @@ function formatEvent({ job, task, type, level, message = '', ...event } = {}) {
|
|
|
64
64
|
|
|
65
65
|
function formatJobTag(job) {
|
|
66
66
|
if (typeof job === 'string') {
|
|
67
|
-
return `[
|
|
68
|
-
}
|
|
69
|
-
let tags = '';
|
|
70
|
-
if (job.id) {
|
|
71
|
-
tags += `[${job.id}]`;
|
|
67
|
+
return `[job=${job}]`;
|
|
72
68
|
}
|
|
69
|
+
const terms = [];
|
|
73
70
|
if (job.name) {
|
|
74
|
-
|
|
71
|
+
terms.push(job.name);
|
|
75
72
|
}
|
|
76
|
-
|
|
73
|
+
if (job.id) {
|
|
74
|
+
terms.push(job.id);
|
|
75
|
+
}
|
|
76
|
+
return terms.length ? `[job=${terms.join('/')}]` : '';
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
function formatTaskTag(task) {
|
|
80
|
-
|
|
81
|
-
return `[${task}]`;
|
|
82
|
-
}
|
|
83
|
-
return `[${task.name || task.id}]`;
|
|
80
|
+
return `[task=${typeof task === 'string' ? task : (task.name || task.id)}]`;
|
|
84
81
|
}
|
|
85
82
|
|
|
86
83
|
function defaultLogOutput(level, message) {
|
package/src/process.js
CHANGED
|
@@ -23,3 +23,33 @@ export async function spawn(command, args, { onStdout, onStderr, ...options } =
|
|
|
23
23
|
});
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
export function getMemoryUsage(child) {
|
|
28
|
+
const ps = spawn('ps', ['-o', 'pid,rss,vsz,pmem', '-p', child.pid]);
|
|
29
|
+
for await (const line of ps.stdout) {
|
|
30
|
+
const [pid, rss, vsz, pmem] = line.toString().trim().split(' ');
|
|
31
|
+
if (pid === child.pid) {
|
|
32
|
+
return { rss, vsz, pmem };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function finished(child) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
// fatal errors, like ENOENT, EACCES
|
|
41
|
+
child.on('error', reject);
|
|
42
|
+
|
|
43
|
+
child.on('close', (code, signal) => {
|
|
44
|
+
if (code === 0) {
|
|
45
|
+
resolve();
|
|
46
|
+
} else {
|
|
47
|
+
reject(new Error(`Command "${child.spawnargs.join(' ')}" failed with code ${code} and signal ${signal}`));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function handleEpipe() {
|
|
54
|
+
process.stdout.on('error', err => err.code == 'EPIPE' && process.exit(0));
|
|
55
|
+
}
|
package/src/tasks.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
1
2
|
import { LOG_LEVEL } from './constants.js';
|
|
2
|
-
import {
|
|
3
|
-
import { createLogFunction,
|
|
3
|
+
import { finished } from './process.js';
|
|
4
|
+
import { createLogFunction, generateShortId } from './logs.js';
|
|
4
5
|
import { formatDuration } from './utils.js';
|
|
5
6
|
|
|
6
7
|
export async function runJob(job, options = {}) {
|
|
@@ -10,9 +11,10 @@ export async function runJob(job, options = {}) {
|
|
|
10
11
|
|
|
11
12
|
log(`Starting job with ${tasks.length} tasks`);
|
|
12
13
|
|
|
14
|
+
const total = tasks.length;
|
|
13
15
|
let index = 0;
|
|
14
16
|
for (let task of tasks) {
|
|
15
|
-
task = normalizeTask(task, index);
|
|
17
|
+
task = normalizeTask(task, { index, total });
|
|
16
18
|
await runTask(job, task, options);
|
|
17
19
|
index++;
|
|
18
20
|
}
|
|
@@ -27,12 +29,10 @@ async function runTask(job, task, options = {}) {
|
|
|
27
29
|
const { command, args, shell } = task;
|
|
28
30
|
log(`Starting task: ${formatTaskCommand(task)}`);
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
...options,
|
|
35
|
-
});
|
|
32
|
+
const child = spawn(command, args, { shell });
|
|
33
|
+
child.stdout.on('data', data => log(parseJsonIfNecessary(data)));
|
|
34
|
+
child.stderr.on('data', data => log(LOG_LEVEL.ERROR.NAME, parseJsonIfNecessary(data)));
|
|
35
|
+
await finished(child);
|
|
36
36
|
|
|
37
37
|
const endTime = Date.now();
|
|
38
38
|
const elapsed = endTime - task.timestamp;
|
|
@@ -42,15 +42,15 @@ async function runTask(job, task, options = {}) {
|
|
|
42
42
|
function normalizeJob(job) {
|
|
43
43
|
job = { ...job, timestamp: Date.now() };
|
|
44
44
|
if (!job.id) {
|
|
45
|
-
job.id =
|
|
45
|
+
job.id = generateShortId();
|
|
46
46
|
}
|
|
47
47
|
return job;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
function normalizeTask(task, index) {
|
|
51
|
-
task = { ...task, index, timestamp: Date.now() };
|
|
50
|
+
function normalizeTask(task, { index, total } = {}) {
|
|
51
|
+
task = { ...task, index, total, timestamp: Date.now() };
|
|
52
52
|
if (!task.id) {
|
|
53
|
-
task.id =
|
|
53
|
+
task.id = generateShortId();
|
|
54
54
|
}
|
|
55
55
|
return task;
|
|
56
56
|
}
|
package/src/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '0.6.6-beta.
|
|
1
|
+
export default '0.6.6-beta.10';
|