Reuse supervised project sessions between verified replacement steps

This commit is contained in:
阿白叔 committed 2026-10-15 10:37:37 +08:00
1 parent 4194c011e3
commit 2172d8faee
9 files changed
+754 -80

No files matched your search

+166 -16
View File
@@ -32,6 +32,9 @@ Dim QP_BatchSaveCommitted
Dim QP_DiscoveryMode, QP_TargetSchematic, QP_TargetSheet
Dim QP_FailureNumber, QP_FailureSource, QP_FailureDescription
Dim QP_NcSyncSummary
Dim QP_SessionMode, QP_SessionToken, QP_SessionProject, QP_MonitorJobId
Dim QP_OwnedProjectPath, QP_SessionWaiting, QP_SessionExternalChange
Dim QP_CheckpointSchematic, QP_CheckpointSheet
Dim FailureState, CloseError
Set QP_Fso = CreateObject("Scripting.FileSystemObject")
QP_RootTemplate = "%LOCALAPPDATA%\DxProductivity\trial-task-center\quick-run"
@@ -67,6 +70,11 @@ QP_ComponentIndexContext = ""
QP_CurrentJobId = ""
QP_ExitCode = 0
QP_Stage = "session_startup"
QP_SessionMode = False
QP_OwnedProjectPath = ""
QP_SessionWaiting = False
QP_SessionExternalChange = False
QP_MonitorJobId = QP_ExpectedJobId
Set QP_App = Nothing
On Error Resume Next
@@ -86,8 +94,14 @@ Else
If QP_DiscoveryMode Then
QPDiscoverSheets
Else
QPMain
If Err.Number = 0 And QP_BatchMode Then QPFinishBatchItem
Do
QPMain
If Err.Number <> 0 Then Exit Do
If QP_BatchMode Then QPFinishBatchItem
If Err.Number <> 0 Or Not QP_SessionMode Then Exit Do
QPSessionNext
If Err.Number <> 0 Or Not QP_SessionMode Then Exit Do
Loop
End If
If Err.Number <> 0 Then
QP_FailureNumber = Err.Number
@@ -96,7 +110,7 @@ Else
If QP_DiscoveryMode Then QPCleanupDiscoveryFailure
FailureState = "hard_failure"
CloseError = ""
If QP_BatchMode Then
If QP_BatchMode Or QP_SessionMode Then
QPDiscardFailedBatchProject CloseError
If Len(CloseError) = 0 Then
If QP_BatchSaveCommitted Then
@@ -173,6 +187,7 @@ Sub QPBootstrapSession()
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "sheet discovery authorization does not match this job"
QP_DiscoveryMode = True
End If
QPReadSessionRequest
QPProgress "session_startup", 0, 0
TargetPath = QPConfirmedProjectPath()
If QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root, "recovery.request")) Then
@@ -184,6 +199,7 @@ Sub QPBootstrapSession()
Set QP_App = GetObject(, "Viewdraw.Application")
Else
Set QP_App = QPAttachOrStart(QPDesignerProcessCount())
If QP_SessionMode Then WScript.ConnectObject QP_App, "QPEvent_"
QPEnsureProject QP_App, TargetPath
End If
QP_Stage = "session_ready"
@@ -193,7 +209,9 @@ End Sub
Sub QPCleanupDiscoveryFailure()
On Error Resume Next
If QP_OwnedProject And Not QP_App Is Nothing Then QP_App.CloseProject
If QP_OwnedProject And Not QP_App Is Nothing Then
If QPCanonicalPath(QPProjectPath()) = QPCanonicalPath(QP_OwnedProjectPath) Then QP_App.CloseProject
End If
QP_OwnedProject = False
Err.Clear
On Error GoTo 0
@@ -220,15 +238,21 @@ Sub QPWaitForMonitor()
End Sub
Sub QPProgress(ByVal Stage, ByVal Done, ByVal Total)
Dim Safe, StopPath
Dim Safe, StopPath, Job, Suffix
If Len(CStr(QP_AttemptToken)) = 0 Then Exit Sub
Safe = 0
If Not CBool(QP_NativeMutationPossible) And Not CBool(QP_RestoreTransactionOpen) And _
Not CBool(QP_NativeCallActive) Then Safe = 1
QP_ProgressSequence = CLng(QP_ProgressSequence) + 1
Job = QP_ExpectedJobId
Suffix = ""
If QP_SessionMode Then
Job = QP_MonitorJobId
Suffix = vbTab & QP_ExpectedJobId
End If
WScript.StdOut.WriteLine "DX_PROGRESS" & vbTab & QP_AttemptToken & vbTab & _
QP_ExpectedJobId & vbTab & CStr(QP_ProgressSequence) & vbTab & Stage & _
vbTab & CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(Safe)
Job & vbTab & CStr(QP_ProgressSequence) & vbTab & Stage & _
vbTab & CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(Safe) & Suffix
If Safe = 1 And Stage <> "finished" Then
StopPath = QP_Fso.BuildPath(QP_Root, "monitor-" & QP_AttemptToken & ".stop")
If QP_Fso.FileExists(StopPath) Then
@@ -239,6 +263,111 @@ Sub QPProgress(ByVal Stage, ByVal Done, ByVal Total)
End If
End Sub
Sub QPReadSessionRequest()
Dim Path, Fields, Index, Character
Path = QP_Fso.BuildPath(QP_Root, "project-session.request")
If Not QP_Fso.FileExists(Path) Then Exit Sub
If Not QP_BatchMode Or QP_DiscoveryMode Or _
QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root, "recovery.request")) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "invalid project session mode"
Fields = Split(Trim(QPReadUtf8(Path)), vbTab)
If UBound(Fields) <> 2 Then Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "invalid session request"
QP_SessionToken = CStr(Fields(0))
If Len(QP_SessionToken) <> 32 Or Fields(2) <> QP_ExpectedJobId Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "session authorization mismatch"
For Index = 1 To 32
Character = Mid(QP_SessionToken, Index, 1)
If InStr(1, "0123456789abcdef", Character, vbBinaryCompare) = 0 Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "invalid session token"
Next
QP_SessionProject = CStr(Fields(1))
If QPCanonicalPath(QPConfirmedProjectPath()) <> QPCanonicalPath(QP_SessionProject) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "session project mismatch"
QP_SessionMode = True
End Sub
Sub QPSessionNext()
Dim Path, Fields, Attempt, TargetPath, Documents, Document, View
Path = QP_Fso.BuildPath(QP_Root, "project-session-next.tsv")
QPProgress "session_wait", 1, 1
For Attempt = 1 To 1500
If QP_Fso.FileExists(Path) Then
Fields = Split(Trim(QPReadUtf8(Path)), vbTab)
If UBound(Fields) <> 1 Then Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "invalid continuation"
If Fields(0) <> QP_SessionToken Then Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "continuation token mismatch"
QP_Fso.DeleteFile Path, True
QPRequireSessionCheckpoint
QP_SessionWaiting = False
If Fields(1) = "close" Then
QPCloseBatchProject QP_SessionProject, False
QP_SessionMode = False
Exit Sub
End If
QP_ExpectedJobId = CStr(Fields(1))
QP_CurrentJobId = QP_ExpectedJobId
QP_BatchMode = True
QP_BatchState = ""
QP_BatchSaveCommitted = False
QP_NativeMutationPossible = False
If Trim(QPReadUtf8(QP_Fso.BuildPath(QP_Root, "batch.request"))) <> QP_ExpectedJobId Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "next batch authorization mismatch"
TargetPath = QPConfirmedProjectPath()
If QPCanonicalPath(TargetPath) <> QPCanonicalPath(QP_SessionProject) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "cannot switch project inside a session"
Set View = QP_App.ActiveView
If Not View Is Nothing Then
If CStr(View.GetName(0)) <> QP_TargetSchematic Or CStr(View.Block.SheetNum) <> QP_TargetSheet Then
Set Documents = QP_App.SchematicSheetDocuments()
If Documents.Count <> 1 Then Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "owned sheet set changed"
Set Document = Documents.Item(1)
Document.Close False, ""
End If
End If
QPEnsureProject QP_App, TargetPath
Exit Sub
End If
WScript.Sleep 100
Next
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "supervisor did not release project checkpoint"
End Sub
Sub QPEvent_DocumentModified()
If QP_SessionWaiting Then QP_SessionExternalChange = True
End Sub
Sub QPEvent_BlockModified(ByVal Block)
If QP_SessionWaiting Then QP_SessionExternalChange = True
End Sub
Sub QPEvent_ProjectModified(ByVal NewProject)
If QP_SessionWaiting Then QP_SessionExternalChange = True
End Sub
Sub QPEvent_BeforeProjectModified()
If QP_SessionWaiting Then QP_SessionExternalChange = True
End Sub
Sub QPEvent_SourceFileModified(ByVal FileName)
If QP_SessionWaiting Then QP_SessionExternalChange = True
End Sub
Sub QPRequireSessionCheckpoint()
Dim Documents, View
If Not QP_OwnedProject Or Len(QP_OwnedProjectPath) = 0 Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "session project ownership was lost"
If QPCanonicalPath(QPProjectPath()) <> QPCanonicalPath(QP_OwnedProjectPath) Or _
QPCanonicalPath(QP_OwnedProjectPath) <> QPCanonicalPath(QP_SessionProject) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "active project changed; no automatic save or close"
If QP_SessionExternalChange Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "project modified outside worker at checkpoint; no automatic save or close"
Set Documents = QP_App.SchematicSheetDocuments()
Set View = QP_App.ActiveView
If Documents.Count <> 1 Or View Is Nothing Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "owned sheet set changed at checkpoint"
If CStr(View.GetName(0)) <> QP_CheckpointSchematic Or CStr(View.Block.SheetNum) <> QP_CheckpointSheet Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "active sheet changed at checkpoint"
End Sub
Sub QPInvalidatePinIndex()
Set QP_PinIndex = Nothing
QP_PinContext = ""
@@ -443,7 +572,7 @@ Sub QPEnsureProject(ByRef App, ByVal TargetPath)
CurrentPath = ""
If Not ProjectData Is Nothing Then CurrentPath = CStr(ProjectData.GetProjectFilePath())
If Len(Trim(CurrentPath)) > 0 Then
If QP_BatchMode Then Err.Raise vbObjectError + 870, "QuickPartReplace.Batch", _
If QP_BatchMode And Not (QP_SessionMode And QP_OwnedProject) Then Err.Raise vbObjectError + 870, "QuickPartReplace.Batch", _
"batch cannot take over an already open project; save your wanted edits and close the project before starting the queue"
If QPCanonicalPath(CurrentPath) <> QPCanonicalPath(TargetPath) Then _
Err.Raise vbObjectError + 853, "QuickPartReplace.Session", _
@@ -455,6 +584,7 @@ Sub QPEnsureProject(ByRef App, ByVal TargetPath)
Opened = App.OpenProject(CStr(TargetPath))
If Not Opened Then Err.Raise vbObjectError + 854, "QuickPartReplace.Session", "DxDesigner refused to open the target project"
QP_OwnedProject = True
QP_OwnedProjectPath = TargetPath
End If
Set ProjectData = App.GetProjectData()
If ProjectData Is Nothing Then _
@@ -678,7 +808,9 @@ End Sub
Sub QPCloseBatchProject(ByVal ExpectedPath, ByVal SaveChanges)
Dim Documents, Document
If Not QP_OwnedProject Or QP_RestoreTransactionOpen Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "project ownership or restoration transaction is not safe to close"
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "project ownership or restoration transaction is not safe to close"
If QPCanonicalPath(ExpectedPath) <> QPCanonicalPath(QP_OwnedProjectPath) Or QP_SessionExternalChange Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "owned project changed; no automatic close"
If QPCanonicalPath(QPProjectPath()) <> QPCanonicalPath(ExpectedPath) Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "active project changed before save and close"
Set Documents = QP_App.SchematicSheetDocuments()
@@ -692,6 +824,7 @@ Sub QPCloseBatchProject(ByVal ExpectedPath, ByVal SaveChanges)
If Len(CStr(QP_App.GetProjectData().GetProjectFilePath())) > 0 Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "project did not close; queue stopped"
QP_OwnedProject = False
QP_OwnedProjectPath = ""
QPInvalidatePinIndex
QPInvalidateComponentIndex
End Sub
@@ -699,9 +832,9 @@ End Sub
Sub QPDiscardFailedBatchProject(ByRef ErrorDescription)
Dim ErrorNumber, ErrorSource, Description
ErrorDescription = ""
If Not QP_BatchMode Or Not QP_OwnedProject Or QP_App Is Nothing Then Exit Sub
If (Not QP_BatchMode And Not QP_SessionMode) Or Not QP_OwnedProject Or QP_App Is Nothing Then Exit Sub
On Error Resume Next
QPCloseBatchProject QPProjectPath(), False
QPCloseBatchProject QP_OwnedProjectPath, False
ErrorNumber = Err.Number
ErrorSource = Err.Source
Description = Err.Description
@@ -741,8 +874,10 @@ Sub QPFinishBatchItem()
ValueStates.Item(CStr(Index)).Add "custom_value", QPCustomValue(Command)
Next
End If
QPCloseBatchProject TargetPath, True
QP_BatchSaveCommitted = True
If FinalState = "restored_ok" Or Not QP_SessionMode Then
QPCloseBatchProject TargetPath, True
QP_BatchSaveCommitted = True
End If
If FinalState = "restored_ok" Then
QPProgress "verify_saved_project", 0, 1
QPEnsureProject QP_App, TargetPath
@@ -757,12 +892,27 @@ Sub QPFinishBatchItem()
Plans.Add CStr(Index), Candidate
Next
QPVerifyRestoredPlans Command, Records, Plans, ValueStates
QPCloseBatchProject TargetPath, True
If Not QP_SessionMode Then QPCloseBatchProject TargetPath, True
FinalState = "saved_ok"
QP_BatchMessage = QP_BatchMessage & " Saved, reopened for readback, and closed."
If QP_SessionMode Then
QP_BatchMessage = QP_BatchMessage & " Saved and reopened for readback; owned project checkpoint verified."
Else
QP_BatchMessage = QP_BatchMessage & " Saved, reopened for readback, and closed."
End If
End If
QP_BatchMode = False
QPWriteStatus QP_Root, QP_CurrentJobId, FinalState, "batch_closed", QP_BatchMessage
If QP_SessionMode Then
QP_NativeMutationPossible = False
QP_SessionExternalChange = False
QP_CheckpointSchematic = QP_TargetSchematic
QP_CheckpointSheet = QP_TargetSheet
QP_SessionWaiting = True
QPWriteStatus QP_Root, QP_CurrentJobId, FinalState, "session_verified", QP_BatchMessage
QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "project-session-result.tsv"), _
QP_SessionToken & vbTab & QP_CurrentJobId & vbTab & FinalState & vbTab & "session_verified"
Else
QPWriteStatus QP_Root, QP_CurrentJobId, FinalState, "batch_closed", QP_BatchMessage
End If
QPProgress "finished", 1, 1
End Sub