fix: 隔离OA审批接口单条失败 #777

Merged
yueyang merged 1 commits from fix/yueyang-oa-approver-api-resilience into main 2026-10-29 22:27:29 +08:00
2 changed files with 79 additions and 3 deletions

View File

@ -27,6 +27,8 @@ class DingProcessDetailMirrorSynchronizer
private const APPROVAL_API_BACKLOG_BATCH_LIMIT = 750;
private const APPROVAL_API_MAX_CONSECUTIVE_FAILURES = 5;
private const BUSINESS_ID_SYNC_LIMIT = 50;
private readonly ConnectionInterface $connection;
@ -35,6 +37,8 @@ class DingProcessDetailMirrorSynchronizer
private bool $approvalApiAvailable = true;
private int $approvalApiFailureStreak = 0;
/** @var array<string,array<string,mixed>> */
private array $approvalSnapshots = [];
@ -63,6 +67,7 @@ class DingProcessDetailMirrorSynchronizer
{
$this->approvalApiAttempts = 0;
$this->approvalApiAvailable = true;
$this->approvalApiFailureStreak = 0;
$this->approvalSnapshots = [];
$scope = $this->scope();
$state = $this->state();
@ -172,6 +177,7 @@ class DingProcessDetailMirrorSynchronizer
$this->approvalApiAttempts = 0;
$this->approvalApiAvailable = true;
$this->approvalApiFailureStreak = 0;
$this->approvalSnapshots = [];
$scope = $this->scope();
$state = $this->state();
@ -1304,13 +1310,20 @@ class DingProcessDetailMirrorSynchronizer
$this->approvalApiAttempts++;
try {
return $this->approvalSnapshots[$processInstanceId] =
$this->approvalClient->processSnapshot($processInstanceId);
$snapshot = $this->approvalClient->processSnapshot($processInstanceId);
$this->approvalApiFailureStreak = 0;
return $this->approvalSnapshots[$processInstanceId] = $snapshot;
} catch (Throwable $exception) {
$this->approvalApiAvailable = false;
$this->approvalApiFailureStreak++;
if ($this->approvalApiFailureStreak
>= self::APPROVAL_API_MAX_CONSECUTIVE_FAILURES) {
$this->approvalApiAvailable = false;
}
Log::warning('钉钉官方审批接口读取流程状态失败。', [
'process_instance_id' => $processInstanceId,
'exception' => $exception::class,
'consecutive_failures' => $this->approvalApiFailureStreak,
]);
return null;

View File

@ -383,6 +383,63 @@ class DingProcessDetailMirrorSynchronizerTest extends TestCase
$this->assertSame('not_applicable', $row->payment_source_state);
}
public function test_one_failed_approval_snapshot_does_not_block_later_workflows(): void
{
config([
'upstream_mirrors.ding_process_details.processes.PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF' => [
'document_type' => '种菜核销',
'detail_table' => '收款费用明细',
'detail_amount_fields' => ['代发订单金额'],
'amount_total_fields' => ['收款合计'],
],
]);
$this->query->workflows = [[
'processInstanceId' => 'process-snapshot-fails',
'processCode' => 'PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF',
'businessId' => '202610291930000052467',
'title' => '苏木提交的种菜核销种菜报销流程',
'status' => 'RUNNING',
'result' => 'NONE',
'originatorUserId' => 'user-1',
'joinAt' => '2026-10-29 19:30:00',
], [
'processInstanceId' => 'process-snapshot-succeeds',
'processCode' => 'PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF',
'businessId' => '202610291931000052468',
'title' => '苏木提交的种菜核销种菜报销流程',
'status' => 'RUNNING',
'result' => 'NONE',
'originatorUserId' => 'user-1',
'joinAt' => '2026-10-29 19:31:00',
]];
$this->query->scheme = [
'收款费用明细' => json_encode([[
'rowValue' => [
['label' => '代发订单金额', 'value' => '100.00'],
],
]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'收款合计' => 100,
];
$this->approvalClient->failingInstances = ['process-snapshot-fails'];
$this->approvalClient->tasksByInstance['process-snapshot-succeeds'] = [[
'taskGroupName' => '行政核价',
'status' => 'RUNNING',
'result' => 'NONE',
'userId' => 'user-2',
'createTime' => '2026-10-29T19:32Z',
'finishTime' => '',
]];
$this->synchronizer->sync(true);
$rows = DB::connection('business_mirror')
->table('ding_process_detail_mirror')
->whereIn('business_id', ['202610291930000052467', '202610291931000052468'])
->pluck('current_approver_name', 'business_id');
$this->assertNull($rows['202610291930000052467']);
$this->assertSame('王五', $rows['202610291931000052468']);
}
public function test_validation_failure_keeps_the_previous_published_rows(): void
{
$this->query->workflows = [[
@ -1100,6 +1157,9 @@ class FakeDingTalkProcessInstanceClient extends DingTalkProcessInstanceClient
/** @var list<string> */
public array $calls = [];
/** @var list<string> */
public array $failingInstances = [];
public function isConfigured(): bool
{
return $this->configured;
@ -1113,6 +1173,9 @@ class FakeDingTalkProcessInstanceClient extends DingTalkProcessInstanceClient
public function processSnapshot(string $processInstanceId): array
{
$this->calls[] = $processInstanceId;
if (in_array($processInstanceId, $this->failingInstances, true)) {
throw new RuntimeException('审批接口模拟失败');
}
$cashierStatus = $this->cashierStatusByInstance[$processInstanceId] ?? 'not_started';
$tasks = $this->tasksByInstance[$processInstanceId] ?? [];
$operations = [];