From 0dc4a5f55a564da7dbbe82db25afba8f9b792333 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Tue, 3 Nov 2026 19:25:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=B1=E6=95=88=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=E4=B8=8D=E5=86=8D=E9=98=BB=E6=96=AD=E7=A7=8D=E8=8F=9C?= =?UTF-8?q?=E6=8A=A5=E9=94=80=E5=90=8C=E6=AD=A5=20(#843)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 逐项隔离无效调度配置,正常数据集继续同步;记录脱敏警告并保留失败退出码。相关 4 项测试、22 项断言通过。 --- app/Console/Commands/SyncBusinessMirrors.php | 23 +++++++++- .../Console/BusinessMirrorScheduleTest.php | 44 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/SyncBusinessMirrors.php b/app/Console/Commands/SyncBusinessMirrors.php index b80fdb55..59d2ef81 100644 --- a/app/Console/Commands/SyncBusinessMirrors.php +++ b/app/Console/Commands/SyncBusinessMirrors.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; +use InvalidArgumentException; use Modules\EcommerceFinance\Repositories\GenericBusinessMirrorRepository; use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry; use Modules\EcommerceFinance\Services\BusinessMirrorSynchronizer; @@ -22,6 +23,8 @@ class SyncBusinessMirrors extends Command protected $description = '将项目实际使用的上游表按稳定游标同步到共享业务镜像库'; + private bool $hasSchedulingErrors = false; + public function handle( BusinessMirrorDatasetRegistry $datasets, BusinessMirrorSynchronizer $synchronizer, @@ -34,6 +37,7 @@ class SyncBusinessMirrors extends Command return self::SUCCESS; } + $this->hasSchedulingErrors = false; $requested = $this->argument('dataset'); $keys = $requested === null ? $this->scheduledKeys($datasets, $mirror) @@ -41,7 +45,7 @@ class SyncBusinessMirrors extends Command $maxDatasets = max((int) ($this->option('max-datasets') ?? config('upstream_pipeline.sync.max_datasets', 8)), 1); $processed = 0; - $failed = false; + $failed = $this->hasSchedulingErrors; $deadline = microtime(true) + max((int) config('upstream_pipeline.sync.cycle_seconds', 30), 1); foreach ($keys as $task) { @@ -165,7 +169,22 @@ class SyncBusinessMirrors extends Command ), true); foreach ($datasets->all() as $key => $dataset) { - if (isset($dedicated[$key]) || ! $datasets->scheduled($key)) { + if (isset($dedicated[$key])) { + continue; + } + + try { + if (! $datasets->scheduled($key)) { + continue; + } + } catch (InvalidArgumentException $exception) { + // A stale source registration must not stop unrelated mirrors. + $this->hasSchedulingErrors = true; + Log::warning('共享业务镜像调度配置无效,已跳过该数据集。', [ + 'dataset' => $key, + 'exception' => $exception::class, + ]); + $this->warn($key . ':调度配置无效,已跳过,其他数据集继续同步。'); continue; } diff --git a/tests/Feature/Console/BusinessMirrorScheduleTest.php b/tests/Feature/Console/BusinessMirrorScheduleTest.php index a3b785fb..6af692e7 100644 --- a/tests/Feature/Console/BusinessMirrorScheduleTest.php +++ b/tests/Feature/Console/BusinessMirrorScheduleTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Console; +use Illuminate\Support\Facades\Log; +use InvalidArgumentException; use Mockery; use Modules\EcommerceFinance\Contracts\BusinessMirrorDataset; use Modules\EcommerceFinance\Repositories\GenericBusinessMirrorRepository; @@ -12,6 +14,48 @@ use Tests\TestCase; class BusinessMirrorScheduleTest extends TestCase { + public function test_stale_source_does_not_block_healthy_datasets_and_reports_failure(): void + { + config([ + 'upstream_mirrors.refresh_enabled' => true, + 'upstream_pipeline.sync.max_datasets' => 2, + ]); + $registry = Mockery::mock(BusinessMirrorDatasetRegistry::class); + $mirror = Mockery::mock(GenericBusinessMirrorRepository::class); + $sync = Mockery::mock(BusinessMirrorSynchronizer::class); + $objects = []; + foreach (['healthy-a', 'stale-source', 'planting-work-orders'] as $key) { + $objects[$key] = Mockery::mock(BusinessMirrorDataset::class); + if ($key === 'stale-source') { + $registry->shouldReceive('scheduled')->once()->with($key) + ->andThrow(new InvalidArgumentException('Unregistered source: private diagnostic')); + $mirror->shouldNotReceive('state')->with($objects[$key]); + continue; + } + $registry->shouldReceive('scheduled')->once()->with($key)->andReturn(true); + $registry->shouldReceive('definitionValue')->with($key, 'full_reconcile_hours', 0)->andReturn(0); + $mirror->shouldReceive('state')->with($objects[$key])->andReturn([ + 'initial_completed_at' => '2026-10-19 00:00:00', + 'last_attempted_at' => '2026-10-25 00:00:00', + 'status' => 'succeeded', + ]); + $sync->shouldReceive('sync')->once()->with($key, false, null, null) + ->ordered()->andReturn(['status' => 'succeeded', 'batches' => 1, 'rows' => 1]); + } + $registry->shouldReceive('all')->once()->andReturn($objects); + Log::shouldReceive('warning')->once()->with('共享业务镜像调度配置无效,已跳过该数据集。', [ + 'dataset' => 'stale-source', + 'exception' => InvalidArgumentException::class, + ]); + $this->app->instance(BusinessMirrorDatasetRegistry::class, $registry); + $this->app->instance(GenericBusinessMirrorRepository::class, $mirror); + $this->app->instance(BusinessMirrorSynchronizer::class, $sync); + + $this->artisan('business-mirror:sync-tables') + ->expectsOutputToContain('stale-source:调度配置无效,已跳过') + ->assertFailed(); + } + public function test_unapproved_first_sync_is_skipped(): void { config(['upstream_mirrors.refresh_enabled' => true]);