2023-12-01 01:52:53 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
|
|
const log_1 = require("./log");
|
|
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
|
|
const child_process_1 = require("child_process");
|
|
|
|
dotenv_1.default.config();
|
|
|
|
const log = (0, log_1.createLog)('client');
|
|
|
|
class Client {
|
|
|
|
constructor() {
|
|
|
|
this.maxJobs = 1;
|
|
|
|
this.url = 'http://localhost:3333';
|
|
|
|
this.jobs = [];
|
|
|
|
if (typeof process.env.YOLO_WEB_URL !== 'undefined') {
|
|
|
|
this.url = process.env.YOLO_WEB_URL;
|
|
|
|
}
|
|
|
|
this.cron();
|
|
|
|
}
|
|
|
|
cron() {
|
|
|
|
this.job();
|
|
|
|
setInterval(this.job.bind(this), 60 * 1000);
|
|
|
|
}
|
|
|
|
job() {
|
|
|
|
if (this.jobs.length >= this.maxJobs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.poll();
|
|
|
|
}
|
|
|
|
async poll() {
|
|
|
|
const options = {
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
const url = `${this.url}/job`;
|
|
|
|
let res;
|
2024-01-10 16:27:33 +00:00
|
|
|
let json = null;
|
2023-12-01 01:52:53 +00:00
|
|
|
try {
|
|
|
|
log.info('Checking for new jobs...');
|
|
|
|
res = await (0, node_fetch_1.default)(url, options);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(`ERROR getting ${url}`);
|
|
|
|
log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
json = await res.json();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error('ERROR parsing JSON');
|
|
|
|
log.error(err);
|
|
|
|
}
|
|
|
|
if (json === null || json.length === 0) {
|
|
|
|
log.info('No jobs found.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.claim(json[0]);
|
|
|
|
}
|
|
|
|
async claim(id) {
|
|
|
|
const options = {
|
|
|
|
method: 'POST'
|
|
|
|
};
|
|
|
|
const url = `${this.url}/job/claim/${id}`;
|
|
|
|
let res;
|
|
|
|
let json;
|
|
|
|
try {
|
|
|
|
log.info(`Claiming job ${id}...`);
|
|
|
|
res = await (0, node_fetch_1.default)(url, options);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(`ERROR posting to ${url}`);
|
|
|
|
log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
json = await res.json();
|
|
|
|
log.info(json);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error('ERROR parsing JSON');
|
|
|
|
log.error(err);
|
|
|
|
process.exit(2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.jobs.indexOf(id) === -1) {
|
|
|
|
this.jobs.push(id);
|
|
|
|
log.info(`Claimed job ${id}`);
|
|
|
|
this.train(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async train(job) {
|
|
|
|
const cmd = 'bash';
|
|
|
|
const args = ['./scripts/train.sh', JSON.stringify(job)];
|
|
|
|
const child = (0, child_process_1.spawn)(cmd, args);
|
|
|
|
child.stdout.on('data', (data) => {
|
|
|
|
const line = data.toString().trim();
|
|
|
|
if (line === '')
|
|
|
|
return;
|
|
|
|
log.info(line);
|
|
|
|
});
|
|
|
|
child.stderr.on('data', (data) => {
|
|
|
|
const line = data.toString().trim();
|
|
|
|
if (line === '')
|
|
|
|
return;
|
|
|
|
log.info(line);
|
|
|
|
});
|
|
|
|
/*child.on('close', (code : number) => {
|
|
|
|
log.info(`child process close all stdio with code ${code}`);
|
|
|
|
});*/
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
log.info(`Train job completed with code ${code}`);
|
|
|
|
if (code !== 0) {
|
|
|
|
this.fail(job.id, `Train script exited with code ${code}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
child.on('error', (err) => {
|
|
|
|
log.error('train job errored out');
|
|
|
|
log.error(err);
|
|
|
|
this.fail(job.id, err.toString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async fail(id, reason) {
|
|
|
|
const meta = {
|
|
|
|
meta: reason
|
|
|
|
};
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(meta)
|
|
|
|
};
|
|
|
|
const url = `${this.url}/job/fail/${id}`;
|
|
|
|
let res;
|
|
|
|
let json;
|
|
|
|
this.removeJob(id);
|
|
|
|
try {
|
|
|
|
log.info(`Failed job ${id}...`);
|
|
|
|
res = await (0, node_fetch_1.default)(url, options);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error(`ERROR posting to ${url}`);
|
|
|
|
log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
json = await res.json();
|
|
|
|
log.info(json);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
log.error('ERROR parsing JSON');
|
|
|
|
log.error(err);
|
|
|
|
process.exit(2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.info(json);
|
|
|
|
}
|
|
|
|
removeJob(id) {
|
|
|
|
const index = this.jobs.indexOf(id);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.jobs.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new Client();
|
|
|
|
//# sourceMappingURL=client.js.map
|