mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 06:59:09 +00:00
reuse agent and test url compatibility
This commit is contained in:
@@ -9,6 +9,8 @@ import { dbModel } from '../../models/dataModel.js';
|
|||||||
import { validateHttpObject } from '../../utils/parserFunctions.js';
|
import { validateHttpObject } from '../../utils/parserFunctions.js';
|
||||||
import { logger } from '../../classes/Logger.js';
|
import { logger } from '../../classes/Logger.js';
|
||||||
|
|
||||||
|
import { URL } from 'node:url';
|
||||||
|
|
||||||
type Action = TimerLifeCycleKey | string;
|
type Action = TimerLifeCycleKey | string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,10 +18,11 @@ type Action = TimerLifeCycleKey | string;
|
|||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
export class HttpIntegration implements IIntegration {
|
export class HttpIntegration implements IIntegration {
|
||||||
|
protected httpAgent: null | http.Agent;
|
||||||
subscriptions: Subscription;
|
subscriptions: Subscription;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// this.httpClient = null;
|
this.httpAgent = null;
|
||||||
this.subscriptions = dbModel.http.subscriptions;
|
this.subscriptions = dbModel.http.subscriptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,15 +36,18 @@ export class HttpIntegration implements IIntegration {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// this allows re-calling the init function during runtime
|
// this allows re-calling the init function during runtime
|
||||||
// this.httpClient?.close();
|
this.httpAgent?.destroy();
|
||||||
|
// this.httpAgent = new http.Agent({ keepAlive: true, timeout: 2000, maxSockets: 5, maxFreeSockets: 40 });
|
||||||
|
//TODO: find the correct settings
|
||||||
|
this.httpAgent = new http.Agent({ keepAlive: true, timeout: 1000, maxFreeSockets: 1, maxSockets: 1});
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: `HTTP integration client`,
|
message: `HTTP integration client ready`,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: `Failed initialising HTTP: ${error}`,
|
message: `Failed initialising HTTP integration: ${error}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +59,7 @@ export class HttpIntegration implements IIntegration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispatch(action: Action, state?: object) {
|
dispatch(action: Action, state?: object) {
|
||||||
if (false) {
|
if (!this.httpAgent) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Client not initialised',
|
message: 'Client not initialised',
|
||||||
@@ -74,14 +80,19 @@ export class HttpIntegration implements IIntegration {
|
|||||||
const { enabled, message } = sub;
|
const { enabled, message } = sub;
|
||||||
if (enabled && message) {
|
if (enabled && message) {
|
||||||
const parsedMessage = parseTemplateNested(message, state || {});
|
const parsedMessage = parseTemplateNested(message, state || {});
|
||||||
this.emit(parsedMessage);
|
try {
|
||||||
|
const parsedUrl = new URL(parsedMessage);
|
||||||
|
this.emit(parsedUrl);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(LogOrigin.Tx, `HTTP Integration: ${err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(path: string) {
|
emit(path: URL) {
|
||||||
http
|
http
|
||||||
.get(path, (res) => {
|
.get(path, { agent: this.httpAgent }, (res) => {
|
||||||
if (res.statusCode < 300) {
|
if (res.statusCode < 300) {
|
||||||
res.resume();
|
res.resume();
|
||||||
return {
|
return {
|
||||||
@@ -103,10 +114,10 @@ export class HttpIntegration implements IIntegration {
|
|||||||
|
|
||||||
shutdown() {
|
shutdown() {
|
||||||
console.log('Shutting down HTTP integration');
|
console.log('Shutting down HTTP integration');
|
||||||
// if (this.httpClient) {
|
if (this.httpAgent) {
|
||||||
// // this.httpClient?.close();
|
this.httpAgent?.destroy();
|
||||||
// this.httpClient = null;
|
this.httpAgent = null;
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user