1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| function Sync-EdgeData { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$SourceNodeID, [Parameter(Mandatory = $true)] [string]$TargetNodeID, [Parameter()] [string[]]$DataTypes, [Parameter()] [ValidateSet("RealTime", "Scheduled", "OnDemand")] [string]$SyncMode = "Scheduled", [Parameter()] [int]$Interval = 300, [Parameter()] [hashtable]$Filters ) try { $sync = [PSCustomObject]@{ SourceNodeID = $SourceNodeID TargetNodeID = $TargetNodeID StartTime = Get-Date Mode = $SyncMode Status = "Initializing" DataTypes = $DataTypes Statistics = @{} Errors = @() } $sourceNode = Get-EdgeNode -NodeID $SourceNodeID $targetNode = Get-EdgeNode -NodeID $TargetNodeID if (-not $sourceNode -or -not $targetNode) { throw "源节点或目标节点不存在" } $syncConfig = [PSCustomObject]@{ Mode = $SyncMode Interval = $Interval Filters = $Filters Compression = $true Encryption = $true } $initResult = Initialize-DataSync ` -SourceNode $sourceNode ` -TargetNode $targetNode ` -Config $syncConfig if (-not $initResult.Success) { throw "同步初始化失败:$($initResult.Message)" } switch ($SyncMode) { "RealTime" { $syncJob = Start-Job -ScriptBlock { param($sourceID, $targetID, $config) Sync-RealTimeData -SourceID $sourceID -TargetID $targetID -Config $config } -ArgumentList $SourceNodeID, $TargetNodeID, $syncConfig } "Scheduled" { $syncJob = Start-Job -ScriptBlock { param($sourceID, $targetID, $config) Sync-ScheduledData -SourceID $sourceID -TargetID $targetID -Config $config } -ArgumentList $SourceNodeID, $TargetNodeID, $syncConfig } "OnDemand" { $syncJob = Start-Job -ScriptBlock { param($sourceID, $targetID, $config) Sync-OnDemandData -SourceID $sourceID -TargetID $targetID -Config $config } -ArgumentList $SourceNodeID, $TargetNodeID, $syncConfig } } while ($syncJob.State -eq "Running") { $status = Get-SyncStatus -JobID $syncJob.Id $sync.Status = $status.State $sync.Statistics = $status.Statistics if ($status.Errors.Count -gt 0) { $sync.Errors += $status.Errors } Start-Sleep -Seconds 5 } $sync.EndTime = Get-Date return $sync } catch { Write-Error "数据同步失败:$_" return $null } }
|