Skip to content

Commit

Permalink
try GOT
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-Arc committed Dec 4, 2023
1 parent 23bfc07 commit 2a3c7f2
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 50 deletions.
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"express-session": "^1.17.3",
"express-static-gzip": "^2.1.7",
"express-validator": "^6.14.2",
"got": "^14.0.0",
"lowdb": "^5.0.5",
"multer": "^1.4.5-lts.1",
"node-osc": "^9.0.2",
Expand Down
64 changes: 35 additions & 29 deletions apps/server/src/services/integration-service/HttpIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import http from 'node:http';
import { URL } from 'node:url';
import got from 'got';

import { HttpSettings, HttpSubscription, HttpSubscriptionOptions, LogOrigin } from 'ontime-types';

Expand All @@ -16,11 +15,11 @@ type Action = TimerLifeCycleKey | string;
* @class
*/
export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
protected httpAgent: null | http.Agent;
// protected httpAgent: null | http.Agent;
subscriptions: HttpSubscription;

constructor() {
this.httpAgent = null;
// this.httpAgent = null;
this.subscriptions = dbModel.http.subscriptions;
}

Expand All @@ -31,7 +30,7 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
const { subscriptions, enabledOut } = config;

if (!enabledOut) {
this.httpAgent?.destroy();
// this.httpAgent?.destroy();
return {
success: false,
message: 'HTTP output disabled',
Expand All @@ -42,8 +41,8 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {

try {
// this allows re-calling the init function during runtime
this.httpAgent?.destroy();
this.httpAgent = new http.Agent({ keepAlive: true, timeout: 2000 });
// this.httpAgent?.destroy();
// this.httpAgent = new http.Agent({ keepAlive: true, timeout: 2000 });
return {
success: true,
message: `HTTP integration client ready`,
Expand All @@ -63,12 +62,12 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
}

dispatch(action: Action, state?: object) {
if (!this.httpAgent) {
return {
success: false,
message: 'Client not initialised',
};
}
// if (!this.httpAgent) {
// return {
// success: false,
// message: 'Client not initialised',
// };
// }

if (!action) {
return {
Expand All @@ -85,8 +84,8 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
if (enabled && message) {
const parsedMessage = parseTemplateNested(message, state || {});
try {
const parsedUrl = new URL(parsedMessage);
if (parsedUrl.protocol !== 'http:') {
const parsedUrl = new globalThis.URL(parsedMessage);
if (parsedUrl.protocol != 'http:') {
logger.error(LogOrigin.Tx, `HTTP Integration: Only HTTP allowed, got ${parsedUrl.protocol}`);
return {
success: false,
Expand All @@ -105,24 +104,31 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
});
}

async emit(path: URL) {
http
.get(path, { agent: this.httpAgent }, (res) => {
if (res.statusCode !== 200) {
logger.error(LogOrigin.Tx, `HTTP Error: ${res.statusCode}`);
}
res.resume();
})
.on('error', (err) => {
logger.error(LogOrigin.Tx, `HTTP integration: ${err}`);
async emit(path: globalThis.URL) {
try {
await got.get(path, {
retry: { limit: 0 },
});
} catch (err) {
logger.error(LogOrigin.Tx, `HTTP integration: ${err}`);
}
// http
// .get(path, { agent: this.httpAgent }, (res) => {
// if (res.statusCode !== 200) {
// logger.error(LogOrigin.Tx, `HTTP Error: ${res.statusCode}`);
// }
// res.resume();
// })
// .on('error', (err) => {
// logger.error(LogOrigin.Tx, `HTTP integration: ${err}`);
// });
}

shutdown() {
if (this.httpAgent) {
this.httpAgent?.destroy();
this.httpAgent = null;
}
// if (this.httpAgent) {
// this.httpAgent?.destroy();
// this.httpAgent = null;
// }
}
}

Expand Down
Loading

0 comments on commit 2a3c7f2

Please sign in to comment.