连续同步失败后触发重试时间点
This commit is contained in:
@@ -23,12 +23,15 @@ class SplitTicketSyncService
|
||||
|
||||
private SplitTicketSyncDispatchService $dispatchService;
|
||||
|
||||
private SplitTicketSyncFailRetryService $failRetryService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->numberSync = new SplitTicketNumberSyncService();
|
||||
$this->ruleService = new SplitTicketRuleService();
|
||||
$this->lockService = new SplitTicketSyncLockService();
|
||||
$this->dispatchService = new SplitTicketSyncDispatchService();
|
||||
$this->failRetryService = new SplitTicketSyncFailRetryService();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -400,7 +403,13 @@ class SplitTicketSyncService
|
||||
->where('manual_manage', 0)
|
||||
->whereIn('ticket_type', $types);
|
||||
if ($failThreshold > 0) {
|
||||
$query->where('sync_fail_count', '<', $failThreshold);
|
||||
$query->where(function ($query) use ($failThreshold): void {
|
||||
$query->where('sync_fail_count', '<', $failThreshold)
|
||||
->whereOr(function ($query): void {
|
||||
$query->where('sync_fail_pause_at', '>', 0)
|
||||
->where('sync_auto_sync_stopped', 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
$query->order('sync_time', 'asc')->order('id', 'asc');
|
||||
if ($limit > 0) {
|
||||
@@ -613,6 +622,27 @@ class SplitTicketSyncService
|
||||
if ((string) $ticket['status'] === 'hidden') {
|
||||
return '工单已关闭';
|
||||
}
|
||||
|
||||
if ($this->failRetryService->isPermanentlyStopped($ticket)) {
|
||||
return '自动同步已终止(重试已用尽)';
|
||||
}
|
||||
|
||||
if ($this->failRetryService->isInRetryMode($ticket)) {
|
||||
$retrySkip = $this->failRetryService->shouldAllowAutoRetry($ticket);
|
||||
if ($retrySkip !== null) {
|
||||
return $retrySkip;
|
||||
}
|
||||
if (!SplitScrmSpiderFactory::isSupported((string) $ticket['ticket_type'])) {
|
||||
return '工单类型尚未实现';
|
||||
}
|
||||
$interval = SplitSyncConfigService::getIntervalMinutes((string) $ticket['ticket_type']);
|
||||
if ($interval <= 0) {
|
||||
return '该类型未配置自动同步周期';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$failThreshold = SplitSyncConfigService::getFailPauseThreshold();
|
||||
if ($failThreshold > 0 && (int) ($ticket['sync_fail_count'] ?? 0) >= $failThreshold) {
|
||||
return sprintf('连续同步失败超过%d次已暂停', $failThreshold);
|
||||
@@ -661,6 +691,7 @@ class SplitTicketSyncService
|
||||
if ($success) {
|
||||
$data['sync_success_time'] = $now;
|
||||
$data['sync_success_mode'] = in_array($syncMode, ['auto', 'manual'], true) ? $syncMode : 'manual';
|
||||
$data = array_merge($data, $this->failRetryService->clearOnSuccessFields());
|
||||
}
|
||||
if (!$ticket->allowField(array_keys($data))->save($data)) {
|
||||
throw new Exception('工单同步结果保存失败');
|
||||
@@ -679,6 +710,21 @@ class SplitTicketSyncService
|
||||
'sync_message' => $message,
|
||||
'sync_fail_count' => $failCount,
|
||||
];
|
||||
|
||||
if ($this->failRetryService->isInRetryMode($ticket)) {
|
||||
$update = array_merge($update, $this->failRetryService->onRetryFailure($ticket));
|
||||
$ticket->save($update);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($failThreshold > 0 && $failCount >= $failThreshold && SplitSyncConfigService::hasFailRetrySchedule()) {
|
||||
$update = array_merge($update, $this->failRetryService->enterRetryIfNeeded($ticket, $failCount, $failThreshold));
|
||||
$ticket->save($update);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($neverSyncedSuccessfully || ($failThreshold > 0 && $failCount >= $failThreshold)) {
|
||||
$update['status'] = 'hidden';
|
||||
}
|
||||
@@ -787,7 +833,7 @@ class SplitTicketSyncService
|
||||
|
||||
$openList = Ticket::where('status', 'normal')
|
||||
->whereIn('ticket_type', $supportedTypes)
|
||||
->field('id,ticket_type,ticket_url,sync_fail_count,sync_time,sync_status')
|
||||
->field('id,ticket_type,ticket_url,sync_fail_count,sync_time,sync_status,sync_fail_pause_at,sync_fail_retry_index,sync_auto_sync_stopped,status,manual_manage')
|
||||
->select();
|
||||
|
||||
$result = [];
|
||||
@@ -838,7 +884,16 @@ class SplitTicketSyncService
|
||||
$ticketType = (string) ($ticket['ticket_type'] ?? '');
|
||||
$failCount = (int) ($ticket['sync_fail_count'] ?? 0);
|
||||
|
||||
if ($failThreshold > 0 && $failCount >= $failThreshold) {
|
||||
if ($this->failRetryService->isPermanentlyStopped($ticket)) {
|
||||
return '自动同步已终止(重试已用尽)';
|
||||
}
|
||||
if ($this->failRetryService->isInRetryMode($ticket)) {
|
||||
$retrySkip = $this->failRetryService->shouldAllowAutoRetry($ticket);
|
||||
if ($retrySkip !== null) {
|
||||
return $retrySkip;
|
||||
}
|
||||
}
|
||||
if ($failThreshold > 0 && $failCount >= $failThreshold && !SplitSyncConfigService::hasFailRetrySchedule()) {
|
||||
return sprintf('连续同步失败超过%d次已暂停自动同步', $failThreshold);
|
||||
}
|
||||
if (!in_array($ticketType, $autoSyncTypes, true)) {
|
||||
|
||||
Reference in New Issue
Block a user