fix(plugins): revert lock usage back for some plugins

This commit is contained in:
mytharcher 2024-08-16 06:49:53 +00:00
parent 9e8436ce19
commit ac0bae6be9
4 changed files with 22 additions and 39 deletions

View File

@ -14,6 +14,7 @@
"@formily/react": "2.x", "@formily/react": "2.x",
"@formily/shared": "2.x", "@formily/shared": "2.x",
"@types/node-xlsx": "^0.15.1", "@types/node-xlsx": "^0.15.1",
"async-mutex": "^0.5.0",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"node-xlsx": "^0.16.1", "node-xlsx": "^0.16.1",
"react": "^18.2.0", "react": "^18.2.0",

View File

@ -12,9 +12,10 @@ import { Repository } from '@nocobase/database';
import XlsxExporter from '../xlsx-exporter'; import XlsxExporter from '../xlsx-exporter';
import XLSX from 'xlsx'; import XLSX from 'xlsx';
import { Mutex } from 'async-mutex';
import { DataSource } from '@nocobase/data-source-manager'; import { DataSource } from '@nocobase/data-source-manager';
import PluginActionExportServer from '..';
import { LockAcquireError } from '@nocobase/lock-manager'; const mutex = new Mutex();
async function exportXlsxAction(ctx: Context, next: Next) { async function exportXlsxAction(ctx: Context, next: Next) {
const { title, filter, sort, fields, except } = ctx.action.params; const { title, filter, sort, fields, except } = ctx.action.params;
@ -52,27 +53,15 @@ async function exportXlsxAction(ctx: Context, next: Next) {
} }
export async function exportXlsx(ctx: Context, next: Next) { export async function exportXlsx(ctx: Context, next: Next) {
const plugin = ctx.app.pm.get(PluginActionExportServer) as PluginActionExportServer; if (mutex.isLocked()) {
const { collection } = ctx.getCurrentRepository();
const dataSource = ctx.dataSource as DataSource;
const lockKey = `${plugin.name}:${dataSource.name}:${collection.name}`;
let lock;
try {
lock = ctx.app.lockManager.tryAcquire(lockKey);
} catch (error) {
if (error instanceof LockAcquireError) {
throw new Error( throw new Error(
ctx.t(`another export action is running, please try again later.`, { ctx.t(`another export action is running, please try again later.`, {
ns: 'action-export', ns: 'action-export',
}), }),
{
cause: error,
},
); );
} }
}
const release = await lock.acquire(5000); const release = await mutex.acquire();
try { try {
await exportXlsxAction(ctx, next); await exportXlsxAction(ctx, next);

View File

@ -18,6 +18,7 @@
"@koa/multer": "^3.0.2", "@koa/multer": "^3.0.2",
"@types/node-xlsx": "^0.15.1", "@types/node-xlsx": "^0.15.1",
"antd": "5.x", "antd": "5.x",
"async-mutex": "^0.5.0",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"mathjs": "^10.6.0", "mathjs": "^10.6.0",
"node-xlsx": "^0.16.1", "node-xlsx": "^0.16.1",

View File

@ -10,13 +10,14 @@
import { Context, Next } from '@nocobase/actions'; import { Context, Next } from '@nocobase/actions';
import { Repository } from '@nocobase/database'; import { Repository } from '@nocobase/database';
import XLSX from 'xlsx'; import XLSX from 'xlsx';
import { Mutex } from 'async-mutex';
import { XlsxImporter } from '../services/xlsx-importer'; import { XlsxImporter } from '../services/xlsx-importer';
import { DataSource } from '@nocobase/data-source-manager'; import { DataSource } from '@nocobase/data-source-manager';
import PluginActionImportServer from '..';
import { LockAcquireError } from '@nocobase/lock-manager';
const IMPORT_LIMIT_COUNT = 2000; const IMPORT_LIMIT_COUNT = 2000;
const mutex = new Mutex();
async function importXlsxAction(ctx: Context, next: Next) { async function importXlsxAction(ctx: Context, next: Next) {
let columns = (ctx.request.body as any).columns as any[]; let columns = (ctx.request.body as any).columns as any[];
if (typeof columns === 'string') { if (typeof columns === 'string') {
@ -59,24 +60,15 @@ async function importXlsxAction(ctx: Context, next: Next) {
} }
export async function importXlsx(ctx: Context, next: Next) { export async function importXlsx(ctx: Context, next: Next) {
const plugin = ctx.app.pm.get(PluginActionImportServer) as PluginActionImportServer; if (mutex.isLocked()) {
const { collection } = ctx.getCurrentRepository();
const dataSource = ctx.dataSource as DataSource;
const lockKey = `${plugin.name}:${dataSource.name}:${collection.name}`;
let lock;
try {
lock = ctx.app.lockManager.tryAcquire(lockKey);
} catch (error) {
if (error instanceof LockAcquireError) {
throw new Error( throw new Error(
ctx.t(`another import action is running, please try again later.`, { ctx.t(`another export action is running, please try again later.`, {
ns: 'action-import', ns: 'action-export',
}), }),
); );
} }
}
const release = await lock.acquire(5000); const release = await mutex.acquire();
try { try {
await importXlsxAction(ctx, next); await importXlsxAction(ctx, next);