Skip to content

Commit 545f763

Browse files
committed
add close command to close a worker
1 parent b9abf6b commit 545f763

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/execute/julia.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ export function juliaTransportFile() {
834834
return join(juliaRuntimeDir(), "julia_transport.txt");
835835
}
836836

837-
function juliaServerLogFile() {
837+
export function juliaServerLogFile() {
838838
return join(juliaRuntimeDir(), "julia_server_log.txt");
839839
}
840840

@@ -857,7 +857,12 @@ function populateJuliaEngineCommand(command: Command) {
857857
.description(
858858
"Prints the julia server log file if it exists which can be used to diagnose problems.",
859859
)
860-
.action(printJuliaServerLog);
860+
.action(printJuliaServerLog)
861+
.command("close", "Close the worker for a given notebook")
862+
.arguments("<file:string>")
863+
.action(async (_, file) => {
864+
await closeWorker(file);
865+
});
861866
return;
862867
}
863868

@@ -910,3 +915,49 @@ function printJuliaServerLog() {
910915
}
911916
return;
912917
}
918+
919+
// todo: this could use a refactor with the other functions that make
920+
// server connections or execute commands, this one is just supposed to
921+
// simplify the pattern where a running server is expected (there will be an error if there is none)
922+
// and we want to get the API response out quickly
923+
async function connectAndWriteJuliaCommandToRunningServer<
924+
T extends ServerCommand["type"],
925+
>(
926+
command: Extract<ServerCommand, { type: T }>,
927+
): Promise<ServerCommandResponse<T>> {
928+
const transportFile = juliaTransportFile();
929+
if (!existsSync(transportFile)) {
930+
throw new Error("Julia control server is not running.");
931+
}
932+
const transportOptions = readTransportFile(transportFile);
933+
934+
const conn = await getReadyServerConnection(
935+
transportOptions,
936+
{} as JuliaExecuteOptions,
937+
);
938+
const successfullyConnected = typeof conn !== "string";
939+
940+
if (successfullyConnected) {
941+
const result = await writeJuliaCommand(
942+
conn,
943+
command,
944+
transportOptions.key,
945+
{} as JuliaExecuteOptions,
946+
);
947+
conn.close();
948+
return result;
949+
} else {
950+
throw new Error(
951+
`Found transport file but can't connect to control server.`,
952+
);
953+
}
954+
}
955+
956+
async function closeWorker(file: string) {
957+
const absfile = normalizePath(file);
958+
await connectAndWriteJuliaCommandToRunningServer({
959+
type: "close",
960+
content: { file: absfile },
961+
});
962+
info("Worker closed successfully.");
963+
}

0 commit comments

Comments
 (0)