Skip to content

Commit

Permalink
Merge pull request #1094 from equinor/master
Browse files Browse the repository at this point in the history
Release Resfresh env on action (#1093)
  • Loading branch information
Richard87 authored Sep 20, 2024
2 parents e1051b9 + 504dee1 commit 33d358b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const ActiveComponentOverview: FunctionComponent<{
envName={envName}
componentName={componentName}
oauth2={component.oauth2}
refetch={refetch}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function ActiveComponentToolbar({
const [scaleTrigger, scaleState] = useScaleComponentMutation();
const [stopTrigger, stopState] = useStopComponentMutation();
const [restartTrigger, restartState] = useRestartComponentMutation();
const startRefetch = useDurationInterval(2_000, 10_000, refetch);
const startRefetch = useDurationInterval(refetch);

const isStopped = component?.status === 'Stopped';
const isWorking =
Expand Down
2 changes: 1 addition & 1 deletion src/components/page-active-component/component-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ComponentStatus({
component,
}: Props) {
const [resetTrigger, resetState] = useResetScaledComponentMutation();
const startRefetch = useDurationInterval(2_000, 10_000, refetch);
const startRefetch = useDurationInterval(refetch);
const onReset = handlePromiseWithToast(async () => {
await resetTrigger({
appName,
Expand Down
14 changes: 11 additions & 3 deletions src/components/page-active-component/oauth-service.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Accordion, Typography } from '@equinor/eds-core-react';
import * as PropTypes from 'prop-types';
import type { FunctionComponent } from 'react';

import { OAuthToolbar } from './oauth-toolbar';

Expand All @@ -9,12 +8,20 @@ import { getOAuthReplicaUrl } from '../../utils/routing';
import { ReplicaList } from '../replica-list';
import { ComponentStatusBadge } from '../status-badges';

export const OAuthService: FunctionComponent<{
type Props = {
appName: string;
envName: string;
componentName: string;
oauth2: OAuth2AuxiliaryResource;
}> = ({ appName, envName, componentName, oauth2 }) => (
refetch: () => unknown;
};
export const OAuthService = ({
appName,
envName,
componentName,
oauth2,
refetch,
}: Props) => (
<Accordion className="accordion elevated" chevronPosition="right">
<Accordion.Item isExpanded>
<Accordion.Header>
Expand All @@ -37,6 +44,7 @@ export const OAuthService: FunctionComponent<{
envName={envName}
componentName={componentName}
oauth2={oauth2}
refetch={refetch}
/>
</span>
</div>
Expand Down
29 changes: 15 additions & 14 deletions src/components/page-active-component/oauth-toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import { Button, CircularProgress } from '@equinor/eds-core-react';
import * as PropTypes from 'prop-types';

import { useDurationInterval } from '../../effects/use-interval';
import {
type OAuth2AuxiliaryResource,
useRestartOAuthAuxiliaryResourceMutation,
} from '../../store/radix-api';
import { getFetchErrorMessage } from '../../store/utils';
import { errorToast } from '../global-top-nav/styled-toaster';
import { handlePromiseWithToast } from '../global-top-nav/styled-toaster';

type Props = {
appName: string;
envName: string;
componentName: string;
oauth2?: OAuth2AuxiliaryResource;
refetch: () => unknown;
};
export function OAuthToolbar({
appName,
envName,
componentName,
oauth2,
refetch,
}: Props) {
const [trigger, { isLoading }] = useRestartOAuthAuxiliaryResourceMutation();
const startRefetch = useDurationInterval(refetch);

const isRestartEnabled =
oauth2?.deployment?.status === 'Consistent' &&
oauth2?.deployment?.replicaList?.length > 0 &&
!isLoading;
oauth2?.deployment?.status !== 'Stopped' || isLoading;

const restartInProgress =
isLoading || oauth2?.deployment?.status === 'Reconciling';

const onRestart = handlePromiseWithToast(
async () => {
await trigger({ appName, envName, componentName }).unwrap();
startRefetch();
},
'Restaring OAuth2 Service',
'Failed to restart Oauth2 Service'
);
return (
<div className="grid grid--gap-small">
<div className="grid grid--gap-small grid--auto-columns">
{restartInProgress && <CircularProgress size={32} />}

<Button
onClick={async () => {
try {
await trigger({ appName, envName, componentName }).unwrap();
} catch (error) {
errorToast(
`Failed to restart OAUTH. ${getFetchErrorMessage(error)}`
);
}
}}
onClick={onRestart}
disabled={!isRestartEnabled}
variant="outlined"
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/page-environment/environment-overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const EnvironmentOverview: FunctionComponent<{
environment={environment}
startEnabled
stopEnabled
fethcEnvironment={refetchEnv}
refetch={refetchEnv}
/>
<Typography variant="h4">Overview</Typography>
<div className="grid grid--gap-medium grid--overview-columns">
Expand Down
12 changes: 7 additions & 5 deletions src/components/page-environment/environment-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button, CircularProgress } from '@equinor/eds-core-react';
import * as PropTypes from 'prop-types';
import type { FunctionComponent } from 'react';

import { useDurationInterval } from '../../effects/use-interval';
import { type Environment, radixApi } from '../../store/radix-api';
import { getFetchErrorMessage } from '../../store/utils';
import { errorToast } from '../global-top-nav/styled-toaster';
Expand All @@ -11,14 +12,15 @@ export const EnvironmentToolbar: FunctionComponent<{
environment: Readonly<Environment>;
startEnabled?: boolean;
stopEnabled?: boolean;
fethcEnvironment?: () => Promise<unknown>;
refetch?: () => unknown;
}> = ({
appName,
environment: { activeDeployment, name },
startEnabled,
stopEnabled,
fethcEnvironment,
refetch,
}) => {
const refetchEnv = useDurationInterval(refetch);
const [start, startState] = radixApi.endpoints.startEnvironment.useMutation();
const [stop, stopState] = radixApi.endpoints.stopEnvironment.useMutation();
const [restart, restartState] =
Expand Down Expand Up @@ -63,7 +65,7 @@ export const EnvironmentToolbar: FunctionComponent<{
onClick={async () => {
try {
await start({ appName, envName: name }).unwrap();
fethcEnvironment?.();
refetchEnv();
} catch (error) {
errorToast(
`Failed to start environment. ${getFetchErrorMessage(error)}`
Expand All @@ -80,7 +82,7 @@ export const EnvironmentToolbar: FunctionComponent<{
onClick={async () => {
try {
await stop({ appName, envName: name }).unwrap();
fethcEnvironment?.();
refetchEnv();
} catch (error) {
errorToast(
`Failed to stop environment. ${getFetchErrorMessage(error)}`
Expand All @@ -96,7 +98,7 @@ export const EnvironmentToolbar: FunctionComponent<{
onClick={async () => {
try {
await restart({ appName, envName: name }).unwrap();
fethcEnvironment?.();
refetchEnv();
} catch (error) {
errorToast(
`Failed to restart environment. ${getFetchErrorMessage(error)}`
Expand Down
6 changes: 3 additions & 3 deletions src/effects/use-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function useInterval(callback: () => void, delay?: number): void {
}

export function useDurationInterval(
intervalMs: number,
durationMs: number,
callback: () => unknown
callback: () => unknown,
intervalMs = 2_000,
durationMs = 15_000
) {
const [startAt, setStartAt] = useState<number>(0);

Expand Down
1 change: 0 additions & 1 deletion src/effects/use-local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default function useLocalStorage<T>(
useInterval(() => {
const current = getLocalStorageItem(key);
if (JSON.stringify(current) != JSON.stringify(state)) {
console.log('updating state', { current, state });
setState(current);
}
}, 250);
Expand Down

0 comments on commit 33d358b

Please sign in to comment.