| ... | ... | @@ -19,8 +19,75 @@ export const DESKTOP_LOG_FILE_MAX_FILES = 10; |
| 19 | 19 | const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child"; |
| 20 | 20 | |
| 21 | 21 | interface RotatingLogFileWriter { |
| 22 | | readonly writeBytes: (chunk: Uint8Array) => Effect.Effect<void>; |
| 23 | | readonly writeText: (chunk: string) => Effect.Effect<void>; |
| 22 | readonly filePath: string; |
| 23 | readonly writeBytes: ( |
| 24 | chunk: Uint8Array, |
| 25 | ) => Effect.Effect<void, PlatformError.PlatformError | DesktopLogFileWriterRecoveryError>; |
| 26 | readonly writeText: ( |
| 27 | chunk: string, |
| 28 | ) => Effect.Effect<void, PlatformError.PlatformError | DesktopLogFileWriterRecoveryError>; |
| 29 | } |
| 30 | |
| 31 | class DesktopLogFileWriterConfigurationError extends Schema.TaggedErrorClass<DesktopLogFileWriterConfigurationError>()( |
| 32 | "DesktopLogFileWriterConfigurationError", |
| 33 | { |
| 34 | option: Schema.Literals(["maxBytes", "maxFiles"]), |
| 35 | value: Schema.Number, |
| 36 | }, |
| 37 | ) { |
| 38 | override get message(): string { |
| 39 | return `${this.option} must be >= 1 (received ${this.value})`; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | class DesktopLogFileWriterRecoveryError extends Schema.TaggedErrorClass<DesktopLogFileWriterRecoveryError>()( |
| 44 | "DesktopLogFileWriterRecoveryError", |
| 45 | { |
| 46 | logFilePath: Schema.String, |
| 47 | cause: Schema.Defect(), |
| 48 | recoveryCause: Schema.Defect(), |
| 49 | }, |
| 50 | ) { |
| 51 | override get message(): string { |
| 52 | return `Failed to refresh desktop backend output log size after a write failure at ${this.logFilePath}.`; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export class DesktopBackendOutputLogSetupError extends Schema.TaggedErrorClass<DesktopBackendOutputLogSetupError>()( |
| 57 | "DesktopBackendOutputLogSetupError", |
| 58 | { |
| 59 | logFilePath: Schema.String, |
| 60 | cause: Schema.Defect(), |
| 61 | }, |
| 62 | ) { |
| 63 | override get message(): string { |
| 64 | return `Failed to initialize the desktop backend output log at ${this.logFilePath}.`; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | export class DesktopBackendOutputLogWriteError extends Schema.TaggedErrorClass<DesktopBackendOutputLogWriteError>()( |
| 69 | "DesktopBackendOutputLogWriteError", |
| 70 | { |
| 71 | operation: Schema.Literals(["encode-record", "write-record"]), |
| 72 | logFilePath: Schema.String, |
| 73 | cause: Schema.Defect(), |
| 74 | }, |
| 75 | ) { |
| 76 | override get message(): string { |
| 77 | return `Desktop backend output log operation "${this.operation}" failed at ${this.logFilePath}.`; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | export class DesktopBackendConsoleWriteError extends Schema.TaggedErrorClass<DesktopBackendConsoleWriteError>()( |
| 82 | "DesktopBackendConsoleWriteError", |
| 83 | { |
| 84 | streamName: Schema.Literals(["stdout", "stderr"]), |
| 85 | cause: Schema.Defect(), |
| 86 | }, |
| 87 | ) { |
| 88 | override get message(): string { |
| 89 | return `Failed to mirror desktop backend output to ${this.streamName}.`; |
| 90 | } |
| 24 | 91 | } |
| 25 | 92 | |
| 26 | 93 | export class DesktopBackendOutputLog extends Context.Service< |
| ... | ... | @@ -37,18 +104,6 @@ export class DesktopBackendOutputLog extends Context.Service< |
| 37 | 104 | } |
| 38 | 105 | >()("@t3tools/desktop/app/DesktopBackendOutputLog") {} |
| 39 | 106 | |
| 40 | | class DesktopLogFileWriterConfigurationError extends Schema.TaggedErrorClass<DesktopLogFileWriterConfigurationError>()( |
| 41 | | "DesktopLogFileWriterConfigurationError", |
| 42 | | { |
| 43 | | option: Schema.Literals(["maxBytes", "maxFiles"]), |
| 44 | | value: Schema.Number, |
| 45 | | }, |
| 46 | | ) { |
| 47 | | override get message(): string { |
| 48 | | return `${this.option} must be >= 1 (received ${this.value})`; |
| 49 | | } |
| 50 | | } |
| 51 | | |
| 52 | 107 | type DesktopLogFileWriterError = |
| 53 | 108 | | DesktopLogFileWriterConfigurationError |
| 54 | 109 | | PlatformError.PlatformError; |
| ... | ... | @@ -85,10 +140,13 @@ const sanitizeLogValue = (value: string): string => value.replace(/\s+/g, " ").t |
| 85 | 140 | const refreshFileSize = ( |
| 86 | 141 | fileSystem: FileSystem.FileSystem, |
| 87 | 142 | filePath: string, |
| 88 | | ): Effect.Effect<number, never> => |
| 143 | ): Effect.Effect<number, PlatformError.PlatformError> => |
| 89 | 144 | fileSystem.stat(filePath).pipe( |
| 90 | 145 | Effect.map((stat) => Number(stat.size)), |
| 91 | | Effect.orElseSucceed(() => 0), |
| 146 | Effect.catchTags({ |
| 147 | PlatformError: (error) => |
| 148 | error.reason._tag === "NotFound" ? Effect.succeed(0) : Effect.fail(error), |
| 149 | }), |
| 92 | 150 | ); |
| 93 | 151 | |
| 94 | 152 | const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(function* (input: { |
| ... | ... | @@ -126,41 +184,52 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 126 | 184 | const currentSize = yield* Ref.make(yield* refreshFileSize(fileSystem, input.filePath)); |
| 127 | 185 | const mutex = yield* Semaphore.make(1); |
| 128 | 186 | |
| 187 | const recoverCurrentSize = ( |
| 188 | cause: PlatformError.PlatformError, |
| 189 | ): Effect.Effect<never, PlatformError.PlatformError | DesktopLogFileWriterRecoveryError> => |
| 190 | refreshFileSize(fileSystem, input.filePath).pipe( |
| 191 | Effect.matchEffect({ |
| 192 | onFailure: (recoveryCause) => |
| 193 | Effect.fail( |
| 194 | new DesktopLogFileWriterRecoveryError({ |
| 195 | logFilePath: input.filePath, |
| 196 | cause, |
| 197 | recoveryCause, |
| 198 | }), |
| 199 | ), |
| 200 | onSuccess: (size) => Ref.set(currentSize, size).pipe(Effect.andThen(Effect.fail(cause))), |
| 201 | }), |
| 202 | ); |
| 203 | |
| 129 | 204 | const pruneOverflowBackups = Effect.gen(function* () { |
| 130 | | const entries = yield* fileSystem.readDirectory(directory).pipe(Effect.orElseSucceed(() => [])); |
| 205 | const entries = yield* fileSystem.readDirectory(directory); |
| 131 | 206 | for (const entry of entries) { |
| 132 | 207 | if (!entry.startsWith(`${baseName}.`)) continue; |
| 133 | 208 | const suffix = Number(entry.slice(baseName.length + 1)); |
| 134 | 209 | if (!Number.isInteger(suffix) || suffix <= maxFiles) continue; |
| 135 | | yield* fileSystem.remove(path.join(directory, entry), { force: true }).pipe(Effect.ignore); |
| 210 | yield* fileSystem.remove(path.join(directory, entry), { force: true }); |
| 136 | 211 | } |
| 137 | 212 | }); |
| 138 | 213 | |
| 139 | 214 | const rotate = Effect.gen(function* () { |
| 140 | | yield* fileSystem.remove(withSuffix(maxFiles), { force: true }).pipe(Effect.ignore); |
| 215 | yield* fileSystem.remove(withSuffix(maxFiles), { force: true }); |
| 141 | 216 | for (let index = maxFiles - 1; index >= 1; index -= 1) { |
| 142 | 217 | const source = withSuffix(index); |
| 143 | | const sourceExists = yield* fileSystem.exists(source).pipe(Effect.orElseSucceed(() => false)); |
| 218 | const sourceExists = yield* fileSystem.exists(source); |
| 144 | 219 | if (sourceExists) { |
| 145 | 220 | yield* fileSystem.rename(source, withSuffix(index + 1)); |
| 146 | 221 | } |
| 147 | 222 | } |
| 148 | | const currentExists = yield* fileSystem |
| 149 | | .exists(input.filePath) |
| 150 | | .pipe(Effect.orElseSucceed(() => false)); |
| 223 | const currentExists = yield* fileSystem.exists(input.filePath); |
| 151 | 224 | if (currentExists) { |
| 152 | 225 | yield* fileSystem.rename(input.filePath, withSuffix(1)); |
| 153 | 226 | } |
| 154 | 227 | yield* Ref.set(currentSize, 0); |
| 155 | | }).pipe( |
| 156 | | Effect.catch(() => |
| 157 | | refreshFileSize(fileSystem, input.filePath).pipe( |
| 158 | | Effect.flatMap((size) => Ref.set(currentSize, size)), |
| 159 | | ), |
| 160 | | ), |
| 161 | | ); |
| 228 | }); |
| 162 | 229 | |
| 163 | | const writeBytes = (chunk: Uint8Array): Effect.Effect<void> => { |
| 230 | const writeBytes = ( |
| 231 | chunk: Uint8Array, |
| 232 | ): Effect.Effect<void, PlatformError.PlatformError | DesktopLogFileWriterRecoveryError> => { |
| 164 | 233 | if (chunk.byteLength === 0) return Effect.void; |
| 165 | 234 | |
| 166 | 235 | return mutex.withPermits(1)( |
| ... | ... | @@ -178,11 +247,9 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 178 | 247 | yield* rotate; |
| 179 | 248 | } |
| 180 | 249 | }).pipe( |
| 181 | | Effect.catch(() => |
| 182 | | refreshFileSize(fileSystem, input.filePath).pipe( |
| 183 | | Effect.flatMap((size) => Ref.set(currentSize, size)), |
| 184 | | ), |
| 185 | | ), |
| 250 | Effect.catchTags({ |
| 251 | PlatformError: recoverCurrentSize, |
| 252 | }), |
| 186 | 253 | ), |
| 187 | 254 | ); |
| 188 | 255 | }; |
| ... | ... | @@ -190,6 +257,7 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 190 | 257 | yield* pruneOverflowBackups; |
| 191 | 258 | |
| 192 | 259 | return { |
| 260 | filePath: input.filePath, |
| 193 | 261 | writeBytes, |
| 194 | 262 | writeText: (chunk) => writeBytes(textEncoder.encode(chunk)), |
| 195 | 263 | } satisfies RotatingLogFileWriter; |
| ... | ... | @@ -199,10 +267,17 @@ const writeDevelopmentConsoleOutput = ( |
| 199 | 267 | streamName: "stdout" | "stderr", |
| 200 | 268 | chunk: Uint8Array, |
| 201 | 269 | ): Effect.Effect<void> => |
| 202 | | Effect.sync(() => { |
| 203 | | const output = streamName === "stderr" ? process.stderr : process.stdout; |
| 204 | | output.write(chunk); |
| 205 | | }).pipe(Effect.ignore); |
| 270 | Effect.try({ |
| 271 | try: () => { |
| 272 | const output = streamName === "stderr" ? process.stderr : process.stdout; |
| 273 | output.write(chunk); |
| 274 | }, |
| 275 | catch: (cause) => new DesktopBackendConsoleWriteError({ streamName, cause }), |
| 276 | }).pipe( |
| 277 | Effect.catchTags({ |
| 278 | DesktopBackendConsoleWriteError: (error) => Effect.logError(error.message, { error }), |
| 279 | }), |
| 280 | ); |
| 206 | 281 | |
| 207 | 282 | const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackendChildLogRecord")( |
| 208 | 283 | function* ( |
| ... | ... | @@ -222,17 +297,47 @@ const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackend |
| 222 | 297 | annotations: input.annotations, |
| 223 | 298 | spans: {}, |
| 224 | 299 | fiberId: DESKTOP_BACKEND_CHILD_LOG_FIBER_ID, |
| 225 | | }); |
| 226 | | yield* logFile.writeText(`${encoded}\n`); |
| 227 | | }).pipe(Effect.ignore({ log: true })); |
| 300 | }).pipe( |
| 301 | Effect.mapError( |
| 302 | (cause) => |
| 303 | new DesktopBackendOutputLogWriteError({ |
| 304 | operation: "encode-record", |
| 305 | logFilePath: logFile.filePath, |
| 306 | cause, |
| 307 | }), |
| 308 | ), |
| 309 | ); |
| 310 | yield* logFile.writeText(`${encoded}\n`).pipe( |
| 311 | Effect.mapError( |
| 312 | (cause) => |
| 313 | new DesktopBackendOutputLogWriteError({ |
| 314 | operation: "write-record", |
| 315 | logFilePath: logFile.filePath, |
| 316 | cause, |
| 317 | }), |
| 318 | ), |
| 319 | ); |
| 320 | }).pipe( |
| 321 | Effect.catchTags({ |
| 322 | DesktopBackendOutputLogWriteError: (error) => Effect.logError(error.message, { error }), |
| 323 | }), |
| 324 | ); |
| 228 | 325 | }, |
| 229 | 326 | ); |
| 230 | 327 | |
| 231 | | const make = Effect.gen(function* () { |
| 328 | export const make = Effect.gen(function* () { |
| 232 | 329 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 330 | const logFilePath = environment.path.join(environment.logDir, "server-child.log"); |
| 233 | 331 | const writer = yield* makeRotatingLogFileWriter({ |
| 234 | | filePath: environment.path.join(environment.logDir, "server-child.log"), |
| 235 | | }).pipe(Effect.option); |
| 332 | filePath: logFilePath, |
| 333 | }).pipe( |
| 334 | Effect.mapError((cause) => new DesktopBackendOutputLogSetupError({ logFilePath, cause })), |
| 335 | Effect.map(Option.some), |
| 336 | Effect.catchTags({ |
| 337 | DesktopBackendOutputLogSetupError: (error) => |
| 338 | Effect.logError(error.message, { error }).pipe(Effect.as(Option.none())), |
| 339 | }), |
| 340 | ); |
| 236 | 341 | |
| 237 | 342 | const service = Option.match(writer, { |
| 238 | 343 | onNone: () => DesktopBackendOutputLogNoop, |