Skip to content

Commit

Permalink
Merge pull request #560 from meetqy/552-bug-素材批量删除会生成未同步记录
Browse files Browse the repository at this point in the history
552 bug 素材批量删除会生成未同步记录
  • Loading branch information
meetqy authored Dec 7, 2023
2 parents 790ca5f + 6e905d2 commit a0166b2
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 68 deletions.
2 changes: 1 addition & 1 deletion apps/electron/electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ process.env.SENTRY_AUTH_TOKEN =
export default defineConfig({
main: {
build: {
sourcemap: IS_DEV ? "inline" : true,
sourcemap: !IS_DEV,
},
esbuild: {
drop: IS_DEV ? undefined : ["console", "debugger"],
Expand Down
6 changes: 4 additions & 2 deletions packages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { config, configCore } from "./src/config";
import { folder, folderCore } from "./src/folder";
import { image, imageCore } from "./src/image";
import { library, libraryCore } from "./src/library";
import { log } from "./src/log";
import { pending } from "./src/pending";
import { log, logCore } from "./src/log";
import { pending, pendingCore } from "./src/pending";
import { sync } from "./src/sync";
import { tag } from "./src/tag";
import { t } from "./src/utils";
Expand All @@ -26,6 +26,8 @@ export const routerCore = {
folder: folderCore,
image: imageCore,
library: libraryCore,
pending: pendingCore,
log: logCore,
};

export type AppRouter = typeof router;
Expand Down
36 changes: 20 additions & 16 deletions packages/api/src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const ee = new EventEmitter();

let watcher: chokidar.FSWatcher | null = null;

export const libraryInput = {
update: z.object({
lastSyncTime: z.date().optional(),
}),
};

export const libraryCore = {
findUnique: async () => {
const [library, pendingCount, syncCount, trashCount, unSyncCount] =
Expand All @@ -39,6 +45,18 @@ export const libraryCore = {
trashCount,
};
},

update: async (input: z.infer<(typeof libraryInput)["update"]>) => {
const json: Prisma.LibraryUpdateManyMutationInput = {};

if (input.lastSyncTime) {
json.lastSyncTime = input.lastSyncTime;
}

return await prisma.library.updateMany({
data: json,
});
},
};

export const library = t.router({
Expand Down Expand Up @@ -67,22 +85,8 @@ export const library = t.router({
}),

update: t.procedure
.input(
z.object({
lastSyncTime: z.date().optional(),
}),
)
.mutation(async ({ input }) => {
const json: Prisma.LibraryUpdateManyMutationInput = {};

if (input.lastSyncTime) {
json.lastSyncTime = input.lastSyncTime;
}

return await prisma.library.updateMany({
data: json,
});
}),
.input(libraryInput.update)
.mutation(async ({ input }) => libraryCore.update(input)),

delete: t.procedure.mutation(async () => {
if (watcher) {
Expand Down
34 changes: 20 additions & 14 deletions packages/api/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import { prisma } from "@rao-pics/db";

import { t } from "./utils";

export const logInput = {
upsert: z.object({
path: z.string(),
type: LogTypeEnumZod,
message: z.string(),
}),
};

export const logCore = {
upsert: async (input: z.infer<(typeof logInput)["upsert"]>) => {
return await prisma.log.upsert({
where: { path: input.path },
create: input,
update: input,
});
},
};

export const log = t.router({
upsert: t.procedure
.input(
z.object({
path: z.string(),
type: LogTypeEnumZod,
message: z.string(),
}),
)
.mutation(async ({ input }) => {
return await prisma.log.upsert({
where: { path: input.path },
create: input,
update: input,
});
}),
.input(logInput.upsert)
.mutation(async ({ input }) => logCore.upsert(input)),

delete: t.procedure.input(z.string()).mutation(async ({ input }) => {
return await prisma.log.deleteMany({ where: { path: input } });
Expand Down
54 changes: 34 additions & 20 deletions packages/api/src/pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,45 @@ import { prisma } from "@rao-pics/db";

import { t } from "./utils";

export const pending = t.router({
upsert: t.procedure
.input(
z.object({
path: z.string(),
type: PendingTypeEnumZod,
}),
)
.mutation(async ({ input }) => {
return await prisma.pending.upsert({
where: { path: input.path },
create: input,
update: input,
});
}),

get: t.procedure.input(z.string().optional()).query(async ({ input }) => {
export const pendingInput = {
upsert: z.object({
path: z.string(),
type: PendingTypeEnumZod,
}),
};

export const pendingCore = {
upsert: async (input: z.infer<(typeof pendingInput)["upsert"]>) => {
return await prisma.pending.upsert({
where: { path: input.path },
create: input,
update: input,
});
},

get: async (input?: string) => {
if (input) {
return await prisma.pending.findUnique({ where: { path: input } });
}

return await prisma.pending.findMany();
}),
},

delete: t.procedure.input(z.string()).mutation(async ({ input }) => {
delete: async (input: string) => {
return await prisma.pending.delete({ where: { path: input } });
}),
},
};

export const pending = t.router({
upsert: t.procedure
.input(pendingInput.upsert)
.mutation(({ input }) => pendingCore.upsert(input)),

get: t.procedure
.input(z.string().optional())
.query(({ input }) => pendingCore.get(input)),

delete: t.procedure
.input(z.string())
.mutation(({ input }) => pendingCore.delete(input)),
});
24 changes: 11 additions & 13 deletions packages/api/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { prisma } from "@rao-pics/db";
import type { Pending } from "@rao-pics/db";
import { RLogger } from "@rao-pics/rlog";

import { router, routerCore } from "../..";
import { routerCore } from "../..";
import { configCore } from "../config";
import { folderCore } from "../folder";
import { t } from "../utils";
Expand Down Expand Up @@ -51,16 +51,15 @@ export const sync = t.router({
}),
)
.mutation(async ({ input }) => {
const caller = router.createCaller({});
const pendings = (await caller.pending.get()) as Pending[];
const pendings = (await routerCore.pending.get()) as Pending[];

// 同步文件夹
await syncFolder(join(input.libraryPath, "metadata.json"));

await syncImage(pendings);

// 同步完成自动更新 library lastSyncTime
await caller.library.update({ lastSyncTime: new Date() });
await routerCore.library.update({ lastSyncTime: new Date() });
return true;
}),

Expand Down Expand Up @@ -153,9 +152,6 @@ export const syncImage = async (pendings: Pending[]) => {
ee.emit("sync.start", { status: "ok", type: "image", count });
break;
}

// 删除 pending
await router.createCaller({}).pending.delete(p.path);
} catch (e) {
ee.emit("sync.start", {
status: "error",
Expand All @@ -165,24 +161,22 @@ export const syncImage = async (pendings: Pending[]) => {
});

const errorMsg = (e as Error).message.match(/\[(?<type>.*)\]/);
const caller = router.createCaller({});
if (errorMsg) {
const type = errorMsg[0].replace(/\[|\]/g, "");
await caller.log.upsert({
await routerCore.log.upsert({
path: p.path,
type: type as never,
message: (e as Error).message,
});
} else {
await caller.log.upsert({
await routerCore.log.upsert({
path: p.path,
type: "unknown",
message: (e as Error).stack ?? JSON.stringify(e),
});
}

await router.createCaller({}).pending.delete(p.path);

// continue; 当 return 使用
if (e instanceof Error) {
// 自定义 sync_error 无需上报
if (e.cause === "sync_error") {
Expand All @@ -201,7 +195,11 @@ export const syncImage = async (pendings: Pending[]) => {
}

RLogger.error<typeof e>(e, "syncImage");
continue;
} finally {
// 删除 pending
routerCore.pending.delete(p.path).catch((e) => {
RLogger.warning(e, "syncImage delete pending");
});
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import fs from "fs-extra";

import { DB_PATH, IS_DEV } from "@rao-pics/constant/server";

/**
* connection_limit=1 解决 prisma.*.delete 报错
* prisma.pending.delete({ where: { path: input } }); 报错
* https://www.prisma.io/docs/guides/performance-and-optimization/connection-management
* https://github.com/prisma/prisma/issues/11789
* FIX: https://github.com/meetqy/rao-pics/issues/552
*/

const _prisma: PrismaClient = new PrismaClient(
!IS_DEV
? {
datasources: {
db: { url: `file:${DB_PATH}` },
db: { url: `file:${DB_PATH}?connection_limit=1` },
},
}
: undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ generator client {

datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
url = "file:./db.sqlite?connection_limit=1"
}

model Config {
Expand Down

0 comments on commit a0166b2

Please sign in to comment.