add retry count

This commit is contained in:
arc-alex
2023-12-05 21:28:37 +01:00
parent 0523a68ef4
commit d0191ebc26
5 changed files with 21 additions and 5 deletions
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { Switch } from '@chakra-ui/react'; import { Input, Switch } from '@chakra-ui/react';
import type { HttpSettings } from 'ontime-types'; import type { HttpSettings } from 'ontime-types';
import { TimerLifeCycle } from 'ontime-types'; import { TimerLifeCycle } from 'ontime-types';
@@ -49,6 +49,7 @@ export default function HttpIntegration() {
try { try {
const newSettings: HttpSettings = { const newSettings: HttpSettings = {
enabledOut: Boolean(values.enabledOut), enabledOut: Boolean(values.enabledOut),
retryCount: 0,
subscriptions: { subscriptions: {
onLoad: values.subscriptions.onLoad ?? [], onLoad: values.subscriptions.onLoad ?? [],
onStart: values.subscriptions.onStart ?? [], onStart: values.subscriptions.onStart ?? [],
@@ -79,6 +80,14 @@ export default function HttpIntegration() {
</div> </div>
<Switch {...register('enabledOut')} variant='ontime-on-light' /> <Switch {...register('enabledOut')} variant='ontime-on-light' />
</div> </div>
<div className={styles.splitSection}>
<div>
<span className={`${styles.sectionTitle} ${styles.main}`}>Retry count</span>
<span className={styles.sectionSubtitle}>How many times should the connection be retried</span>
</div>
<Input size='xs' variant='ontime-filled-on-light' autoComplete='off' {...register('retryCount')} />
</div>
<HttpSubscriptionRow <HttpSubscriptionRow
cycle={TimerLifeCycle.onLoad} cycle={TimerLifeCycle.onLoad}
title={sectionText.onLoad.title} title={sectionText.onLoad.title}
+1
View File
@@ -59,6 +59,7 @@ export const dbModel: DatabaseModel = {
}, },
http: { http: {
enabledOut: false, enabledOut: false,
retryCount: 0,
subscriptions: { subscriptions: {
onLoad: [], onLoad: [],
onStart: [], onStart: [],
@@ -17,7 +17,7 @@ type Action = TimerLifeCycleKey | string;
export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> { export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
// protected httpAgent: null | http.Agent; // protected httpAgent: null | http.Agent;
subscriptions: HttpSubscription; subscriptions: HttpSubscription;
private retryCount = 0;
constructor() { constructor() {
// this.httpAgent = null; // this.httpAgent = null;
this.subscriptions = dbModel.http.subscriptions; this.subscriptions = dbModel.http.subscriptions;
@@ -27,7 +27,9 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
* Initializes httpClient * Initializes httpClient
*/ */
init(config: HttpSettings) { init(config: HttpSettings) {
const { subscriptions, enabledOut } = config; const { subscriptions, enabledOut, retryCount } = config;
this.retryCount = retryCount;
if (!enabledOut) { if (!enabledOut) {
// this.httpAgent?.destroy(); // this.httpAgent?.destroy();
@@ -102,12 +104,12 @@ export class HttpIntegration implements IIntegration<HttpSubscriptionOptions> {
if (method === 'GET') { if (method === 'GET') {
await got.get(path, { await got.get(path, {
searchParams: options, searchParams: options,
retry: { limit: 0 }, retry: { limit: this.retryCount },
}); });
} else if (method === 'POST') { } else if (method === 'POST') {
await got.post(path, { await got.post(path, {
body: options, body: options,
retry: { limit: 0 }, retry: { limit: this.retryCount },
}); });
} }
} catch (err) { } catch (err) {
+3
View File
@@ -280,8 +280,11 @@ export const parseHttp = (data: { http?: Partial<HttpSettings> }): HttpSettings
? loadedConfig.subscriptions ? loadedConfig.subscriptions
: dbModel.http.subscriptions; : dbModel.http.subscriptions;
const retryCount = Math.max(0, loadedConfig.retryCount ?? dbModel.http.retryCount);
return { return {
enabledOut: loadedConfig.enabledOut ?? dbModel.http.enabledOut, enabledOut: loadedConfig.enabledOut ?? dbModel.http.enabledOut,
retryCount: retryCount,
subscriptions: validatedSubscriptions, subscriptions: validatedSubscriptions,
}; };
} }
@@ -6,5 +6,6 @@ export type HttpSubscription = Subscription<HttpSubscriptionOptions>;
export interface HttpSettings { export interface HttpSettings {
enabledOut: boolean; enabledOut: boolean;
retryCount: number;
subscriptions: HttpSubscription; subscriptions: HttpSubscription;
} }