centralize dev logging using event emitter

This commit is contained in:
Eric Allam
2024-08-08 16:32:15 +01:00
committed by Eric Allam
parent ff9013bf01
commit e37d8d4085
4 changed files with 92 additions and 4 deletions
+61
View File
@@ -0,0 +1,61 @@
import { ResolvedConfig } from "@trigger.dev/core/v3/build";
import { DevCommandOptions } from "../commands/dev.js";
import { logger } from "../utilities/logger.js";
import { chalkGrey, chalkLink, chalkWorker, cliLink } from "../utilities/cliOutput.js";
import { eventBus, EventBusEventArgs } from "../utilities/eventBus.js";
export type DevOutputOptions = {
name: string | undefined;
dashboardUrl: string;
config: ResolvedConfig;
args: DevCommandOptions;
};
export function startDevOutput(options: DevOutputOptions) {
const { dashboardUrl, config } = options;
const rebuildStarted = (...[target]: EventBusEventArgs<"rebuildStarted">) => {
logger.log(chalkGrey("○ Rebuilding background worker…"));
};
const buildStarted = (...[target]: EventBusEventArgs<"buildStarted">) => {
logger.log(chalkGrey("○ Building background worker…"));
};
const workerSkipped = () => {
logger.log(chalkGrey("○ No changes detected, skipping build…"));
};
const backgroundWorkerInitialized = (
...[worker]: EventBusEventArgs<"backgroundWorkerInitialized">
) => {
const testUrl = `${dashboardUrl}/projects/v3/${config.project}/test?environment=dev`;
const runsUrl = `${dashboardUrl}/projects/v3/${config.project}/runs?envSlug=dev`;
const pipe = chalkGrey("|");
const bullet = chalkGrey("○");
const arrow = chalkGrey("->");
const testLink = chalkLink(cliLink("Test tasks", testUrl));
const runsLink = chalkLink(cliLink("View runs", runsUrl));
const workerStarted = chalkGrey("Background worker started");
const workerVersion = chalkWorker(worker.serverWorker!.version);
logger.log(
`${bullet} ${workerStarted} ${arrow} ${workerVersion} ${pipe} ${testLink} ${pipe} ${runsLink}`
);
};
eventBus.on("rebuildStarted", rebuildStarted);
eventBus.on("buildStarted", buildStarted);
eventBus.on("workerSkipped", workerSkipped);
eventBus.on("backgroundWorkerInitialized", backgroundWorkerInitialized);
return () => {
eventBus.off("rebuildStarted", rebuildStarted);
eventBus.off("buildStarted", buildStarted);
eventBus.off("workerSkipped", workerSkipped);
eventBus.off("backgroundWorkerInitialized", backgroundWorkerInitialized);
};
}
+13 -3
View File
@@ -23,6 +23,8 @@ import { EphemeralDirectory, getTmpDir } from "../utilities/tempDirectories.js";
import { copyManifestToDir } from "../build/manifests.js";
import { startWorkerRuntime } from "./workerRuntime.js";
import { chalkGrey } from "../utilities/cliOutput.js";
import { eventBus } from "../utilities/eventBus.js";
import { startDevOutput } from "./devOutput.js";
export type DevSessionOptions = {
name: string | undefined;
@@ -51,6 +53,13 @@ export async function startDevSession({
dashboardUrl,
});
const stopOutput = startDevOutput({
name,
dashboardUrl,
config: rawConfig,
args: rawArgs,
});
logger.debug("Starting dev session", { destination: destination.path, rawConfig });
const externalsExtension = createExternalsBuildExtension("dev", rawConfig);
@@ -94,7 +103,7 @@ export async function startDevSession({
logger.debug("on-end plugin started");
if (bundled) {
logger.log(chalkGrey("○ Rebuilding background worker…"));
eventBus.emit("rebuildStarted", "dev");
}
});
b.onEnd(async (result: esbuild.BuildResult) => {
@@ -122,6 +131,8 @@ export async function startDevSession({
};
async function runBundle() {
eventBus.emit("buildStarted", "dev");
const bundleResult = await bundleWorker({
target: "dev",
cwd: rawConfig.workingDir,
@@ -134,8 +145,6 @@ export async function startDevSession({
jsxAutomatic: rawConfig.build.jsx.automatic,
});
logger.log(chalkGrey("○ Building background worker…"));
await updateBundle(bundleResult);
return bundleResult.stop;
@@ -150,6 +159,7 @@ export async function startDevSession({
destination.remove();
stopBundling?.().catch((error) => {});
runtime.shutdown().catch((error) => {});
stopOutput();
},
};
}
+4 -1
View File
@@ -24,6 +24,7 @@ import {
} from "@trigger.dev/core/v3/zodMessageHandler";
import { resolveDotEnvVars } from "../utilities/dotEnv.js";
import { VERSION } from "../version.js";
import { eventBus } from "../utilities/eventBus.js";
export interface WorkerRuntime {
shutdown(): Promise<void>;
@@ -157,7 +158,7 @@ class DevWorkerRuntime implements WorkerRuntime {
async initializeWorker(manifest: BuildManifest, options?: { cwd?: string }): Promise<void> {
if (this.lastBuild && this.lastBuild.contentHash === manifest.contentHash) {
logger.log(chalkGrey("○ No changes detected, skipping build…"));
eventBus.emit("workerSkipped");
return;
}
@@ -204,6 +205,8 @@ class DevWorkerRuntime implements WorkerRuntime {
backgroundWorker.serverWorker = backgroundWorkerRecord.data;
this.backgroundWorkerCoordinator.registerWorker(backgroundWorker);
this.lastBuild = manifest;
eventBus.emit("backgroundWorkerInitialized", backgroundWorker);
}
async #getEnvVars(): Promise<Record<string, string>> {
+14
View File
@@ -0,0 +1,14 @@
import { BuildTarget } from "@trigger.dev/core/v3";
import { EventEmitter } from "node:events";
import { BackgroundWorker } from "../dev/backgroundWorker.js";
export type EventBusEvents = {
rebuildStarted: [BuildTarget];
buildStarted: [BuildTarget];
workerSkipped: [];
backgroundWorkerInitialized: [BackgroundWorker];
};
export type EventBusEventArgs<T extends keyof EventBusEvents> = EventBusEvents[T];
export const eventBus = new EventEmitter<EventBusEvents>();