Skip to content

Commit

Permalink
cooperative termination of preview server
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Apr 30, 2022
1 parent ccef322 commit a3333f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/command/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
} from "../../core/resources.ts";
import { execProcess } from "../../core/process.ts";
import { monitorQuartoSrcChanges } from "../../core/quarto.ts";
import { exitWithCleanup } from "../../core/cleanup.ts";

interface PreviewOptions {
port?: number;
Expand Down Expand Up @@ -216,6 +217,11 @@ export function isPreviewRenderRequest(req: Request) {
}
}

export function isPreviewTerminateRequest(req: Request) {
const kTerminateToken = "4231F431-58D3-4320-9713-994558E4CC45";
return req.url.includes(kTerminateToken);
}

export function previewRenderRequest(
req: Request,
hasClients: boolean,
Expand Down Expand Up @@ -540,6 +546,8 @@ function htmlFileRequestHandlerOptions(
onRequest: async (req: Request) => {
if (reloader.handle(req)) {
return Promise.resolve(reloader.connect(req));
} else if (isPreviewTerminateRequest(req)) {
exitWithCleanup(0);
} else if (req.url.endsWith("/quarto-render/")) {
// don't wait for the promise so the
// caller gets an immediate reply
Expand Down
5 changes: 4 additions & 1 deletion src/project/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { ServeOptions } from "./types.ts";
import { watchProject } from "./watch.ts";
import {
isPreviewRenderRequest,
isPreviewTerminateRequest,
previewRenderRequest,
previewRenderRequestIsCompatible,
} from "../../command/preview/preview.ts";
Expand Down Expand Up @@ -88,7 +89,7 @@ import { createTempContext, TempContext } from "../../core/temp.ts";
import { ServeRenderManager } from "./render.ts";
import { projectScratchPath } from "../project-scratch.ts";
import { monitorQuartoSrcChanges } from "../../core/quarto.ts";
import { onCleanup } from "../../core/cleanup.ts";
import { exitWithCleanup, onCleanup } from "../../core/cleanup.ts";

export const kRenderNone = "none";
export const kRenderDefault = "default";
Expand Down Expand Up @@ -254,6 +255,8 @@ export async function serveProject(
onRequest: async (req: Request) => {
if (watcher.handle(req)) {
return await watcher.connect(req);
} else if (isPreviewTerminateRequest(req)) {
exitWithCleanup(0);
} else if (isPreviewRenderRequest(req)) {
const prevReq = previewRenderRequest(
req,
Expand Down

0 comments on commit a3333f7

Please sign in to comment.