Skip to content

Commit

Permalink
format recent jobs route responseand handling better job list on the …
Browse files Browse the repository at this point in the history
…component
  • Loading branch information
brnovasco committed Jun 13, 2024
1 parent 51b3745 commit 1104983
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,21 @@ export default async function Home() {
}

async function RecentJobLinks() {
const recentJobs = await api.job.userRecentJobs();
const { jobs } = await api.job.userRecentJobs();

if (jobs.length === 0) return null;

return (
<div className="flex flex-col items-center gap-4 mt-12 ">
<div className="mt-12 flex flex-col items-center gap-4 ">
<h2 className="text-xl font-semibold">Recent Jobs</h2>
<ul className="flex flex-col gap-2">
{recentJobs.jobs.map(({jobId, state}) => (
{jobs.map(({ jobId, state }) => (
<li key={jobId}>
<Link
href={`/instances?jobId=${jobId}`}
className={buttonVariants({ variant: "link" })}
>
{`${jobId} (${state?.split(" ")[0] ?? state})`}
{`${jobId} (${state})`}
<MoveUpRightIcon className="ml-2 h-4 w-4" />
</Link>
</li>
Expand Down
50 changes: 36 additions & 14 deletions src/server/api/routers/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,24 @@ export const jobRouter = createTRPCRouter({
message: "Job data not found",
});
}

const [state, submit, start, end, elapsed, partition, nodeList, allocGRES, nCPUS, reason, exitCode] =
data.split("|");

const [
state,
submit,
start,
end,
elapsed,
partition,
nodeList,
allocGRES,
nCPUS,
reason,
exitCode,
] = data.split("|");

// some of the fields have multiple values separated by new lines or spaces, generally we only want the first value
return {
state: state?.split(' ')[0] ?? state,
state: state?.split(" ")[0] ?? state,
submit,
start,
end,
Expand All @@ -144,7 +155,7 @@ export const jobRouter = createTRPCRouter({
nodeList,
allocGRES,
nCPUS: nCPUS,
reason: `${state}, exit code: ${exitCode}, reason: ${reason}`,
reason: `${state}, exit code: ${exitCode}, reason: ${reason}`,
};
}),
partitionOptions: protectedProcedure.query(async ({ ctx }) => {
Expand Down Expand Up @@ -209,21 +220,32 @@ export const jobRouter = createTRPCRouter({
privateKey: keys.privateKey,
passphrase: env.SSH_PASSPHRASE,
});

const command = `sacct --parsable2 --user ${username} --name ${jobName} --allocations --format=JobID,State --noheader`
const command = `sacct --parsable2 --user ${username} --name ${jobName} --allocations --format=JobID,State --noheader`;
const { stdout, stderr } = await connection.execCommand(command);

connection.dispose();
if (stderr) {
throw new Error(stderr);
}

const jobs = stdout.trim().split("\n").map((line) => {
const [jobId, state] = line.split("|");
return { jobId, state };

if (stdout.trim().length === 0) {
return { jobs: [] } as { jobs: { jobId: string; state: string }[] };
}
);


const jobs = stdout
.trim()
.split("\n")
.map((line) => {
const [jobId, jobState] = line.split("|");
if (!jobId || !jobState) {
return;
}
// the job state comes with a suffix that we don't need, so we split it and get the first part
return { jobId, state: jobState.split("_")[0] ?? jobState };
})
.filter(Boolean) as { jobId: string; state: string }[];

return { jobs };
}),
});

0 comments on commit 1104983

Please sign in to comment.