|
| 1 | +import { mkdir, readFile, readdir, rm, stat, unlink } from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | + |
| 4 | +import { S3Storage } from '../fileStorage/s3Storage' |
| 5 | +import { ProcessEnv } from '../processEnv' |
| 6 | + |
| 7 | +const withLogFolder = async (): Promise<string> => { |
| 8 | + const logFolder = path.resolve(ProcessEnv.logFolder) |
| 9 | + await mkdir(logFolder, { recursive: true }) |
| 10 | + return logFolder |
| 11 | +} |
| 12 | + |
| 13 | +const trimSlashes = (value: string): string => { |
| 14 | + let start = 0 |
| 15 | + let end = value.length |
| 16 | + |
| 17 | + while (start < end && value[start] === '/') start += 1 |
| 18 | + while (end > start && value[end - 1] === '/') end -= 1 |
| 19 | + |
| 20 | + return value.slice(start, end) |
| 21 | +} |
| 22 | + |
| 23 | +const uploadLogFileToS3 = async (logFolder: string, fileName: string, s3Storage: S3Storage): Promise<void> => { |
| 24 | + const shouldDeleteAfterUpload = fileName !== 'arena.log' |
| 25 | + |
| 26 | + const absolutePath = path.join(logFolder, fileName) |
| 27 | + const fileStats = await stat(absolutePath) |
| 28 | + if (shouldDeleteAfterUpload && fileStats.size === 0) return |
| 29 | + |
| 30 | + const key = `${trimSlashes(ProcessEnv.logS3Prefix)}/${fileName}` |
| 31 | + const body = await readFile(absolutePath) |
| 32 | + const contentType = fileName.endsWith('.gz') ? 'application/gzip' : 'text/plain; charset=utf-8' |
| 33 | + |
| 34 | + await s3Storage.putFile(key, body, contentType) |
| 35 | + |
| 36 | + if (shouldDeleteAfterUpload) { |
| 37 | + await unlink(absolutePath) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +const uploadPendingLogFilesToS3 = async (logFolder: string, s3Storage: S3Storage): Promise<void> => { |
| 42 | + const entries = await readdir(logFolder, { withFileTypes: true }) |
| 43 | + |
| 44 | + for (const entry of entries) { |
| 45 | + if (!entry.isFile()) continue |
| 46 | + await uploadLogFileToS3(logFolder, entry.name, s3Storage) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const cleanupStaleLogFiles = async (logFolder: string): Promise<void> => { |
| 51 | + const cutoffTimestamp = Date.now() - ProcessEnv.logRetentionDays * 24 * 60 * 60 * 1000 |
| 52 | + const entries = await readdir(logFolder, { withFileTypes: true }) |
| 53 | + |
| 54 | + for (const entry of entries) { |
| 55 | + if (!entry.isFile()) continue |
| 56 | + |
| 57 | + const absolutePath = path.join(logFolder, entry.name) |
| 58 | + const fileStats = await stat(absolutePath) |
| 59 | + if (fileStats.mtimeMs < cutoffTimestamp) { |
| 60 | + await rm(absolutePath, { force: true }) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +const uploadLogFilesToS3 = async (): Promise<void> => { |
| 66 | + if (!ProcessEnv.logS3Enabled) return |
| 67 | + |
| 68 | + const logFolder = await withLogFolder() |
| 69 | + const s3Storage = S3Storage.fromEnvironment() |
| 70 | + if (!s3Storage) return |
| 71 | + |
| 72 | + await uploadPendingLogFilesToS3(logFolder, s3Storage) |
| 73 | + await cleanupStaleLogFiles(logFolder) |
| 74 | +} |
| 75 | + |
| 76 | +export const startLogUploadPolling = (): void => { |
| 77 | + if (!ProcessEnv.logS3Enabled) return |
| 78 | + |
| 79 | + void uploadLogFilesToS3() |
| 80 | + const timer = setInterval(() => { |
| 81 | + void uploadLogFilesToS3() |
| 82 | + }, ProcessEnv.logUploadIntervalMs) |
| 83 | + timer.unref?.() |
| 84 | +} |
0 commit comments