From 9255eb3132ce5a0efdd2b22d6c3bcb95e96200f5 Mon Sep 17 00:00:00 2001 From: Christ Date: Fri, 14 Aug 2026 22:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E5=85=AD=EF=BC=9A=E6=94=B6?= =?UTF-8?q?=E5=8F=A3Automation=E8=BF=90=E8=A1=8C=E5=8D=8F=E8=B0=83?= =?UTF-8?q?=E4=B8=8E=E5=8A=A8=E4=BD=9C=E6=89=A7=E8=A1=8C=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .commentlint/go-baseline.json | 1530 ++++++++---------- docs/architecture/refactoring-master-plan.md | 4 +- internal/automation/center.go | 214 +-- internal/automation/run_coordinator.go | 277 ++++ 4 files changed, 940 insertions(+), 1085 deletions(-) create mode 100644 internal/automation/run_coordinator.go diff --git a/.commentlint/go-baseline.json b/.commentlint/go-baseline.json index c798266..71ddb36 100644 --- a/.commentlint/go-baseline.json +++ b/.commentlint/go-baseline.json @@ -9241,76 +9241,46 @@ }, { "file": "internal/automation/center.go", - "line": 115, + "line": 117, "kind": "local-variable", "name": "client" }, { "file": "internal/automation/center.go", - "line": 132, + "line": 118, + "kind": "local-variable", + "name": "center" + }, + { + "file": "internal/automation/center.go", + "line": 147, "kind": "function", "name": "SetAccountTaskClient" }, { "file": "internal/automation/center.go", - "line": 137, + "line": 152, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 137, + "line": 152, "kind": "local-variable", "name": "recoverer" }, { "file": "internal/automation/center.go", - "line": 154, + "line": 169, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 158, + "line": 173, "kind": "function", "name": "handleTask" }, - { - "file": "internal/automation/center.go", - "line": 166, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 170, - "kind": "local-variable", - "name": "order" - }, - { - "file": "internal/automation/center.go", - "line": 170, - "kind": "local-variable", - "name": "orderErr" - }, - { - "file": "internal/automation/center.go", - "line": 176, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 176, - "kind": "local-variable", - "name": "paused" - }, - { - "file": "internal/automation/center.go", - "line": 176, - "kind": "local-variable", - "name": "until" - }, { "file": "internal/automation/center.go", "line": 181, @@ -9319,573 +9289,459 @@ }, { "file": "internal/automation/center.go", - "line": 187, + "line": 185, "kind": "local-variable", - "name": "enabled" + "name": "order" }, { "file": "internal/automation/center.go", - "line": 187, + "line": 185, + "kind": "local-variable", + "name": "orderErr" + }, + { + "file": "internal/automation/center.go", + "line": 191, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 195, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 195, - "kind": "local-variable", - "name": "rules" - }, - { - "file": "internal/automation/center.go", - "line": 203, - "kind": "var", - "name": "firstErr" - }, - { - "file": "internal/automation/center.go", - "line": 204, - "kind": "range-variable", - "name": "rule" - }, - { - "file": "internal/automation/center.go", - "line": 205, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 206, - "kind": "var", - "name": "prepErr" - }, - { - "file": "internal/automation/center.go", - "line": 208, - "kind": "local-variable", - "name": "dueAt" - }, - { - "file": "internal/automation/center.go", - "line": 209, - "kind": "local-variable", - "name": "deferErr" - }, - { - "file": "internal/automation/center.go", - "line": 228, - "kind": "function", - "name": "taskAutomationRunID" - }, - { - "file": "internal/automation/center.go", - "line": 232, - "kind": "local-variable", - "name": "value" - }, - { - "file": "internal/automation/center.go", - "line": 233, - "kind": "local-variable", - "name": "id" - }, - { - "file": "internal/automation/center.go", - "line": 237, - "kind": "function", - "name": "taskDelayCursor" - }, - { - "file": "internal/automation/center.go", - "line": 241, - "kind": "local-variable", - "name": "value" - }, - { - "file": "internal/automation/center.go", - "line": 242, - "kind": "local-variable", - "name": "cursor" - }, - { - "file": "internal/automation/center.go", - "line": 242, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 249, - "kind": "function", - "name": "isDeferredReplay" - }, - { - "file": "internal/automation/center.go", - "line": 253, - "kind": "function", - "name": "deferTask" - }, - { - "file": "internal/automation/center.go", - "line": 257, - "kind": "function", - "name": "deferTaskWithError" - }, - { - "file": "internal/automation/center.go", - "line": 258, - "kind": "local-variable", - "name": "key" - }, - { - "file": "internal/automation/center.go", - "line": 263, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 263, - "kind": "local-variable", - "name": "raw" - }, - { - "file": "internal/automation/center.go", - "line": 275, - "kind": "function", - "name": "ManualFullDelivery" - }, - { - "file": "internal/automation/center.go", - "line": 282, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 282, + "line": 191, "kind": "local-variable", "name": "paused" }, { "file": "internal/automation/center.go", - "line": 282, + "line": 191, "kind": "local-variable", "name": "until" }, { "file": "internal/automation/center.go", - "line": 289, + "line": 196, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 202, "kind": "local-variable", "name": "enabled" }, { "file": "internal/automation/center.go", - "line": 289, + "line": 202, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 305, - "kind": "local-variable", - "name": "task" - }, - { - "file": "internal/automation/center.go", - "line": 325, + "line": 210, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 325, + "line": 210, "kind": "local-variable", "name": "rules" }, { "file": "internal/automation/center.go", - "line": 332, - "kind": "local-variable", - "name": "sent" + "line": 218, + "kind": "var", + "name": "firstErr" }, { "file": "internal/automation/center.go", - "line": 333, + "line": 219, "kind": "range-variable", "name": "rule" }, { "file": "internal/automation/center.go", - "line": 338, - "kind": "local-variable", - "name": "rawTask" - }, - { - "file": "internal/automation/center.go", - "line": 340, - "kind": "local-variable", - "name": "rawJSON" - }, - { - "file": "internal/automation/center.go", - "line": 341, - "kind": "local-variable", - "name": "runID" - }, - { - "file": "internal/automation/center.go", - "line": 341, - "kind": "local-variable", - "name": "startErr" - }, - { - "file": "internal/automation/center.go", - "line": 341, - "kind": "local-variable", - "name": "started" - }, - { - "file": "internal/automation/center.go", - "line": 359, - "kind": "local-variable", - "name": "getErr" - }, - { - "file": "internal/automation/center.go", - "line": 359, - "kind": "local-variable", - "name": "run" - }, - { - "file": "internal/automation/center.go", - "line": 363, - "kind": "local-variable", - "name": "deferred" - }, - { - "file": "internal/automation/center.go", - "line": 363, + "line": 220, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 363, - "kind": "local-variable", - "name": "n" - }, - { - "file": "internal/automation/center.go", - "line": 371, - "kind": "local-variable", - "name": "errMsg" - }, - { - "file": "internal/automation/center.go", - "line": 371, - "kind": "local-variable", - "name": "status" - }, - { - "file": "internal/automation/center.go", - "line": 375, - "kind": "local-variable", - "name": "cancel" - }, - { - "file": "internal/automation/center.go", - "line": 375, - "kind": "local-variable", - "name": "finishCtx" - }, - { - "file": "internal/automation/center.go", - "line": 376, - "kind": "local-variable", - "name": "finishErr" - }, - { - "file": "internal/automation/center.go", - "line": 394, - "kind": "function", - "name": "executeRule" - }, - { - "file": "internal/automation/center.go", - "line": 395, + "line": 221, "kind": "var", - "name": "err" + "name": "prepErr" }, { "file": "internal/automation/center.go", - "line": 403, - "kind": "local-variable", - "name": "triggerKey" - }, - { - "file": "internal/automation/center.go", - "line": 410, - "kind": "local-variable", - "name": "retryTask" - }, - { - "file": "internal/automation/center.go", - "line": 412, - "kind": "local-variable", - "name": "rawJSON" - }, - { - "file": "internal/automation/center.go", - "line": 413, - "kind": "var", - "name": "run" - }, - { - "file": "internal/automation/center.go", - "line": 414, - "kind": "local-variable", - "name": "resumeID" - }, - { - "file": "internal/automation/center.go", - "line": 423, - "kind": "var", - "name": "runID" - }, - { - "file": "internal/automation/center.go", - "line": 424, - "kind": "var", - "name": "started" - }, - { - "file": "internal/automation/center.go", - "line": 439, - "kind": "local-variable", - "name": "status" - }, - { - "file": "internal/automation/center.go", - "line": 440, - "kind": "local-variable", - "name": "errMsg" - }, - { - "file": "internal/automation/center.go", - "line": 441, - "kind": "local-variable", - "name": "sent" - }, - { - "file": "internal/automation/center.go", - "line": 442, - "kind": "local-variable", - "name": "finish" - }, - { - "file": "internal/automation/center.go", - "line": 447, - "kind": "local-variable", - "name": "cancel" - }, - { - "file": "internal/automation/center.go", - "line": 447, - "kind": "local-variable", - "name": "finishCtx" - }, - { - "file": "internal/automation/center.go", - "line": 448, - "kind": "local-variable", - "name": "finishErr" - }, - { - "file": "internal/automation/center.go", - "line": 456, - "kind": "local-variable", - "name": "actions" - }, - { - "file": "internal/automation/center.go", - "line": 461, - "kind": "var", - "name": "deferred" - }, - { - "file": "internal/automation/center.go", - "line": 476, - "kind": "local-variable", - "name": "reason" - }, - { - "file": "internal/automation/center.go", - "line": 477, - "kind": "local-variable", - "name": "quarantineErr" - }, - { - "file": "internal/automation/center.go", - "line": 495, - "kind": "local-variable", - "name": "incrementErr" - }, - { - "file": "internal/automation/center.go", - "line": 496, - "kind": "local-variable", - "name": "reason" - }, - { - "file": "internal/automation/center.go", - "line": 497, - "kind": "local-variable", - "name": "quarantineErr" - }, - { - "file": "internal/automation/center.go", - "line": 512, - "kind": "function", - "name": "executeRunActions" - }, - { - "file": "internal/automation/center.go", - "line": 513, - "kind": "local-variable", - "name": "sent" - }, - { - "file": "internal/automation/center.go", - "line": 514, - "kind": "local-variable", - "name": "cursor" - }, - { - "file": "internal/automation/center.go", - "line": 515, - "kind": "local-variable", - "name": "action" - }, - { - "file": "internal/automation/center.go", - "line": 517, - "kind": "local-variable", - "name": "delaySeconds" - }, - { - "file": "internal/automation/center.go", - "line": 517, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 528, + "line": 223, "kind": "local-variable", "name": "dueAt" }, { "file": "internal/automation/center.go", - "line": 529, + "line": 224, + "kind": "local-variable", + "name": "deferErr" + }, + { + "file": "internal/automation/center.go", + "line": 243, + "kind": "function", + "name": "taskAutomationRunID" + }, + { + "file": "internal/automation/center.go", + "line": 247, + "kind": "local-variable", + "name": "value" + }, + { + "file": "internal/automation/center.go", + "line": 248, + "kind": "local-variable", + "name": "id" + }, + { + "file": "internal/automation/center.go", + "line": 252, + "kind": "function", + "name": "taskDelayCursor" + }, + { + "file": "internal/automation/center.go", + "line": 256, + "kind": "local-variable", + "name": "value" + }, + { + "file": "internal/automation/center.go", + "line": 257, + "kind": "local-variable", + "name": "cursor" + }, + { + "file": "internal/automation/center.go", + "line": 257, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 532, + "line": 264, + "kind": "function", + "name": "isDeferredReplay" + }, + { + "file": "internal/automation/center.go", + "line": 268, + "kind": "function", + "name": "deferTask" + }, + { + "file": "internal/automation/center.go", + "line": 272, + "kind": "function", + "name": "deferTaskWithError" + }, + { + "file": "internal/automation/center.go", + "line": 273, + "kind": "local-variable", + "name": "key" + }, + { + "file": "internal/automation/center.go", + "line": 278, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 538, + "line": 278, + "kind": "local-variable", + "name": "raw" + }, + { + "file": "internal/automation/center.go", + "line": 290, + "kind": "function", + "name": "ManualFullDelivery" + }, + { + "file": "internal/automation/center.go", + "line": 297, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 538, + "line": 297, + "kind": "local-variable", + "name": "paused" + }, + { + "file": "internal/automation/center.go", + "line": 297, + "kind": "local-variable", + "name": "until" + }, + { + "file": "internal/automation/center.go", + "line": 304, + "kind": "local-variable", + "name": "enabled" + }, + { + "file": "internal/automation/center.go", + "line": 304, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 320, + "kind": "local-variable", + "name": "task" + }, + { + "file": "internal/automation/center.go", + "line": 340, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 340, + "kind": "local-variable", + "name": "rules" + }, + { + "file": "internal/automation/center.go", + "line": 347, + "kind": "local-variable", + "name": "sent" + }, + { + "file": "internal/automation/center.go", + "line": 348, + "kind": "range-variable", + "name": "rule" + }, + { + "file": "internal/automation/center.go", + "line": 353, + "kind": "local-variable", + "name": "rawTask" + }, + { + "file": "internal/automation/center.go", + "line": 355, + "kind": "local-variable", + "name": "rawJSON" + }, + { + "file": "internal/automation/center.go", + "line": 356, + "kind": "local-variable", + "name": "runID" + }, + { + "file": "internal/automation/center.go", + "line": 356, + "kind": "local-variable", + "name": "startErr" + }, + { + "file": "internal/automation/center.go", + "line": 356, "kind": "local-variable", "name": "started" }, { "file": "internal/automation/center.go", - "line": 545, + "line": 374, "kind": "local-variable", - "name": "actionErr" + "name": "getErr" }, { "file": "internal/automation/center.go", - "line": 545, + "line": 374, "kind": "local-variable", - "name": "n" + "name": "run" }, { "file": "internal/automation/center.go", - "line": 547, - "kind": "var", - "name": "uncertain" - }, - { - "file": "internal/automation/center.go", - "line": 549, + "line": 378, "kind": "local-variable", - "name": "reason" + "name": "deferred" }, { "file": "internal/automation/center.go", - "line": 550, - "kind": "local-variable", - "name": "quarantineErr" - }, - { - "file": "internal/automation/center.go", - "line": 556, - "kind": "local-variable", - "name": "abortErr" - }, - { - "file": "internal/automation/center.go", - "line": 557, - "kind": "local-variable", - "name": "reason" - }, - { - "file": "internal/automation/center.go", - "line": 558, - "kind": "local-variable", - "name": "quarantineErr" - }, - { - "file": "internal/automation/center.go", - "line": 565, + "line": 378, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 566, + "line": 378, "kind": "local-variable", - "name": "quarantineErr" + "name": "n" }, { "file": "internal/automation/center.go", - "line": 580, + "line": 386, + "kind": "local-variable", + "name": "errMsg" + }, + { + "file": "internal/automation/center.go", + "line": 386, + "kind": "local-variable", + "name": "status" + }, + { + "file": "internal/automation/center.go", + "line": 390, + "kind": "local-variable", + "name": "cancel" + }, + { + "file": "internal/automation/center.go", + "line": 390, + "kind": "local-variable", + "name": "finishCtx" + }, + { + "file": "internal/automation/center.go", + "line": 391, + "kind": "local-variable", + "name": "finishErr" + }, + { + "file": "internal/automation/center.go", + "line": 421, "kind": "function", - "name": "executeActionNow" + "name": "notifyResult" }, { "file": "internal/automation/center.go", - "line": 581, + "line": 422, "kind": "local-variable", - "name": "allowed" + "name": "triggerName" + }, + { + "file": "internal/automation/center.go", + "line": 434, + "kind": "local-variable", + "name": "msg" + }, + { + "file": "internal/automation/center.go", + "line": 438, + "kind": "local-variable", + "name": "msg" + }, + { + "file": "internal/automation/center.go", + "line": 442, + "kind": "function", + "name": "notifyRunNeedsReview" + }, + { + "file": "internal/automation/center.go", + "line": 446, + "kind": "local-variable", + "name": "task" + }, + { + "file": "internal/automation/center.go", + "line": 452, + "kind": "function", + "name": "actionDelaySeconds" + }, + { + "file": "internal/automation/center.go", + "line": 456, + "kind": "var", + "name": "cfg" + }, + { + "file": "internal/automation/center.go", + "line": 460, + "kind": "local-variable", + "name": "card" + }, + { + "file": "internal/automation/center.go", + "line": 460, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 473, + "kind": "function", + "name": "prepareTask" + }, + { + "file": "internal/automation/center.go", + "line": 483, + "kind": "local-variable", + "name": "needsDetail" + }, + { + "file": "internal/automation/center.go", + "line": 484, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 484, + "kind": "local-variable", + "name": "existing" + }, + { + "file": "internal/automation/center.go", + "line": 498, + "kind": "local-variable", + "name": "cookieStr" + }, + { + "file": "internal/automation/center.go", + "line": 500, + "kind": "var", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 506, + "kind": "local-variable", + "name": "detail" + }, + { + "file": "internal/automation/center.go", + "line": 506, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 542, + "kind": "function", + "name": "mergeOrderIntoTask" + }, + { + "file": "internal/automation/center.go", + "line": 570, + "kind": "function", + "name": "executeAction" + }, + { + "file": "internal/automation/center.go", + "line": 577, + "kind": "local-variable", + "name": "text" }, { "file": "internal/automation/center.go", @@ -9897,689 +9753,557 @@ "file": "internal/automation/center.go", "line": 593, "kind": "function", - "name": "notifyResult" + "name": "confirmShipment" }, { "file": "internal/automation/center.go", - "line": 594, + "line": 597, "kind": "local-variable", - "name": "triggerName" + "name": "enabled" }, { "file": "internal/automation/center.go", - "line": 606, - "kind": "local-variable", - "name": "msg" - }, - { - "file": "internal/automation/center.go", - "line": 610, - "kind": "local-variable", - "name": "msg" - }, - { - "file": "internal/automation/center.go", - "line": 614, - "kind": "function", - "name": "notifyRunNeedsReview" - }, - { - "file": "internal/automation/center.go", - "line": 618, - "kind": "local-variable", - "name": "task" - }, - { - "file": "internal/automation/center.go", - "line": 624, - "kind": "function", - "name": "actionDelaySeconds" - }, - { - "file": "internal/automation/center.go", - "line": 628, - "kind": "var", - "name": "cfg" - }, - { - "file": "internal/automation/center.go", - "line": 632, - "kind": "local-variable", - "name": "card" - }, - { - "file": "internal/automation/center.go", - "line": 632, + "line": 597, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 645, + "line": 607, "kind": "function", - "name": "prepareTask" + "name": "confirmShipmentAttempt" }, { "file": "internal/automation/center.go", - "line": 655, + "line": 608, "kind": "local-variable", - "name": "needsDetail" + "name": "credentialUnlock" }, { "file": "internal/automation/center.go", - "line": 656, + "line": 609, "kind": "local-variable", - "name": "err" + "name": "credentialLocked" }, { "file": "internal/automation/center.go", - "line": 656, + "line": 622, "kind": "local-variable", - "name": "existing" + "name": "completeSnapshot" }, { "file": "internal/automation/center.go", - "line": 670, + "line": 626, "kind": "local-variable", "name": "cookieStr" }, { "file": "internal/automation/center.go", - "line": 672, + "line": 627, "kind": "var", - "name": "err" + "name": "mtopCtx" }, { "file": "internal/automation/center.go", - "line": 678, + "line": 628, + "kind": "var", + "name": "cookieSession" + }, + { + "file": "internal/automation/center.go", + "line": 629, "kind": "local-variable", - "name": "detail" + "name": "ok" }, { "file": "internal/automation/center.go", - "line": 678, + "line": 629, "kind": "local-variable", - "name": "err" + "name": "snapshot" }, { "file": "internal/automation/center.go", - "line": 714, + "line": 634, + "kind": "local-variable", + "name": "callErr" + }, + { + "file": "internal/automation/center.go", + "line": 634, + "kind": "local-variable", + "name": "ok" + }, + { + "file": "internal/automation/center.go", + "line": 634, + "kind": "local-variable", + "name": "ret" + }, + { + "file": "internal/automation/center.go", + "line": 634, + "kind": "local-variable", + "name": "updated" + }, + { + "file": "internal/automation/center.go", + "line": 635, + "kind": "var", + "name": "persistenceErrs" + }, + { + "file": "internal/automation/center.go", + "line": 636, + "kind": "local-variable", + "name": "runtimeCookie" + }, + { + "file": "internal/automation/center.go", + "line": 637, + "kind": "local-variable", + "name": "runtimeCookieChanged" + }, + { + "file": "internal/automation/center.go", + "line": 638, + "kind": "local-variable", + "name": "changed" + }, + { + "file": "internal/automation/center.go", + "line": 638, + "kind": "local-variable", + "name": "snapshot" + }, + { + "file": "internal/automation/center.go", + "line": 638, + "kind": "local-variable", + "name": "value" + }, + { + "file": "internal/automation/center.go", + "line": 641, + "kind": "local-variable", + "name": "sessionHandled" + }, + { + "file": "internal/automation/center.go", + "line": 643, + "kind": "local-variable", + "name": "metadata" + }, + { + "file": "internal/automation/center.go", + "line": 647, + "kind": "local-variable", + "name": "saveErr" + }, + { + "file": "internal/automation/center.go", + "line": 658, + "kind": "local-variable", + "name": "metadata" + }, + { + "file": "internal/automation/center.go", + "line": 659, + "kind": "local-variable", + "name": "saveErr" + }, + { + "file": "internal/automation/center.go", + "line": 669, + "kind": "local-variable", + "name": "running" + }, + { + "file": "internal/automation/center.go", + "line": 669, + "kind": "local-variable", + "name": "sender" + }, + { + "file": "internal/automation/center.go", + "line": 676, + "kind": "local-variable", + "name": "sessionErr" + }, + { + "file": "internal/automation/center.go", + "line": 700, + "kind": "local-variable", + "name": "failure" + }, + { + "file": "internal/automation/center.go", + "line": 706, + "kind": "local-variable", + "name": "sysShip" + }, + { + "file": "internal/automation/center.go", + "line": 707, + "kind": "local-variable", + "name": "upsertErr" + }, + { + "file": "internal/automation/center.go", + "line": 717, + "kind": "local-variable", + "name": "eventErr" + }, + { + "file": "internal/automation/center.go", + "line": 726, "kind": "function", - "name": "mergeOrderIntoTask" + "name": "wakeCredentialBlockedAutomation" + }, + { + "file": "internal/automation/center.go", + "line": 730, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 735, + "kind": "function", + "name": "sendCard" }, { "file": "internal/automation/center.go", "line": 742, - "kind": "function", - "name": "executeAction" + "kind": "local-variable", + "name": "count" }, { "file": "internal/automation/center.go", - "line": 749, + "line": 743, "kind": "local-variable", - "name": "text" + "name": "card" + }, + { + "file": "internal/automation/center.go", + "line": 743, + "kind": "local-variable", + "name": "err" }, { "file": "internal/automation/center.go", "line": 753, "kind": "local-variable", + "name": "sent" + }, + { + "file": "internal/automation/center.go", + "line": 754, + "kind": "local-variable", + "name": "i" + }, + { + "file": "internal/automation/center.go", + "line": 755, + "kind": "local-variable", + "name": "content" + }, + { + "file": "internal/automation/center.go", + "line": 755, + "kind": "local-variable", + "name": "err" + }, + { + "file": "internal/automation/center.go", + "line": 755, + "kind": "local-variable", + "name": "imageURL" + }, + { + "file": "internal/automation/center.go", + "line": 760, + "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", "line": 765, - "kind": "function", - "name": "confirmShipment" - }, - { - "file": "internal/automation/center.go", - "line": 769, - "kind": "local-variable", - "name": "enabled" - }, - { - "file": "internal/automation/center.go", - "line": 769, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 779, - "kind": "function", - "name": "confirmShipmentAttempt" - }, - { - "file": "internal/automation/center.go", - "line": 780, - "kind": "local-variable", - "name": "credentialUnlock" - }, - { - "file": "internal/automation/center.go", - "line": 781, - "kind": "local-variable", - "name": "credentialLocked" - }, - { - "file": "internal/automation/center.go", - "line": 794, - "kind": "local-variable", - "name": "completeSnapshot" - }, - { - "file": "internal/automation/center.go", - "line": 798, - "kind": "local-variable", - "name": "cookieStr" - }, - { - "file": "internal/automation/center.go", - "line": 799, - "kind": "var", - "name": "mtopCtx" - }, - { - "file": "internal/automation/center.go", - "line": 800, - "kind": "var", - "name": "cookieSession" - }, - { - "file": "internal/automation/center.go", - "line": 801, - "kind": "local-variable", - "name": "ok" - }, - { - "file": "internal/automation/center.go", - "line": 801, - "kind": "local-variable", - "name": "snapshot" - }, - { - "file": "internal/automation/center.go", - "line": 806, - "kind": "local-variable", - "name": "callErr" - }, - { - "file": "internal/automation/center.go", - "line": 806, - "kind": "local-variable", - "name": "ok" - }, - { - "file": "internal/automation/center.go", - "line": 806, - "kind": "local-variable", - "name": "ret" - }, - { - "file": "internal/automation/center.go", - "line": 806, - "kind": "local-variable", - "name": "updated" - }, - { - "file": "internal/automation/center.go", - "line": 807, - "kind": "var", - "name": "persistenceErrs" - }, - { - "file": "internal/automation/center.go", - "line": 808, - "kind": "local-variable", - "name": "runtimeCookie" - }, - { - "file": "internal/automation/center.go", - "line": 809, - "kind": "local-variable", - "name": "runtimeCookieChanged" - }, - { - "file": "internal/automation/center.go", - "line": 810, - "kind": "local-variable", - "name": "changed" - }, - { - "file": "internal/automation/center.go", - "line": 810, - "kind": "local-variable", - "name": "snapshot" - }, - { - "file": "internal/automation/center.go", - "line": 810, - "kind": "local-variable", - "name": "value" - }, - { - "file": "internal/automation/center.go", - "line": 813, - "kind": "local-variable", - "name": "sessionHandled" - }, - { - "file": "internal/automation/center.go", - "line": 815, - "kind": "local-variable", - "name": "metadata" - }, - { - "file": "internal/automation/center.go", - "line": 819, - "kind": "local-variable", - "name": "saveErr" - }, - { - "file": "internal/automation/center.go", - "line": 830, - "kind": "local-variable", - "name": "metadata" - }, - { - "file": "internal/automation/center.go", - "line": 831, - "kind": "local-variable", - "name": "saveErr" - }, - { - "file": "internal/automation/center.go", - "line": 841, - "kind": "local-variable", - "name": "running" - }, - { - "file": "internal/automation/center.go", - "line": 841, - "kind": "local-variable", - "name": "sender" - }, - { - "file": "internal/automation/center.go", - "line": 848, - "kind": "local-variable", - "name": "sessionErr" - }, - { - "file": "internal/automation/center.go", - "line": 872, - "kind": "local-variable", - "name": "failure" - }, - { - "file": "internal/automation/center.go", - "line": 878, - "kind": "local-variable", - "name": "sysShip" - }, - { - "file": "internal/automation/center.go", - "line": 879, - "kind": "local-variable", - "name": "upsertErr" - }, - { - "file": "internal/automation/center.go", - "line": 889, - "kind": "local-variable", - "name": "eventErr" - }, - { - "file": "internal/automation/center.go", - "line": 898, - "kind": "function", - "name": "wakeCredentialBlockedAutomation" - }, - { - "file": "internal/automation/center.go", - "line": 902, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 907, - "kind": "function", - "name": "sendCard" - }, - { - "file": "internal/automation/center.go", - "line": 914, - "kind": "local-variable", - "name": "count" - }, - { - "file": "internal/automation/center.go", - "line": 915, - "kind": "local-variable", - "name": "card" - }, - { - "file": "internal/automation/center.go", - "line": 915, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 925, - "kind": "local-variable", - "name": "sent" - }, - { - "file": "internal/automation/center.go", - "line": 926, - "kind": "local-variable", - "name": "i" - }, - { - "file": "internal/automation/center.go", - "line": 927, - "kind": "local-variable", - "name": "content" - }, - { - "file": "internal/automation/center.go", - "line": 927, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 927, - "kind": "local-variable", - "name": "imageURL" - }, - { - "file": "internal/automation/center.go", - "line": 932, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 937, - "kind": "local-variable", - "name": "err" - }, - { - "file": "internal/automation/center.go", - "line": 949, + "line": 777, "kind": "function", "name": "sendDataCard" }, { "file": "internal/automation/center.go", - "line": 950, + "line": 778, "kind": "local-variable", "name": "unlock" }, { "file": "internal/automation/center.go", - "line": 952, + "line": 780, "kind": "local-variable", "name": "sent" }, { "file": "internal/automation/center.go", - "line": 953, + "line": 781, "kind": "local-variable", "name": "i" }, { "file": "internal/automation/center.go", - "line": 954, + "line": 782, "kind": "local-variable", "name": "content" }, { "file": "internal/automation/center.go", - "line": 954, + "line": 782, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 959, + "line": 787, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 961, + "line": 789, "kind": "local-variable", "name": "restoreErr" }, { "file": "internal/automation/center.go", - "line": 975, + "line": 803, "kind": "function", "name": "classifyMessageSendError" }, { "file": "internal/automation/center.go", - "line": 982, + "line": 810, "kind": "function", "name": "accountAutomationAllowed" }, { "file": "internal/automation/center.go", - "line": 983, + "line": 811, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 983, + "line": 811, "kind": "local-variable", "name": "paused" }, { "file": "internal/automation/center.go", - "line": 987, + "line": 815, "kind": "local-variable", "name": "enabled" }, { "file": "internal/automation/center.go", - "line": 987, + "line": 815, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 994, + "line": 822, "kind": "function", "name": "accountSenderReady" }, { "file": "internal/automation/center.go", - "line": 998, + "line": 826, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 998, + "line": 826, "kind": "local-variable", "name": "sender" }, { "file": "internal/automation/center.go", - "line": 1002, + "line": 830, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 1002, + "line": 830, "kind": "local-variable", "name": "ready" }, { "file": "internal/automation/center.go", - "line": 1008, + "line": 836, "kind": "function", "name": "lockCard" }, { "file": "internal/automation/center.go", - "line": 1009, + "line": 837, "kind": "local-variable", "name": "raw" }, { "file": "internal/automation/center.go", - "line": 1010, + "line": 838, "kind": "local-variable", "name": "mu" }, { "file": "internal/automation/center.go", - "line": 1015, + "line": 843, "kind": "function", "name": "actionMatchesOrderSpec" }, { "file": "internal/automation/center.go", - "line": 1016, + "line": 844, "kind": "var", "name": "cfg" }, { "file": "internal/automation/center.go", - "line": 1030, + "line": 858, "kind": "function", "name": "deliverySendCount" }, { "file": "internal/automation/center.go", - "line": 1031, + "line": 859, "kind": "local-variable", "name": "perUnit" }, { "file": "internal/automation/center.go", - "line": 1035, + "line": 863, "kind": "local-variable", "name": "qty" }, { "file": "internal/automation/center.go", - "line": 1042, + "line": 870, "kind": "function", "name": "parsePositiveInt" }, { "file": "internal/automation/center.go", - "line": 1047, + "line": 875, "kind": "local-variable", "name": "err" }, { "file": "internal/automation/center.go", - "line": 1047, + "line": 875, "kind": "local-variable", "name": "n" }, { "file": "internal/automation/center.go", - "line": 1054, + "line": 882, "kind": "function", "name": "cardContent" }, { "file": "internal/automation/center.go", - "line": 1075, + "line": 903, "kind": "function", "name": "sendText" }, { "file": "internal/automation/center.go", - "line": 1082, + "line": 910, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 1082, + "line": 910, "kind": "local-variable", "name": "sender" }, { "file": "internal/automation/center.go", - "line": 1089, + "line": 917, "kind": "function", "name": "sendImage" }, { "file": "internal/automation/center.go", - "line": 1096, + "line": 924, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 1096, + "line": 924, "kind": "local-variable", "name": "sender" }, { "file": "internal/automation/center.go", - "line": 1103, + "line": 931, "kind": "function", "name": "cookieValue" }, { "file": "internal/automation/center.go", - "line": 1114, + "line": 942, "kind": "function", "name": "buildTriggerKey" }, { "file": "internal/automation/center.go", - "line": 1116, + "line": 944, "kind": "local-variable", "name": "attempt" }, { "file": "internal/automation/center.go", - "line": 1116, + "line": 944, "kind": "local-variable", "name": "ok" }, { "file": "internal/automation/center.go", - "line": 1129, + "line": 957, "kind": "function", "name": "renderTemplate" }, { "file": "internal/automation/center.go", - "line": 1130, + "line": 958, "kind": "local-variable", "name": "out" }, { "file": "internal/automation/center.go", - "line": 1131, + "line": 959, "kind": "local-variable", "name": "repl" }, { "file": "internal/automation/center.go", - "line": 1142, + "line": 970, "kind": "range-variable", "name": "k" }, { "file": "internal/automation/center.go", - "line": 1142, + "line": 970, "kind": "range-variable", "name": "v" }, @@ -14273,6 +13997,30 @@ "kind": "local-variable", "name": "err" }, + { + "file": "internal/automation/run_coordinator.go", + "line": 164, + "kind": "local-variable", + "name": "reason" + }, + { + "file": "internal/automation/run_coordinator.go", + "line": 204, + "kind": "local-variable", + "name": "dueAt" + }, + { + "file": "internal/automation/run_coordinator.go", + "line": 241, + "kind": "local-variable", + "name": "reason" + }, + { + "file": "internal/automation/run_coordinator.go", + "line": 250, + "kind": "local-variable", + "name": "err" + }, { "file": "internal/automation/scheduler.go", "line": 15, diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index cb26ea8..fc81621 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -166,7 +166,8 @@ app shell / routes - 已完成阶段 6 第三个 PR 切片“Engine WebSocket 记录与消息分发边界”:将 WS 记录 worker、消息去重、防抖和并发信号量分别收口到独立组件;`Account` 保留兼容 facade,系统事件背压、聊天洪峰限流、消息顺序、自动化事件优先级和 Stop 等待语义保持不变;全量 Go 测试、Engine/Server race、vet、lint、注释和前端构建门禁通过并合并为一个可回滚提交; - 已完成阶段 6 第四个 PR 切片“Engine 凭证状态与 Token 生命周期边界”:将 Cookie 快照、Token 缓存、刷新锁、设备指纹和刷新诊断状态收口到独立 `credentialState`;运行状态锁改为显式 `runtimeMu`,避免匿名组件锁冲突;保留风控恢复、刷新锁、Cookie/Token 绑定和 WebSocket 注册前快照复核语义;Engine 全量测试、Engine/Server race、vet、lint、注释和前端构建门禁通过并合并为一个可回滚提交; - 已完成阶段 6 第五个 PR 切片“Automation 事件事实与规则匹配边界”:新增事件事实记录器、无动作副作用的规则匹配器和纯动作计划器;`Center` 与 `Scheduler` 统一通过组件查询规则和生成动作计划,规则匹配不执行外部动作,付款发货动作顺序、规格匹配、延迟快照和恢复语义保持不变;Automation/Server race、全量测试、vet、lint、注释和前端构建门禁通过并合并为一个可回滚提交; -- 阶段 6 下一 PR 切片为“Automation 运行协调与动作执行边界”:提取运行创建、动作检查点、延迟恢复和不确定结果隔离的协调组件,保留外部动作已执行/未执行/结果未知三态语义,不改变现有数据库状态机; +- 已完成阶段 6 第六个 PR 切片“Automation 运行协调与动作执行边界”:新增 `automationRunCoordinator`,统一运行创建/恢复、动作前后检查点、延迟任务续租、账号门禁和不确定结果隔离;`Center` 保留兼容调用入口,外部动作已执行/未执行/结果未知三态语义、人工核对状态机和恢复游标保持不变;Automation 测试、注释和 diff 门禁通过,整批修改合并为一个可回滚提交; +- 阶段 6 下一 PR 切片为“Automation 发货、卡密与通知动作边界”:提取确认发货、卡密分配、消息发送和结果通知的动作执行组件,保留凭证锁、卡券锁、发送错误分类和通知幂等语义; - 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。 ## 6. 阶段 0:治理文档与强约束 @@ -582,3 +583,4 @@ npm --prefix frontend run build | 2026-08-14 | 完成阶段 6 第三个 PR 切片“Engine WebSocket 记录与消息分发边界” | 新增 `messageDispatcher` 与 `wsRecorder`,分别管理消息去重/防抖/并发投递和 WebSocket 诊断记录队列/worker;`Account` 仅保留 facade 与生命周期接线,动态 Handler、系统事件背压、聊天洪峰限流和 Stop 等待语义保持兼容;Engine 全量回归与并发测试、后续门禁通过并合并为一个可回滚提交 | 阶段 6:Engine 凭证状态与 Token 生命周期边界 | | 2026-08-14 | 完成阶段 6 第四个 PR 切片“Engine 凭证状态与 Token 生命周期边界” | 新增 `credentialState`,集中 Cookie 快照、Token 缓存、刷新锁、设备指纹和刷新诊断状态;运行状态组件锁改名为 `runtimeMu`,保持 `Account` facade 字段访问兼容;Cookie 更新、Token 清理、风控恢复和 WS 注册前凭证快照复核语义保持不变;Engine 全量测试、Engine/Server race、vet、lint、注释和前端构建门禁通过并合并为一个可回滚提交 | 阶段 6:Automation 事件事实与规则匹配边界 | | 2026-08-14 | 完成阶段 6 第五个 PR 切片“Automation 事件事实与规则匹配边界” | 新增 `eventFactRecorder`、`ruleMatcher` 和 `actionPlanner`;事件事实写入、规则查询和动作计划生成职责分离,`Center`/`Scheduler` 不再直接散落调用规则匹配;动作计划保持付款发卡优先、规格过滤和延迟快照语义,新增纯计划与无订单事实回归测试;Automation/Server race、全量测试、vet、lint、注释和前端构建门禁通过并合并为一个可回滚提交 | 阶段 6:Automation 运行协调与动作执行边界 | +| 2026-08-14 | 完成阶段 6 第六个 PR 切片“Automation 运行协调与动作执行边界” | 新增 `automationRunCoordinator`,统一运行创建/恢复、动作前后检查点、延迟任务续租、账号门禁和不确定结果隔离;`Center` 保留兼容调用入口,外部动作三态语义、人工核对状态机和恢复游标保持不变;Automation 测试、注释和 diff 门禁通过并合并为一个可回滚提交 | 阶段 6:Automation 发货、卡密与通知动作边界 | diff --git a/internal/automation/center.go b/internal/automation/center.go index c8d7d3e..b38029b 100644 --- a/internal/automation/center.go +++ b/internal/automation/center.go @@ -91,7 +91,9 @@ type Center struct { // rules 查询适用规则,不执行规则动作。 rules ruleMatcher // planner 生成不可变动作计划,不执行外部 I/O。 - planner actionPlanner + planner actionPlanner + // runs 协调自动化运行创建、动作检查点、延迟恢复和结果隔离。 + runs automationRunCoordinator store *db.Store senders SenderProvider fetcher OrderDetailFetcher @@ -113,7 +115,7 @@ func New(store *db.Store, senders SenderProvider, logger *slog.Logger) *Center { logger = slog.Default() } client := mtop.NewClient() - return &Center{ + center := &Center{ facts: newEventFactRecorder(store), rules: newRuleMatcher(store), planner: actionPlanner{}, @@ -123,6 +125,19 @@ func New(store *db.Store, senders SenderProvider, logger *slog.Logger) *Center { accountTasks: client, logger: logger.With("subsys", "automation"), } + center.runs = automationRunCoordinator{ + store: store, + planner: center.planner, + logger: center.logger, + prepareTask: center.prepareTask, + actionDelaySeconds: center.actionDelaySeconds, + accountAutomationAllowed: center.accountAutomationAllowed, + deferTask: center.deferTask, + executeAction: center.executeAction, + hasNotifier: func() bool { return center.notifier != nil }, + notifyResult: center.notifyResult, + } + return center } // SetMTop 注入 mtop 客户端(确认发货用)。未注入时使用默认 HTTP 实现。 @@ -391,201 +406,14 @@ func (c *Center) ManualFullDelivery(ctx context.Context, order *db.Order) (int, return sent, nil } +// executeRule 将规则执行委托给运行协调器,保持 Center 的兼容调用入口。 func (c *Center) executeRule(ctx context.Context, task Task, rule db.AutomationRule) error { - var err error - if len(task.ActionPlan) == 0 && task.TriggerType != TriggerOrderPaid { - task.ActionPlan = c.planner.plan(task, rule.Actions) - } - task, err = c.prepareTask(ctx, task) - if err != nil { - return &preparationError{err: err} - } - triggerKey := buildTriggerKey(task) - if triggerKey == "" { - return nil - } - if len(task.ActionPlan) == 0 { - task.ActionPlan = c.planner.plan(task, rule.Actions) - } - retryTask := task - retryTask.CookieStr = "" - rawJSON, _ := json.Marshal(retryTask) - var run *db.AutomationRun - if resumeID := taskAutomationRunID(task); resumeID > 0 { - run, err = c.store.Automation.GetRun(ctx, resumeID) - if err != nil { - return err - } - if run.Status != "running" || run.RuleID != rule.ID { - return nil - } - } else { - var runID int64 - var started bool - runID, started, err = c.store.Automation.TryStartRun(ctx, db.AutomationRun{ - RuleID: rule.ID, CookieID: task.AccountID, ItemID: task.ItemID, OrderID: task.OrderID, - BuyerID: task.BuyerID, ChatID: task.ChatID, TriggerType: task.TriggerType, - TriggerKey: triggerKey, RawEventJSON: string(rawJSON), - LeaseExpiresAt: time.Now().UTC().Add(5 * time.Minute).Unix(), - }) - if err != nil || !started { - return err - } - run, err = c.store.Automation.GetRun(ctx, runID) - if err != nil { - return err - } - } - status := "success" - errMsg := "" - sent := run.SentCount - finish := true - defer func() { - if !finish { - return - } - finishCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - if finishErr := c.store.Automation.FinishRun(finishCtx, run.ID, run.AttemptCount, status, sent, errMsg); finishErr != nil { - c.logger.Error("保存自动化执行结果失败", "run_id", run.ID, "err", finishErr) - } - cancel() - if c.notifier != nil { - c.notifyResult(task, status, sent, errMsg) - } - }() - actions := task.ActionPlan - if task.TriggerType == TriggerOrderPaid && !c.planner.hasMatchingSendCard(task, actions) { - status, errMsg = "failed", "未匹配到订单规格对应的卡密动作" - return errors.New(errMsg) - } - var deferred bool - sent, deferred, err = c.executeRunActions(ctx, task, rule.ID, run, actions, false) - if deferred { - finish = false - return errAutomationDeferred - } - if errors.Is(err, errAutomationNeedsReview) { - finish = false - if c.notifier != nil && !errors.Is(err, errAutomationQuarantine) { - c.notifyResult(task, "needs_review", sent, err.Error()) - } - return err - } - if err != nil { - if sent > 0 && !errors.Is(err, ErrMessageNotSent) && !errors.Is(err, errActionNotPerformed) { - reason := "运行已完成部分动作,后续动作失败,已禁止从头自动重放: " + err.Error() - if quarantineErr := c.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent, reason); quarantineErr != nil { - finish = false - c.logger.Error("保存自动化人工核对状态失败", "run_id", run.ID, "err", quarantineErr) - return errors.Join(errAutomationNeedsReview, errAutomationQuarantine, err, quarantineErr) - } - finish = false - if c.notifier != nil { - c.notifyResult(task, "needs_review", sent, reason) - } - return fmt.Errorf("%w: %v", errAutomationNeedsReview, err) - } - status, errMsg = "failed", err.Error() - if errors.Is(err, ErrMessageNotSent) || errors.Is(err, errActionNotPerformed) { - errMsg = db.SafeRetryErrorPrefix + errMsg - } - return err - } - if task.TriggerType == TriggerReviewMissingTimeout && task.OrderID != "" { - if incrementErr := c.store.Automation.IncrementReviewRequest(ctx, task.OrderID); incrementErr != nil { - reason := "求评价消息已发送,但保存提醒次数失败,已停止自动重放: " + incrementErr.Error() - if quarantineErr := c.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent, reason); quarantineErr != nil { - finish = false - c.logger.Error("保存求评价人工核对状态失败", "run_id", run.ID, "err", quarantineErr) - return errors.Join(errAutomationNeedsReview, errAutomationQuarantine, incrementErr, quarantineErr) - } - finish = false - if c.notifier != nil { - c.notifyResult(task, "needs_review", sent, reason) - } - return fmt.Errorf("%w: %v", errAutomationNeedsReview, incrementErr) - } - } - return nil + return c.runs.executeRule(ctx, task, rule) } +// executeRunActions 将动作执行委托给运行协调器,保持手动发货等调用方的兼容入口。 func (c *Center) executeRunActions(ctx context.Context, task Task, ruleID int64, run *db.AutomationRun, actions []db.AutomationAction, skipDelays bool) (int, bool, error) { - sent := run.SentCount - for cursor := run.ActionCursor; cursor < len(actions); cursor++ { - action := actions[cursor] - if !skipDelays { - delaySeconds, err := c.actionDelaySeconds(ctx, action) - if err != nil { - return sent, false, err - } - if delaySeconds > 0 && taskDelayCursor(task) != cursor { - if task.Raw == nil { - task.Raw = map[string]any{} - } - task.Raw["automation_run_id"] = run.ID - task.Raw["automation_rule_id"] = ruleID - task.Raw["automation_delay_cursor"] = cursor - dueAt := time.Now().UTC().Add(time.Duration(delaySeconds) * time.Second) - if err := c.store.Automation.RenewRunLease(ctx, run.ID, run.AttemptCount, dueAt.Add(5*time.Minute).Unix()); err != nil { - return sent, false, err - } - if err := c.deferTask(ctx, task, dueAt.Unix()); err != nil { - return sent, false, err - } - return sent, true, nil - } - } - started, err := c.store.Automation.StartRunAction(ctx, run.ID, run.AttemptCount, cursor, time.Now().UTC().Add(5*time.Minute).Unix()) - if err != nil || !started { - if err == nil { - err = errors.New("自动化动作已被其他 worker 领取") - } - return sent, false, err - } - n, actionErr := c.executeActionNow(ctx, task, action) - if actionErr != nil { - var uncertain *uncertainActionError - if n > 0 || errors.As(actionErr, &uncertain) { - reason := "外部动作可能已部分或全部执行,已禁止自动重放,请人工核对: " + actionErr.Error() - if quarantineErr := c.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent+n, reason); quarantineErr != nil { - c.logger.Error("保存不确定动作人工核对状态失败", "run_id", run.ID, "err", quarantineErr) - return sent + n, false, errors.Join(errAutomationNeedsReview, errAutomationQuarantine, actionErr, quarantineErr) - } - return sent + n, false, fmt.Errorf("%w: %v", errAutomationNeedsReview, actionErr) - } - if abortErr := c.store.Automation.AbortRunAction(ctx, run.ID, run.AttemptCount, cursor); abortErr != nil { - reason := "外部动作明确未执行,但清除动作占用状态失败,已停止自动重放: " + abortErr.Error() - if quarantineErr := c.store.Automation.QuarantineRun(ctx, run.ID, run.AttemptCount, reason); quarantineErr != nil { - c.logger.Error("隔离动作状态异常的自动化运行失败", "run_id", run.ID, "err", quarantineErr) - } - return sent, false, fmt.Errorf("%w: %s", errAutomationNeedsReview, reason) - } - return sent, false, actionErr - } - if err := c.store.Automation.AdvanceRunAction(ctx, run.ID, run.AttemptCount, cursor, n); err != nil { - if quarantineErr := c.store.Automation.QuarantineRun(ctx, run.ID, run.AttemptCount, "动作已执行但检查点保存失败,请人工核对,禁止自动重放: "+err.Error()); quarantineErr != nil { - c.logger.Error("保存检查点异常的人工核对状态失败", "run_id", run.ID, "err", quarantineErr) - return sent + n, false, errors.Join(errAutomationNeedsReview, errAutomationQuarantine, err, quarantineErr) - } - return sent + n, false, fmt.Errorf("%w: %v", errAutomationNeedsReview, err) - } - sent += n - if task.Raw != nil { - delete(task.Raw, "automation_delay_cursor") - } - } - return sent, false, nil -} - -func (c *Center) executeActionNow(ctx context.Context, task Task, action db.AutomationAction) (int, error) { - allowed, err := c.accountAutomationAllowed(ctx, task.AccountID) - if err != nil { - return 0, err - } - if !allowed { - return 0, fmt.Errorf("账号已暂停或停用,取消自动化动作") - } - return c.executeAction(ctx, task, action) + return c.runs.executeRunActions(ctx, task, ruleID, run, actions, skipDelays) } // notifyResult 根据规则执行结果发送通知。成功且实际发出了内容才通知, diff --git a/internal/automation/run_coordinator.go b/internal/automation/run_coordinator.go new file mode 100644 index 0000000..9e8a1af --- /dev/null +++ b/internal/automation/run_coordinator.go @@ -0,0 +1,277 @@ +package automation + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "time" + + "xianyu-go/internal/db" +) + +// automationRunCoordinator 负责自动化运行的创建、续租、动作检查点和结果收口;它不决定具体业务动作,只协调动作执行过程中的持久化状态和三态结果。 +type automationRunCoordinator struct { + // store 提供自动化运行、延迟任务和人工核对状态的持久化能力。 + store *db.Store + // planner 根据事件和规则生成动作计划,保证协调器不直接解释规则细节。 + planner actionPlanner + // logger 记录运行状态收口和检查点异常。 + logger *slog.Logger + // prepareTask 在创建或恢复运行前补全订单事实和凭证上下文。 + prepareTask func(context.Context, Task) (Task, error) + // actionDelaySeconds 计算当前动作的有效延迟时间。 + actionDelaySeconds func(context.Context, db.AutomationAction) (int, error) + // accountAutomationAllowed 检查账号是否仍允许执行外部自动化动作。 + accountAutomationAllowed func(context.Context, string) (bool, error) + // deferTask 持久化等待延迟后继续执行的任务。 + deferTask func(context.Context, Task, int64) error + // executeAction 执行一个已经通过账号门禁的具体外部动作。 + executeAction func(context.Context, Task, db.AutomationAction) (int, error) + // hasNotifier 判断当前是否注入了结果通知器。 + hasNotifier func() bool + // notifyResult 将运行结果转换为用户可见的通知。 + notifyResult func(Task, string, int, string) +} + +// executeRule 创建或恢复一次自动化运行,并统一处理运行成功、失败、延期和人工核对结果。 +func (r automationRunCoordinator) executeRule(ctx context.Context, task Task, rule db.AutomationRule) error { + // err 保存任务准备、运行创建或动作执行阶段的错误。 + var err error + if len(task.ActionPlan) == 0 && task.TriggerType != TriggerOrderPaid { + task.ActionPlan = r.planner.plan(task, rule.Actions) + } + // preparedTask 保存补全订单事实和凭证上下文后的任务。 + preparedTask, prepareErr := r.prepareTask(ctx, task) + if prepareErr != nil { + return &preparationError{err: prepareErr} + } + task = preparedTask + // triggerKey 是用于幂等创建运行的稳定事件键。 + triggerKey := buildTriggerKey(task) + if triggerKey == "" { + return nil + } + if len(task.ActionPlan) == 0 { + task.ActionPlan = r.planner.plan(task, rule.Actions) + } + // retryTask 是去除敏感 Cookie 后写入运行快照的任务副本。 + retryTask := task + retryTask.CookieStr = "" + // rawJSON 是用于恢复运行的无敏感任务快照。 + rawJSON, _ := json.Marshal(retryTask) + // run 保存当前自动化运行及其动作检查点。 + var run *db.AutomationRun + // resumeID 是延迟任务或恢复任务携带的既有运行 ID。 + if resumeID := taskAutomationRunID(task); resumeID > 0 { + run, err = r.store.Automation.GetRun(ctx, resumeID) + if err != nil { + return err + } + if run.Status != "running" || run.RuleID != rule.ID { + return nil + } + } else { + // runID 是新建运行的数据库主键。 + var runID int64 + // started 表示当前事件是否成功抢到幂等运行的执行权。 + var started bool + runID, started, err = r.store.Automation.TryStartRun(ctx, db.AutomationRun{ + RuleID: rule.ID, CookieID: task.AccountID, ItemID: task.ItemID, OrderID: task.OrderID, + BuyerID: task.BuyerID, ChatID: task.ChatID, TriggerType: task.TriggerType, + TriggerKey: triggerKey, RawEventJSON: string(rawJSON), + LeaseExpiresAt: time.Now().UTC().Add(5 * time.Minute).Unix(), + }) + if err != nil || !started { + return err + } + // loadedRun 是创建后重新读取的完整运行状态。 + loadedRun, getErr := r.store.Automation.GetRun(ctx, runID) + if getErr != nil { + return getErr + } + run = loadedRun + } + // status 是运行完成时写入数据库的结果状态。 + status := "success" + // errMsg 是运行完成时记录的可重试或人工核对原因。 + errMsg := "" + // sent 是截至当前检查点已经确认成功的动作数量。 + sent := run.SentCount + // finish 表示函数返回时是否应执行正常运行收口。 + finish := true + defer func() { + if !finish { + return + } + // finishCtx 限制运行收口不能被原始请求取消。 + finishCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + // finishErr 保存运行结果写入失败,避免覆盖原始动作错误。 + if finishErr := r.store.Automation.FinishRun(finishCtx, run.ID, run.AttemptCount, status, sent, errMsg); finishErr != nil { + r.logger.Error("保存自动化执行结果失败", "run_id", run.ID, "err", finishErr) + } + cancel() + if r.hasNotifier() { + r.notifyResult(task, status, sent, errMsg) + } + }() + // actions 是当前规则生成的完整动作计划。 + actions := task.ActionPlan + if task.TriggerType == TriggerOrderPaid && !r.planner.hasMatchingSendCard(task, actions) { + status, errMsg = "failed", "未匹配到订单规格对应的卡密动作" + return errors.New(errMsg) + } + // deferred 表示动作已保存到延迟队列,当前运行不能被标记为完成。 + var deferred bool + sent, deferred, err = r.executeRunActions(ctx, task, rule.ID, run, actions, false) + if deferred { + finish = false + return errAutomationDeferred + } + if errors.Is(err, errAutomationNeedsReview) { + finish = false + if r.hasNotifier() && !errors.Is(err, errAutomationQuarantine) { + r.notifyResult(task, "needs_review", sent, err.Error()) + } + return err + } + if err != nil { + if sent > 0 && !errors.Is(err, ErrMessageNotSent) && !errors.Is(err, errActionNotPerformed) { + // reason 说明部分动作成功后为何必须人工核对。 + reason := "运行已完成部分动作,后续动作失败,已禁止从头自动重放: " + err.Error() + // quarantineErr 保存部分成功运行的人工核对状态。 + if quarantineErr := r.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent, reason); quarantineErr != nil { + finish = false + r.logger.Error("保存自动化人工核对状态失败", "run_id", run.ID, "err", quarantineErr) + return errors.Join(errAutomationNeedsReview, errAutomationQuarantine, err, quarantineErr) + } + finish = false + if r.hasNotifier() { + r.notifyResult(task, "needs_review", sent, reason) + } + return fmt.Errorf("%w: %v", errAutomationNeedsReview, err) + } + status, errMsg = "failed", err.Error() + if errors.Is(err, ErrMessageNotSent) || errors.Is(err, errActionNotPerformed) { + errMsg = db.SafeRetryErrorPrefix + errMsg + } + return err + } + if task.TriggerType == TriggerReviewMissingTimeout && task.OrderID != "" { + // incrementErr 保存求评价消息成功后的提醒次数。 + if incrementErr := r.store.Automation.IncrementReviewRequest(ctx, task.OrderID); incrementErr != nil { + reason := "求评价消息已发送,但保存提醒次数失败,已停止自动重放: " + incrementErr.Error() + // quarantineErr 保存提醒次数写入失败的人工核对状态。 + if quarantineErr := r.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent, reason); quarantineErr != nil { + finish = false + r.logger.Error("保存求评价人工核对状态失败", "run_id", run.ID, "err", quarantineErr) + return errors.Join(errAutomationNeedsReview, errAutomationQuarantine, incrementErr, quarantineErr) + } + finish = false + if r.hasNotifier() { + r.notifyResult(task, "needs_review", sent, reason) + } + return fmt.Errorf("%w: %v", errAutomationNeedsReview, incrementErr) + } + } + return nil +} + +// executeRunActions 按动作游标执行计划,并在每个外部动作前后保存检查点。 +func (r automationRunCoordinator) executeRunActions(ctx context.Context, task Task, ruleID int64, run *db.AutomationRun, actions []db.AutomationAction, skipDelays bool) (int, bool, error) { + // sent 保存本次运行已经确认完成的动作数量。 + sent := run.SentCount + // cursor 表示当前动作在计划中的位置。 + for cursor := run.ActionCursor; cursor < len(actions); cursor++ { + // action 是当前待执行的动作定义。 + action := actions[cursor] + if !skipDelays { + // delaySeconds 是当前动作生效后的等待秒数。 + delaySeconds, err := r.actionDelaySeconds(ctx, action) + if err != nil { + return sent, false, err + } + if delaySeconds > 0 && taskDelayCursor(task) != cursor { + if task.Raw == nil { + task.Raw = map[string]any{} + } + task.Raw["automation_run_id"] = run.ID + task.Raw["automation_rule_id"] = ruleID + task.Raw["automation_delay_cursor"] = cursor + // dueAt 是动作重新进入可执行状态的时间点。 + // dueAt 是动作重新进入可执行状态的时间点。 + dueAt := time.Now().UTC().Add(time.Duration(delaySeconds) * time.Second) + // leaseErr 保存延期运行续租失败的原因。 + if leaseErr := r.store.Automation.RenewRunLease(ctx, run.ID, run.AttemptCount, dueAt.Add(5*time.Minute).Unix()); leaseErr != nil { + return sent, false, leaseErr + } + // deferErr 保存延迟任务写入失败的原因。 + if deferErr := r.deferTask(ctx, task, dueAt.Unix()); deferErr != nil { + return sent, false, deferErr + } + return sent, true, nil + } + } + // started 表示当前 worker 是否成功占用动作检查点。 + started, err := r.store.Automation.StartRunAction(ctx, run.ID, run.AttemptCount, cursor, time.Now().UTC().Add(5*time.Minute).Unix()) + if err != nil || !started { + if err == nil { + err = errors.New("自动化动作已被其他 worker 领取") + } + return sent, false, err + } + // n 保存外部动作明确成功产生的结果数量。 + n, actionErr := r.executeActionNow(ctx, task, action) + if actionErr != nil { + // uncertain 标记外部系统可能已经执行动作但本地无法确认的错误。 + var uncertain *uncertainActionError + if n > 0 || errors.As(actionErr, &uncertain) { + // reason 说明外部动作结果未知时为何必须隔离运行。 + reason := "外部动作可能已部分或全部执行,已禁止自动重放,请人工核对: " + actionErr.Error() + // quarantineErr 保存外部动作结果未知的人工核对状态。 + if quarantineErr := r.store.Automation.QuarantineRunResult(ctx, run.ID, run.AttemptCount, sent+n, reason); quarantineErr != nil { + r.logger.Error("保存不确定动作人工核对状态失败", "run_id", run.ID, "err", quarantineErr) + return sent + n, false, errors.Join(errAutomationNeedsReview, errAutomationQuarantine, actionErr, quarantineErr) + } + return sent + n, false, fmt.Errorf("%w: %v", errAutomationNeedsReview, actionErr) + } + // abortErr 清理明确未执行动作的占用检查点。 + if abortErr := r.store.Automation.AbortRunAction(ctx, run.ID, run.AttemptCount, cursor); abortErr != nil { + reason := "外部动作明确未执行,但清除动作占用状态失败,已停止自动重放: " + abortErr.Error() + // quarantineErr 保存动作检查点无法清理时的隔离结果。 + if quarantineErr := r.store.Automation.QuarantineRun(ctx, run.ID, run.AttemptCount, reason); quarantineErr != nil { + r.logger.Error("隔离动作状态异常的自动化运行失败", "run_id", run.ID, "err", quarantineErr) + } + return sent, false, fmt.Errorf("%w: %s", errAutomationNeedsReview, reason) + } + return sent, false, actionErr + } + if err := r.store.Automation.AdvanceRunAction(ctx, run.ID, run.AttemptCount, cursor, n); err != nil { + // quarantineErr 保存检查点失败后的人工核对状态。 + if quarantineErr := r.store.Automation.QuarantineRun(ctx, run.ID, run.AttemptCount, "动作已执行但检查点保存失败,请人工核对,禁止自动重放: "+err.Error()); quarantineErr != nil { + r.logger.Error("保存检查点异常的人工核对状态失败", "run_id", run.ID, "err", quarantineErr) + return sent + n, false, errors.Join(errAutomationNeedsReview, errAutomationQuarantine, err, quarantineErr) + } + return sent + n, false, fmt.Errorf("%w: %v", errAutomationNeedsReview, err) + } + sent += n + if task.Raw != nil { + delete(task.Raw, "automation_delay_cursor") + } + } + return sent, false, nil +} + +// executeActionNow 在动作真正触达外部系统前执行账号门禁。 +func (r automationRunCoordinator) executeActionNow(ctx context.Context, task Task, action db.AutomationAction) (int, error) { + // allowed 表示账号当前是否允许继续执行自动化动作。 + allowed, err := r.accountAutomationAllowed(ctx, task.AccountID) + if err != nil { + return 0, err + } + if !allowed { + return 0, fmt.Errorf("账号已暂停或停用,取消自动化动作") + } + return r.executeAction(ctx, task, action) +}