| ... | @@ -19,8 +19,75 @@ export const DESKTOP_LOG_FILE_MAX_FILES = 10; | ... | @@ -19,8 +19,75 @@ export const DESKTOP_LOG_FILE_MAX_FILES = 10; |
| 19 | const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child"; | 19 | const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child"; |
| 20 | | 20 | |
| 21 | interface RotatingLogFileWriter { | 21 | interface RotatingLogFileWriter { |
| 22 | readonly writeBytes: (chunk: Uint8Array) => Effect.Effect<void>; | 22 | readonly filePath: string; |
| 23 | readonly writeText: (chunk: string) => Effect.Effect<void>; | 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 | export class DesktopBackendOutputLog extends Context.Service< | 93 | export class DesktopBackendOutputLog extends Context.Service< |
| ... | @@ -37,18 +104,6 @@ export class DesktopBackendOutputLog extends Context.Service< | ... | @@ -37,18 +104,6 @@ export class DesktopBackendOutputLog extends Context.Service< |
| 37 | } | 104 | } |
| 38 | >()("@t3tools/desktop/app/DesktopBackendOutputLog") {} | 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 | type DesktopLogFileWriterError = | 107 | type DesktopLogFileWriterError = |
| 53 | | DesktopLogFileWriterConfigurationError | 108 | | DesktopLogFileWriterConfigurationError |
| 54 | | PlatformError.PlatformError; | 109 | | PlatformError.PlatformError; |
| ... | @@ -85,10 +140,13 @@ const sanitizeLogValue = (value: string): string => value.replace(/\s+/g, " ").t | ... | @@ -85,10 +140,13 @@ const sanitizeLogValue = (value: string): string => value.replace(/\s+/g, " ").t |
| 85 | const refreshFileSize = ( | 140 | const refreshFileSize = ( |
| 86 | fileSystem: FileSystem.FileSystem, | 141 | fileSystem: FileSystem.FileSystem, |
| 87 | filePath: string, | 142 | filePath: string, |
| 88 | ): Effect.Effect<number, never> => | 143 | ): Effect.Effect<number, PlatformError.PlatformError> => |
| 89 | fileSystem.stat(filePath).pipe( | 144 | fileSystem.stat(filePath).pipe( |
| 90 | Effect.map((stat) => Number(stat.size)), | 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 | const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(function* (input: { | 152 | const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(function* (input: { |
| ... | @@ -126,41 +184,52 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio | ... | @@ -126,41 +184,52 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 126 | const currentSize = yield* Ref.make(yield* refreshFileSize(fileSystem, input.filePath)); | 184 | const currentSize = yield* Ref.make(yield* refreshFileSize(fileSystem, input.filePath)); |
| 127 | const mutex = yield* Semaphore.make(1); | 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 | const pruneOverflowBackups = Effect.gen(function* () { | 204 | const pruneOverflowBackups = Effect.gen(function* () { |
| 130 | const entries = yield* fileSystem.readDirectory(directory).pipe(Effect.orElseSucceed(() => [])); | 205 | const entries = yield* fileSystem.readDirectory(directory); |
| 131 | for (const entry of entries) { | 206 | for (const entry of entries) { |
| 132 | if (!entry.startsWith(`${baseName}.`)) continue; | 207 | if (!entry.startsWith(`${baseName}.`)) continue; |
| 133 | const suffix = Number(entry.slice(baseName.length + 1)); | 208 | const suffix = Number(entry.slice(baseName.length + 1)); |
| 134 | if (!Number.isInteger(suffix) || suffix <= maxFiles) continue; | 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 | const rotate = Effect.gen(function* () { | 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 | for (let index = maxFiles - 1; index >= 1; index -= 1) { | 216 | for (let index = maxFiles - 1; index >= 1; index -= 1) { |
| 142 | const source = withSuffix(index); | 217 | const source = withSuffix(index); |
| 143 | const sourceExists = yield* fileSystem.exists(source).pipe(Effect.orElseSucceed(() => false)); | 218 | const sourceExists = yield* fileSystem.exists(source); |
| 144 | if (sourceExists) { | 219 | if (sourceExists) { |
| 145 | yield* fileSystem.rename(source, withSuffix(index + 1)); | 220 | yield* fileSystem.rename(source, withSuffix(index + 1)); |
| 146 | } | 221 | } |
| 147 | } | 222 | } |
| 148 | const currentExists = yield* fileSystem | 223 | const currentExists = yield* fileSystem.exists(input.filePath); |
| 149 | .exists(input.filePath) | | |
| 150 | .pipe(Effect.orElseSucceed(() => false)); | | |
| 151 | if (currentExists) { | 224 | if (currentExists) { |
| 152 | yield* fileSystem.rename(input.filePath, withSuffix(1)); | 225 | yield* fileSystem.rename(input.filePath, withSuffix(1)); |
| 153 | } | 226 | } |
| 154 | yield* Ref.set(currentSize, 0); | 227 | yield* Ref.set(currentSize, 0); |
| 155 | }).pipe( | 228 | }); |
| 156 | Effect.catch(() => | | |
| 157 | refreshFileSize(fileSystem, input.filePath).pipe( | | |
| 158 | Effect.flatMap((size) => Ref.set(currentSize, size)), | | |
| 159 | ), | | |
| 160 | ), | | |
| 161 | ); | | |
| 162 | | 229 | |
| 163 | const writeBytes = (chunk: Uint8Array): Effect.Effect<void> => { | 230 | const writeBytes = ( |
| | 231 | chunk: Uint8Array, |
| | 232 | ): Effect.Effect<void, PlatformError.PlatformError | DesktopLogFileWriterRecoveryError> => { |
| 164 | if (chunk.byteLength === 0) return Effect.void; | 233 | if (chunk.byteLength === 0) return Effect.void; |
| 165 | | 234 | |
| 166 | return mutex.withPermits(1)( | 235 | return mutex.withPermits(1)( |
| ... | @@ -178,11 +247,9 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio | ... | @@ -178,11 +247,9 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 178 | yield* rotate; | 247 | yield* rotate; |
| 179 | } | 248 | } |
| 180 | }).pipe( | 249 | }).pipe( |
| 181 | Effect.catch(() => | 250 | Effect.catchTags({ |
| 182 | refreshFileSize(fileSystem, input.filePath).pipe( | 251 | PlatformError: recoverCurrentSize, |
| 183 | Effect.flatMap((size) => Ref.set(currentSize, size)), | 252 | }), |
| 184 | ), | | |
| 185 | ), | | |
| 186 | ), | 253 | ), |
| 187 | ); | 254 | ); |
| 188 | }; | 255 | }; |
| ... | @@ -190,6 +257,7 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio | ... | @@ -190,6 +257,7 @@ const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(functio |
| 190 | yield* pruneOverflowBackups; | 257 | yield* pruneOverflowBackups; |
| 191 | | 258 | |
| 192 | return { | 259 | return { |
| | 260 | filePath: input.filePath, |
| 193 | writeBytes, | 261 | writeBytes, |
| 194 | writeText: (chunk) => writeBytes(textEncoder.encode(chunk)), | 262 | writeText: (chunk) => writeBytes(textEncoder.encode(chunk)), |
| 195 | } satisfies RotatingLogFileWriter; | 263 | } satisfies RotatingLogFileWriter; |
| ... | @@ -199,10 +267,17 @@ const writeDevelopmentConsoleOutput = ( | ... | @@ -199,10 +267,17 @@ const writeDevelopmentConsoleOutput = ( |
| 199 | streamName: "stdout" | "stderr", | 267 | streamName: "stdout" | "stderr", |
| 200 | chunk: Uint8Array, | 268 | chunk: Uint8Array, |
| 201 | ): Effect.Effect<void> => | 269 | ): Effect.Effect<void> => |
| 202 | Effect.sync(() => { | 270 | Effect.try({ |
| 203 | const output = streamName === "stderr" ? process.stderr : process.stdout; | 271 | try: () => { |
| 204 | output.write(chunk); | 272 | const output = streamName === "stderr" ? process.stderr : process.stdout; |
| 205 | }).pipe(Effect.ignore); | 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 | const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackendChildLogRecord")( | 282 | const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackendChildLogRecord")( |
| 208 | function* ( | 283 | function* ( |
| ... | @@ -222,17 +297,47 @@ const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackend | ... | @@ -222,17 +297,47 @@ const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackend |
| 222 | annotations: input.annotations, | 297 | annotations: input.annotations, |
| 223 | spans: {}, | 298 | spans: {}, |
| 224 | fiberId: DESKTOP_BACKEND_CHILD_LOG_FIBER_ID, | 299 | fiberId: DESKTOP_BACKEND_CHILD_LOG_FIBER_ID, |
| 225 | }); | 300 | }).pipe( |
| 226 | yield* logFile.writeText(`${encoded}\n`); | 301 | Effect.mapError( |
| 227 | }).pipe(Effect.ignore({ log: true })); | 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 | const environment = yield* DesktopEnvironment.DesktopEnvironment; | 329 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| | 330 | const logFilePath = environment.path.join(environment.logDir, "server-child.log"); |
| 233 | const writer = yield* makeRotatingLogFileWriter({ | 331 | const writer = yield* makeRotatingLogFileWriter({ |
| 234 | filePath: environment.path.join(environment.logDir, "server-child.log"), | 332 | filePath: logFilePath, |
| 235 | }).pipe(Effect.option); | 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 | const service = Option.match(writer, { | 342 | const service = Option.match(writer, { |
| 238 | onNone: () => DesktopBackendOutputLogNoop, | 343 | onNone: () => DesktopBackendOutputLogNoop, |