fix: 出证连接失败时停止查询并允许重试
This commit is contained in:
@@ -50,6 +50,19 @@ describe('Consumer certificate lifecycle', () => {
|
||||
await service.process(row);
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
});
|
||||
it('a refused connection on submit fails instead of querying an unsent deposit', async () => {
|
||||
const { service, row, prisma } = fixture();
|
||||
const cause = Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:443'), { code: 'ECONNREFUSED' });
|
||||
const failure = new TypeError('fetch failed');
|
||||
(failure as { cause?: unknown }).cause = cause;
|
||||
jest.spyOn(DepositProvider.prototype, 'submit').mockRejectedValue(failure);
|
||||
await service.process(row);
|
||||
expect(prisma.blockchainProof.updateMany.mock.calls[1][0].data).toMatchObject({
|
||||
certificateState: 'FAILED',
|
||||
certificateError: '暂时联系不上存证服务,证书没有生成。',
|
||||
certificateNextCheck: null,
|
||||
});
|
||||
});
|
||||
it('a submission timeout changes to query-only, preserving the exact operation id', async () => {
|
||||
const { service, row, prisma } = fixture();
|
||||
const submit = jest.spyOn(DepositProvider.prototype, 'submit').mockRejectedValue(new Error('timeout'));
|
||||
|
||||
@@ -19,6 +19,19 @@ interface CertificateRequest {
|
||||
|
||||
const ACTIVE_STATES = ['PENDING', 'SUBMITTING', 'WAITING', 'CHECKING'];
|
||||
const MAX_CHECKS = 8;
|
||||
const UNREACHABLE_MESSAGE = '暂时联系不上存证服务,证书没有生成。';
|
||||
|
||||
function depositUnreachable(error: unknown) {
|
||||
const texts: string[] = [];
|
||||
let current: unknown = error;
|
||||
for (let depth = 0; depth < 3 && current instanceof Error; depth += 1) {
|
||||
texts.push(current.message);
|
||||
const code = (current as { code?: unknown }).code;
|
||||
if (typeof code === 'string') texts.push(code);
|
||||
current = (current as { cause?: unknown }).cause;
|
||||
}
|
||||
return /ECONNREFUSED|ENOTFOUND|EAI_AGAIN|EHOSTUNREACH|ECONNRESET/.test(texts.join(' '));
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DepositCertificateService {
|
||||
@@ -46,10 +59,12 @@ export class DepositCertificateService {
|
||||
const certificate = request && proof.certificateState === 'READY'
|
||||
? presentStoredCertificate(proof.certificateUrl, request.environment, request.address, request.text, await this.settings.get('certificateAuthorMode'), artist)
|
||||
: null;
|
||||
const status = proof.certificateState === 'READY' && !certificate ? 'FAILED' : proof.certificateState;
|
||||
return {
|
||||
status: proof.certificateState === 'READY' && !certificate ? 'FAILED' : proof.certificateState,
|
||||
status,
|
||||
generatedAt: proof.certificateGeneratedAt,
|
||||
certificate: certificate && request ? { ...certificate, environment: request.environment } : certificate,
|
||||
message: status === 'READY' ? null : proof.certificateError,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -63,7 +78,8 @@ export class DepositCertificateService {
|
||||
const { code, proof } = await this.eligible(shortCode);
|
||||
if (proof.certificateState === 'READY') return this.view(proof, code.artwork.artist);
|
||||
const settings = await this.settings.getAllRaw();
|
||||
if (proof.certificateState !== 'NONE') {
|
||||
const retryUnreachable = proof.certificateState === 'FAILED' && !proof.certificateUrl && proof.certificateError === UNREACHABLE_MESSAGE;
|
||||
if (proof.certificateState !== 'NONE' && !retryUnreachable) {
|
||||
// An uncertain submission is never submitted again. User action only resumes queries.
|
||||
if (proof.certificateState === 'UNKNOWN' && (!proof.certificateNextCheck || proof.certificateNextCheck <= new Date())) {
|
||||
await this.prisma.blockchainProof.updateMany({
|
||||
@@ -103,10 +119,10 @@ export class DepositCertificateService {
|
||||
// A single conditional write claims this item; concurrent callers cannot
|
||||
// replace the winner's frozen evidence or operation id. No daily quota.
|
||||
await this.prisma.blockchainProof.updateMany({
|
||||
where: { id: proof.id, certificateState: 'NONE', status: ProofStatus.ONCHAIN },
|
||||
where: { id: proof.id, certificateState: retryUnreachable ? 'FAILED' : 'NONE', status: ProofStatus.ONCHAIN },
|
||||
data: {
|
||||
certificateState: 'PENDING', certificateRequest: request as unknown as Prisma.InputJsonObject,
|
||||
certificateRequestedAt: now, certificateNextCheck: now,
|
||||
certificateError: null, certificateRequestedAt: now, certificateNextCheck: now,
|
||||
},
|
||||
});
|
||||
return this.view(await this.prisma.blockchainProof.findUniqueOrThrow({ where: { id: proof.id } }), code.artwork.artist);
|
||||
@@ -184,9 +200,13 @@ export class DepositCertificateService {
|
||||
certificateNextCheck: new Date(Date.now() + 30_000) });
|
||||
} catch (error) {
|
||||
const rejected = submit && !accepted && error instanceof DepositRejected;
|
||||
await finish({ certificateState: rejected ? 'FAILED' : attempts >= MAX_CHECKS ? 'UNKNOWN' : 'WAITING',
|
||||
certificateAttempts: attempts, certificateError: rejected ? '存证提交被拒绝,请联系平台管理员' : '远端结果暂未确认,后续仅查询原存证',
|
||||
certificateNextCheck: new Date(Date.now() + 30_000) });
|
||||
const unreachable = submit && !accepted && depositUnreachable(error);
|
||||
await finish({
|
||||
certificateState: rejected || unreachable ? 'FAILED' : attempts >= MAX_CHECKS ? 'UNKNOWN' : 'WAITING',
|
||||
certificateAttempts: attempts,
|
||||
certificateError: rejected ? '存证提交被拒绝,请联系平台管理员' : unreachable ? UNREACHABLE_MESSAGE : '远端结果暂未确认,后续仅查询原存证',
|
||||
certificateNextCheck: rejected || unreachable ? null : new Date(Date.now() + 30_000),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ export interface TraceCertificateResult {
|
||||
status: 'NONE' | 'PENDING' | 'SUBMITTING' | 'CHECKING' | 'WAITING' | 'READY' | 'UNKNOWN' | 'FAILED';
|
||||
generatedAt: string | null;
|
||||
certificate: TraceCertificateRecord | null;
|
||||
message?: string | null;
|
||||
}
|
||||
export function requestTraceCertificate(shortCode: string): Promise<TraceCertificateResult> {
|
||||
return instance.post(`/public/trace/${encodeURIComponent(shortCode)}/certificate`, {}, { timeout: 90_000 }) as unknown as Promise<TraceCertificateResult>;
|
||||
|
||||
@@ -89,7 +89,7 @@
|
||||
"certificatePreview": "预览模式不生成证书。",
|
||||
"certificateRequestFailed": "暂时无法生成证书,请稍后再试或联系平台。",
|
||||
"certificateCheckAgain": "重新查询",
|
||||
"certificateFailed": "存证申请未成功,请联系平台处理。",
|
||||
"certificateFailed": "暂时联系不上存证服务,证书没有生成。",
|
||||
"certificateUnavailable": "暂时无法查询证书,请稍后重试。",
|
||||
"certificateRetrieving": "正在调取存证数据",
|
||||
"certificateAcquiring": "获取存证证书",
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<p v-if="preview" class="subpage-note">{{ t('chain.certificatePreview') }}</p>
|
||||
<template v-else>
|
||||
<p v-if="certificateError" class="subpage-note" role="alert">{{ certificateError }}</p>
|
||||
<button v-if="certificateError && retryable" type="button" class="subpage-action" @click="load">
|
||||
<button v-if="certificateError && retryable" type="button" class="subpage-action" @click="load(true)">
|
||||
{{ t('chain.certificateCheckAgain') }}
|
||||
</button>
|
||||
<template v-else-if="certificateRecord">
|
||||
@@ -103,7 +103,11 @@ async function showCertificateResult(result: TraceCertificateResult, run: number
|
||||
measure();
|
||||
return;
|
||||
}
|
||||
if (result.status === 'FAILED') return failCertificate('chain.certificateFailed', false);
|
||||
if (result.status === 'FAILED') {
|
||||
certificateError.value = result.message || t('chain.certificateFailed');
|
||||
retryable.value = true;
|
||||
return;
|
||||
}
|
||||
if (result.status === 'UNKNOWN') return failCertificate('chain.certificateDelayed');
|
||||
if (result.status === 'NONE') return failCertificate('chain.certificateUnavailable');
|
||||
certificatePhase.value = 1;
|
||||
@@ -113,7 +117,7 @@ async function showCertificateResult(result: TraceCertificateResult, run: number
|
||||
catch { if (run === certificateRun) failCertificate('chain.certificateUnavailable'); }
|
||||
}, 5000);
|
||||
}
|
||||
async function load() {
|
||||
async function load(retry = false) {
|
||||
if (preview) return;
|
||||
certificateRun += 1;
|
||||
if (certificateTimer) clearTimeout(certificateTimer);
|
||||
@@ -127,7 +131,7 @@ async function load() {
|
||||
const cached = await fetchTraceCertificate(code);
|
||||
if (run !== certificateRun) return;
|
||||
certificatePhase.value = 1;
|
||||
const result = cached.status === 'NONE' || cached.status === 'UNKNOWN'
|
||||
const result = retry || cached.status === 'NONE' || cached.status === 'UNKNOWN'
|
||||
? await requestTraceCertificate(code) : cached;
|
||||
showCertificateResult(result, run);
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user