Montify: mark sync running before responding; no 400 when already running

This commit is contained in:
2026-10-10 16:18:25 +03:30
parent e82bd2b2df
commit f27b4c264a
@@ -156,10 +156,11 @@ export class MontifySyncService {
): Promise<MontifyConnectionDto> {
const row = await this.connections.getOrThrow(organizationId, connectionId);
if (!row.enabled) throw new BadRequestException('این اتصال غیرفعال است');
if (this.running.has(connectionId)) {
throw new BadRequestException('همگام‌سازی این فروشگاه در حال اجراست');
if (!this.running.has(connectionId)) {
await new Promise<void>((started) => {
void this.runSync(row, mode, started);
});
}
void this.runSync(row, mode);
return this.connections.getDto(organizationId, connectionId);
}
@@ -175,8 +176,15 @@ export class MontifySyncService {
}
}
private async runSync(row: MontifyConnection, mode: 'incremental' | 'full'): Promise<void> {
if (this.running.has(row.id)) return;
private async runSync(
row: MontifyConnection,
mode: 'incremental' | 'full',
onStarted?: () => void,
): Promise<void> {
if (this.running.has(row.id)) {
onStarted?.();
return;
}
this.running.add(row.id);
const startedAt = new Date();
const result: MontifySyncResultDto = {
@@ -196,14 +204,22 @@ export class MontifySyncService {
data: { lastSyncResult: result as unknown as Prisma.InputJsonValue },
});
await this.prisma.montifyConnection.update({
where: { id: row.id },
data: {
syncStatus: 'running',
syncStartedAt: startedAt,
lastSyncResult: result as unknown as Prisma.InputJsonValue,
},
});
try {
await this.prisma.montifyConnection.update({
where: { id: row.id },
data: {
syncStatus: 'running',
syncStartedAt: startedAt,
lastSyncResult: result as unknown as Prisma.InputJsonValue,
},
});
} catch (e) {
this.running.delete(row.id);
this.logger.warn(`Montify sync ${row.id} start: ${e instanceof Error ? e.message : e}`);
return;
} finally {
onStarted?.();
}
let cursor = row.customersCursor;
let fatal: string | null = null;