Skip to content

Commit 380efc7

Browse files
committed
Move useRunKill and it's payload from RunList to KillButton
Take run query as KillButton props. Then do all processing of payload and ownership in KillButton component. This is to refresh run page after killing a run. Signed-off-by: Vallari Agrawal <[email protected]>
1 parent e702359 commit 380efc7

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

Diff for: src/components/KillButton/index.tsx

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from "react";
2-
import type { UseMutationResult } from "@tanstack/react-query";
2+
import type { UseMutationResult, UseQueryResult } from "@tanstack/react-query";
33
import Button from "@mui/material/Button";
44
import Box from "@mui/material/Box";
55
import CircularProgress from "@mui/material/CircularProgress";
@@ -11,15 +11,14 @@ import Typography from "@mui/material/Typography";
1111
import Tooltip from '@mui/material/Tooltip';
1212

1313
import CodeBlock from "../CodeBlock";
14+
import type { Run as RunResponse } from "../../lib/paddles.d";
1415
import { KillRunPayload } from "../../lib/teuthologyAPI.d";
15-
import { useSession } from "../../lib/teuthologyAPI";
16+
import { useSession, useRunKill } from "../../lib/teuthologyAPI";
1617
import Alert from "../Alert";
1718

1819

1920
type KillButtonProps = {
20-
mutation: UseMutationResult;
21-
payload: KillRunPayload;
22-
disabled?: boolean;
21+
query: UseQueryResult<RunResponse>;
2322
};
2423

2524
type KillButtonDialogProps = {
@@ -29,13 +28,21 @@ type KillButtonDialogProps = {
2928
handleClose: () => void;
3029
};
3130

32-
export default function KillButton(props: KillButtonProps) {
31+
export default function KillButton({query: runQuery}: KillButtonProps) {
32+
const killMutation = useRunKill();
3333
const [open, setOpen] = useState(false);
34-
const mutation: UseMutationResult = props.mutation;
3534
const sessionQuery = useSession();
35+
const data: RunResponse | undefined = runQuery.data;
36+
const run_owner = data?.jobs[0].owner || "";
37+
const killPayload: KillRunPayload = {
38+
"--run": data?.name || "",
39+
"--owner": run_owner,
40+
"--machine-type": data?.machine_type || "",
41+
"--preserve-queue": true,
42+
}
3643
const loggedUser = sessionQuery.data?.session?.username;
3744
const isUserAdmin = sessionQuery.data?.session?.isUserAdmin;
38-
const owner = props.payload["--owner"].toLowerCase()
45+
const owner = killPayload["--owner"].toLowerCase()
3946
const isOwner = (loggedUser?.toLowerCase() == owner) || (`scheduled_${loggedUser?.toLowerCase()}@teuthology` == owner)
4047
const isButtonDisabled = (!isOwner && !isUserAdmin)
4148

@@ -53,11 +60,14 @@ export default function KillButton(props: KillButtonProps) {
5360
};
5461

5562
const refreshAndtoggle = () => {
63+
if (open && !killMutation.isIdle) { // on closing confirmation dialog after kill-run
64+
runQuery.refetch();
65+
}
5666
toggleDialog();
57-
mutation.reset();
67+
killMutation.reset();
5868
}
5969

60-
if (props.disabled || !(sessionQuery.data?.session?.username)) {
70+
if ((data?.status.includes("finished")) || !(sessionQuery.data?.session?.username)) {
6171
// run finished or user logged out
6272
return null;
6373
}
@@ -81,14 +91,14 @@ export default function KillButton(props: KillButtonProps) {
8191
</span>
8292
</Tooltip>
8393
<KillButtonDialog
84-
mutation={mutation}
85-
payload={props.payload}
94+
mutation={killMutation}
95+
payload={killPayload}
8696
open={open}
87-
handleClose={toggleDialog}
97+
handleClose={refreshAndtoggle}
8898
/>
8999
</div>
90-
{ (mutation.isError) ? <Alert severity="error" message="Unable to kill run" /> : null }
91-
{ (mutation.isSuccess) ? <Alert severity="success" message={`Run killed successfully! \n`} /> : null }
100+
{ (killMutation.isError) ? <Alert severity="error" message="Unable to kill run" /> : null }
101+
{ (killMutation.isSuccess) ? <Alert severity="success" message={`Run killed successfully! \n`} /> : null }
92102
</div>
93103
);
94104
};

Diff for: src/pages/Run/index.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import { Helmet } from "react-helmet";
99
import type { Run as Run_, RunParams } from "../../lib/paddles.d";
1010

1111
import { useRun } from "../../lib/paddles";
12-
import { useRunKill } from "../../lib/teuthologyAPI";
1312
import JobList from "../../components/JobList";
1413
import Link from "../../components/Link";
1514
import KillButton from "../../components/KillButton";
16-
import { KillRunPayload } from '../../lib/teuthologyAPI.d';
1715

1816
const PREFIX = "index";
1917

@@ -46,7 +44,6 @@ export default function Run() {
4644
pageSize: NumberParam,
4745
});
4846
const { name } = useParams<RunParams>();
49-
const killMutation = useRunKill();
5047
const query = useRun(name === undefined ? "" : name);
5148
if (query === null) return <Typography>404</Typography>;
5249
if (query.isError) return null;
@@ -56,13 +53,6 @@ export default function Run() {
5653
const date = query.data?.scheduled
5754
? format(new Date(query.data.scheduled), "yyyy-MM-dd")
5855
: null;
59-
const run_owner = data?.jobs[0].owner || "";
60-
const killPayload: KillRunPayload = {
61-
"--run": data?.name || "",
62-
"--owner": run_owner,
63-
"--machine-type": data?.machine_type || "",
64-
"--preserve-queue": true,
65-
}
6656
return (
6757
<Root className={classes.root}>
6858
<Helmet>
@@ -83,11 +73,7 @@ export default function Run() {
8373
date
8474
</FilterLink>
8575
</div>
86-
<KillButton
87-
mutation={killMutation}
88-
payload={killPayload}
89-
disabled={(data?.status.includes("finished"))}
90-
/>
76+
<KillButton query={query} />
9177
<JobList query={query} params={params} setter={setParams} />
9278
</Root>
9379
);

0 commit comments

Comments
 (0)