|
| 1 | +/* |
| 2 | + * Copyright 2022 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Profiles } from "madwizard" |
| 18 | +import { basename, join } from "path" |
| 19 | +import { cli } from "madwizard/dist/fe/cli" |
| 20 | +import { MenuItemConstructorOptions } from "electron" |
| 21 | +import { CreateWindowFunction } from "@kui-shell/core" |
| 22 | + |
| 23 | +import { rayIcon } from "../../icons" |
| 24 | +import UpdateFunction from "../../update" |
| 25 | +import ProfileActiveRunWatcher from "../../watchers/profile/active-runs" |
| 26 | + |
| 27 | +import { runMenuItems } from "./runs" |
| 28 | + |
| 29 | +/** active run watcher per profile */ |
| 30 | +const watchers: Record<string, ProfileActiveRunWatcher> = {} |
| 31 | + |
| 32 | +/** This is the utility function that will open a CodeFlare Dashboard window, pointing to the given local filesystem `logdir` */ |
| 33 | +async function openDashboard(createWindow: CreateWindowFunction, logdir: string, follow = true) { |
| 34 | + await createWindow(["codeflare", "dashboard", follow ? "-f" : "", logdir], { |
| 35 | + title: "CodeFlare Dashboard - " + basename(logdir), |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +/** This is the click handler for a an active run menu item */ |
| 40 | +async function openMenuItem(this: CreateWindowFunction, profile: string, runId: string) { |
| 41 | + // check to see if we already have a log aggregator's capture |
| 42 | + const logdir = join(Profiles.guidebookJobDataPath({ profile }), runId) |
| 43 | + if ( |
| 44 | + await import("fs/promises") |
| 45 | + .then((_) => _.access(logdir)) |
| 46 | + .then(() => true) |
| 47 | + .catch(() => false) |
| 48 | + ) { |
| 49 | + // yup, so we can just open up what we are capturing/have |
| 50 | + // already captured |
| 51 | + return openDashboard(this, logdir) |
| 52 | + } |
| 53 | + |
| 54 | + // otherwise, we will need to start of a log aggregator |
| 55 | + const guidebook = "ml/ray/aggregator/with-jobid" |
| 56 | + const rayAddress = await watchers[profile].rayAddress |
| 57 | + if (rayAddress) { |
| 58 | + process.env.JOB_ID = runId |
| 59 | + process.env.RAY_ADDRESS = rayAddress |
| 60 | + process.env.NO_WAIT = "true" // don't wait for job termination |
| 61 | + process.env.QUIET_CONSOLE = "true" // don't tee logs to the console |
| 62 | + const resp = await cli(["madwizard", "guide", guidebook], undefined, { |
| 63 | + profile, |
| 64 | + clean: false /* don't kill the port-forward subprocess! we'll manage that */, |
| 65 | + interactive: false, |
| 66 | + store: process.env.GUIDEBOOK_STORE, |
| 67 | + }) |
| 68 | + |
| 69 | + if (resp) { |
| 70 | + if (!resp.env.LOGDIR_STAGE) { |
| 71 | + console.error("Failed to attach to job", runId) |
| 72 | + } else { |
| 73 | + await openDashboard(this, resp.env.LOGDIR_STAGE) |
| 74 | + |
| 75 | + // now the window has closed, so we can clean up any |
| 76 | + // subprocesses spawned by the guidebook |
| 77 | + if (typeof resp.cleanExit === "function") { |
| 78 | + resp.cleanExit() |
| 79 | + process.on("exit", () => resp.cleanExit()) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** @return menu items that allow attaching to an active run in the given `profileName` */ |
| 87 | +export default function activeRuns( |
| 88 | + profile: string, |
| 89 | + createWindow: CreateWindowFunction, |
| 90 | + updateFn: UpdateFunction |
| 91 | +): MenuItemConstructorOptions[] { |
| 92 | + if (!watchers[profile]) { |
| 93 | + watchers[profile] = new ProfileActiveRunWatcher(updateFn, profile) |
| 94 | + } |
| 95 | + |
| 96 | + const runs = watchers[profile].runs |
| 97 | + if (runs.length > 0) { |
| 98 | + return runMenuItems( |
| 99 | + profile, |
| 100 | + openMenuItem.bind(createWindow), |
| 101 | + runs.map((_) => Object.assign(_, { icon: rayIcon })) |
| 102 | + ) |
| 103 | + } else { |
| 104 | + return [{ label: "No active runs", enabled: false }] |
| 105 | + } |
| 106 | +} |
0 commit comments