118 lines
3.9 KiB
TypeScript
118 lines
3.9 KiB
TypeScript
import Robot from "./Robot";
|
|
import http from 'http';
|
|
import os from 'os';
|
|
|
|
class BuildWait {
|
|
|
|
private readonly key: string;
|
|
private readonly port: number;
|
|
private readonly waitTime: number;
|
|
private readonly extendTime: number;
|
|
private readonly jobName: string;
|
|
private readonly ip: string | null;
|
|
|
|
private server: http.Server | null = null;
|
|
private timer: NodeJS.Timeout | null = null;
|
|
private time: number = 0;
|
|
private isSendedReadyMessage: boolean = false;
|
|
|
|
constructor(key: string, port: number, jobName: string, waitTime: number = 20, extendTime: number = 60) {
|
|
this.key = key;
|
|
this.port = port;
|
|
this.jobName = jobName;
|
|
this.waitTime = waitTime;
|
|
this.extendTime = extendTime;
|
|
|
|
this.ip = this.getIP();
|
|
}
|
|
|
|
private getIP(): string | null {
|
|
const networkInterfaces = os.networkInterfaces();
|
|
for (var key in networkInterfaces) {
|
|
const networkInterface = networkInterfaces[key];
|
|
if (!networkInterface) continue;
|
|
for (const info of networkInterface) {
|
|
if (info.family === 'IPv4' && info.address !== '127.0.0.1' && !info.internal) {
|
|
return info.address;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public start(): void {
|
|
if (this.server) return;
|
|
if (!this.ip) {
|
|
console.log('BuildWait IP Error');
|
|
return;
|
|
}
|
|
console.log('BuildWait Start');
|
|
|
|
this.time = this.waitTime;
|
|
|
|
this.server = http.createServer(this.requestListener.bind(this));
|
|
this.server.listen(this.port);
|
|
this.timer = setInterval(this.onTimer.bind(this), 1000);
|
|
}
|
|
|
|
private requestListener(req: http.IncomingMessage, res: http.ServerResponse): void {
|
|
const url = req.url;
|
|
console.log(`BuildWait Receive: ${url}`);
|
|
switch (url) {
|
|
case '/extend'://延迟
|
|
this.time = this.extendTime;
|
|
this.isSendedReadyMessage = false;
|
|
Robot.sendMessage(this.key, 'text', `${this.jobName}构建推迟到${this.extendTime}秒后`);
|
|
res.end('<html><script>window.close();</script></html>');
|
|
break;
|
|
case '/immediately'://立即
|
|
Robot.sendMessage(this.key, 'text', `${this.jobName}立即构建`);
|
|
this.time = 0;
|
|
res.end('<html><script>window.close();</script></html>');
|
|
break;
|
|
case '/':
|
|
default:
|
|
res.end('Commond Error: ' + url);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private onTimer(): void {
|
|
console.log('BuildWait OnTime: ' + this.time);
|
|
|
|
if (this.time <= 0) {
|
|
this.stopWait();
|
|
} else if (!this.isSendedReadyMessage && this.time <= this.waitTime) {
|
|
this.isSendedReadyMessage = true;
|
|
this.sendReadyToBuildMessage();
|
|
}
|
|
this.time--;
|
|
}
|
|
|
|
private sendReadyToBuildMessage(): void {
|
|
let message = `${this.jobName}即将在${this.waitTime}秒后开始构建\n[立即构建](http://${this.ip}:${this.port}/immediately) | [推迟构建](http://${this.ip}:${this.port}/extend) | [中断构建](http://${this.ip}:${this.port}/extend)`
|
|
Robot.sendMessage(key, 'markdown', message);
|
|
}
|
|
|
|
private stopWait(): void {
|
|
this.server?.close();
|
|
this.server = null;
|
|
this.timer && clearInterval(this.timer);
|
|
this.timer = null;
|
|
this.time = 0;
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
const args = process.argv;
|
|
const port: number = parseInt(args[2]);
|
|
const jobName: string = args[3];
|
|
const key: string = args[4]
|
|
const waitTime: number | undefined = (args[5] ? parseInt(args[5]) : undefined);
|
|
const extendTime: number | undefined = (args[6] ? parseInt(args[6]) : undefined);
|
|
|
|
const buildWait = new BuildWait(key, port, jobName, waitTime, extendTime);
|
|
buildWait.start();
|