fix: 失效数据源不再阻断种菜报销同步 (#843)
逐项隔离无效调度配置,正常数据集继续同步;记录脱敏警告并保留失败退出码。相关 4 项测试、22 项断言通过。
This commit is contained in:
parent
de18f3bc8a
commit
0dc4a5f55a
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user