Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resfresh env on action #1093

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 16 additions & 15 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);
console.log({ oauth2, isLoading });
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