feat: enhance DatabaseManagerService and QueryExecuterService with timeout settings, add AxiosPlugin for HTTP requests, and update DatabasePlugin to use query method
This commit is contained in:
54
src/vm/plugins/axios.plugin.ts
Normal file
54
src/vm/plugins/axios.plugin.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import axios from "axios";
|
||||
import { Plugin } from "../plugin.class";
|
||||
|
||||
export class AxiosPlugin extends Plugin {
|
||||
constructor(name: string) {
|
||||
super(name, ["get", "post", "put", "delete", "head", "options", "patch"]);
|
||||
}
|
||||
|
||||
static init(name: string): AxiosPlugin {
|
||||
return new AxiosPlugin(name);
|
||||
}
|
||||
|
||||
private configure(config) {
|
||||
if (config.timeout > 10e3) {
|
||||
config.timeout = 10e3;
|
||||
} else {
|
||||
config.timeout = 5e3;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
async get(url: string, config?: any): Promise<any> {
|
||||
return await axios.get(url, this.configure(config));
|
||||
}
|
||||
|
||||
async post(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.post(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
async put(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.put(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
async delete(url: string, config?: any): Promise<any> {
|
||||
return await axios.delete(url, this.configure(config));
|
||||
}
|
||||
|
||||
async head(url: string, config?: any): Promise<any> {
|
||||
return await axios.head(url, this.configure(config));
|
||||
}
|
||||
|
||||
async options(url: string, config?: any): Promise<any> {
|
||||
return await axios.options(url, this.configure(config));
|
||||
}
|
||||
|
||||
async patch(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.patch(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
// No resources to clean up
|
||||
}
|
||||
}
|
||||
@ -9,7 +9,7 @@ export class DatabasePlugin extends Plugin {
|
||||
);
|
||||
}
|
||||
|
||||
super(name, ["execute"]);
|
||||
super(name, ["query"]);
|
||||
}
|
||||
|
||||
static async init(
|
||||
@ -20,6 +20,8 @@ export class DatabasePlugin extends Plugin {
|
||||
user: string;
|
||||
password: string;
|
||||
database: string;
|
||||
idleTimeout: number;
|
||||
connectTimeout: number;
|
||||
}
|
||||
): Promise<DatabasePlugin> {
|
||||
const dbConnection = await mysql.createConnection({
|
||||
@ -28,19 +30,18 @@ export class DatabasePlugin extends Plugin {
|
||||
port: config.port,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
idleTimeout: config.idleTimeout,
|
||||
connectTimeout: config.connectTimeout,
|
||||
enableKeepAlive: true,
|
||||
});
|
||||
|
||||
await dbConnection.query("SET SESSION MAX_EXECUTION_TIME=2000;");
|
||||
|
||||
return new DatabasePlugin(name, dbConnection);
|
||||
}
|
||||
|
||||
async execute(query): Promise<any> {
|
||||
const [rows, fields] = await this.dbConnection.query(query);
|
||||
|
||||
return {
|
||||
rows: rows,
|
||||
fields: fields ?? [],
|
||||
};
|
||||
async query(query): Promise<any> {
|
||||
return await this.dbConnection.query(query);
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
|
||||
Reference in New Issue
Block a user