mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-01-31 15:37:09 +01:00
Logging (#131)
* ci: pull version from package.json on build * fix: implicit any type * feat: inital support for logger * style: fix lint * feat: move more logging over to pino * fix: logging around company importing
This commit is contained in:
@@ -9,6 +9,8 @@ import prisma from "../db/database";
|
||||
import cacheHandler from "../cache";
|
||||
import { systemConfig } from "../config/sys-conf";
|
||||
import { type } from "arktype";
|
||||
import { logger } from "~/server/internal/logging";
|
||||
import type pino from "pino";
|
||||
|
||||
export class FsObjectBackend extends ObjectBackend {
|
||||
private baseObjectPath: string;
|
||||
@@ -121,7 +123,7 @@ export class FsObjectBackend extends ObjectBackend {
|
||||
const metadataRaw = JSON.parse(fs.readFileSync(metadataPath, "utf-8"));
|
||||
const metadata = objectMetadata(metadataRaw);
|
||||
if (metadata instanceof type.errors) {
|
||||
console.error("FsObjectBackend#fetchMetadata", metadata.summary);
|
||||
logger.error("FsObjectBackend#fetchMetadata", metadata.summary);
|
||||
return undefined;
|
||||
}
|
||||
await this.metadataCache.set(id, metadata);
|
||||
@@ -175,23 +177,27 @@ export class FsObjectBackend extends ObjectBackend {
|
||||
return fs.readdirSync(this.baseObjectPath);
|
||||
}
|
||||
|
||||
async cleanupMetadata() {
|
||||
async cleanupMetadata(taskLogger: pino.Logger) {
|
||||
const cleanupLogger = taskLogger ?? logger;
|
||||
|
||||
const metadataFiles = fs.readdirSync(this.baseMetadataPath);
|
||||
const objects = await this.listAll();
|
||||
|
||||
const extraFiles = metadataFiles.filter(
|
||||
(file) => !objects.includes(file.replace(/\.json$/, "")),
|
||||
);
|
||||
console.log(
|
||||
cleanupLogger.info(
|
||||
`[FsObjectBackend#cleanupMetadata]: Found ${extraFiles.length} metadata files without corresponding objects.`,
|
||||
);
|
||||
for (const file of extraFiles) {
|
||||
const filePath = path.join(this.baseMetadataPath, file);
|
||||
try {
|
||||
fs.rmSync(filePath);
|
||||
console.log(`[FsObjectBackend#cleanupMetadata]: Removed ${file}`);
|
||||
cleanupLogger.info(
|
||||
`[FsObjectBackend#cleanupMetadata]: Removed ${file}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
cleanupLogger.error(
|
||||
`[FsObjectBackend#cleanupMetadata]: Failed to remove ${file}`,
|
||||
error,
|
||||
);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
import { type } from "arktype";
|
||||
import { parse as getMimeTypeBuffer } from "file-type-mime";
|
||||
import type pino from "pino";
|
||||
import type { Writable } from "stream";
|
||||
import { Readable } from "stream";
|
||||
import { getMimeType as getMimeTypeStream } from "stream-mime-type";
|
||||
@@ -71,7 +72,7 @@ export abstract class ObjectBackend {
|
||||
): Promise<boolean>;
|
||||
abstract fetchHash(id: ObjectReference): Promise<string | undefined>;
|
||||
abstract listAll(): Promise<string[]>;
|
||||
abstract cleanupMetadata(): Promise<void>;
|
||||
abstract cleanupMetadata(taskLogger: pino.Logger): Promise<void>;
|
||||
}
|
||||
|
||||
export class ObjectHandler {
|
||||
@@ -264,7 +265,7 @@ export class ObjectHandler {
|
||||
* This is useful for cleaning up metadata files that are left behinds
|
||||
* @returns
|
||||
*/
|
||||
async cleanupMetadata() {
|
||||
return await this.backend.cleanupMetadata();
|
||||
async cleanupMetadata(taskLogger: pino.Logger) {
|
||||
return await this.backend.cleanupMetadata(taskLogger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,9 +43,9 @@ export class ObjectTransactionalHandler {
|
||||
|
||||
for (const [id, data] of transaction) {
|
||||
if (typeof data === "string") {
|
||||
context?.log(`Importing object from "${data}"`);
|
||||
context?.logger.info(`Importing object from "${data}"`);
|
||||
} else {
|
||||
context?.log(`Importing raw object...`);
|
||||
context?.logger.info(`Importing raw object...`);
|
||||
}
|
||||
await objectHandler.createFromSource(
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user