mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
fix(plugins): revert lock usage back for some plugins
This commit is contained in:
parent
9e8436ce19
commit
ac0bae6be9
@ -14,6 +14,7 @@
|
||||
"@formily/react": "2.x",
|
||||
"@formily/shared": "2.x",
|
||||
"@types/node-xlsx": "^0.15.1",
|
||||
"async-mutex": "^0.5.0",
|
||||
"file-saver": "^2.0.5",
|
||||
"node-xlsx": "^0.16.1",
|
||||
"react": "^18.2.0",
|
||||
|
@ -12,9 +12,10 @@ import { Repository } from '@nocobase/database';
|
||||
|
||||
import XlsxExporter from '../xlsx-exporter';
|
||||
import XLSX from 'xlsx';
|
||||
import { Mutex } from 'async-mutex';
|
||||
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) {
|
||||
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) {
|
||||
const plugin = ctx.app.pm.get(PluginActionExportServer) as PluginActionExportServer;
|
||||
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(
|
||||
ctx.t(`another export action is running, please try again later.`, {
|
||||
ns: 'action-export',
|
||||
}),
|
||||
{
|
||||
cause: error,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (mutex.isLocked()) {
|
||||
throw new Error(
|
||||
ctx.t(`another export action is running, please try again later.`, {
|
||||
ns: 'action-export',
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const release = await lock.acquire(5000);
|
||||
const release = await mutex.acquire();
|
||||
|
||||
try {
|
||||
await exportXlsxAction(ctx, next);
|
||||
|
@ -18,6 +18,7 @@
|
||||
"@koa/multer": "^3.0.2",
|
||||
"@types/node-xlsx": "^0.15.1",
|
||||
"antd": "5.x",
|
||||
"async-mutex": "^0.5.0",
|
||||
"file-saver": "^2.0.5",
|
||||
"mathjs": "^10.6.0",
|
||||
"node-xlsx": "^0.16.1",
|
||||
|
@ -10,13 +10,14 @@
|
||||
import { Context, Next } from '@nocobase/actions';
|
||||
import { Repository } from '@nocobase/database';
|
||||
import XLSX from 'xlsx';
|
||||
import { Mutex } from 'async-mutex';
|
||||
import { XlsxImporter } from '../services/xlsx-importer';
|
||||
import { DataSource } from '@nocobase/data-source-manager';
|
||||
import PluginActionImportServer from '..';
|
||||
import { LockAcquireError } from '@nocobase/lock-manager';
|
||||
|
||||
const IMPORT_LIMIT_COUNT = 2000;
|
||||
|
||||
const mutex = new Mutex();
|
||||
|
||||
async function importXlsxAction(ctx: Context, next: Next) {
|
||||
let columns = (ctx.request.body as any).columns as any[];
|
||||
if (typeof columns === 'string') {
|
||||
@ -59,24 +60,15 @@ async function importXlsxAction(ctx: Context, next: Next) {
|
||||
}
|
||||
|
||||
export async function importXlsx(ctx: Context, next: Next) {
|
||||
const plugin = ctx.app.pm.get(PluginActionImportServer) as PluginActionImportServer;
|
||||
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(
|
||||
ctx.t(`another import action is running, please try again later.`, {
|
||||
ns: 'action-import',
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (mutex.isLocked()) {
|
||||
throw new Error(
|
||||
ctx.t(`another export action is running, please try again later.`, {
|
||||
ns: 'action-export',
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const release = await lock.acquire(5000);
|
||||
const release = await mutex.acquire();
|
||||
|
||||
try {
|
||||
await importXlsxAction(ctx, next);
|
||||
|
Loading…
x
Reference in New Issue
Block a user