feat(tenant): business-date-scope monitor lease lanes and authorization-failure cancellation

Add an optional lane filter to the monitor task lease so callers can pull
a specific lane (e.g. high) and scope authorization-failure cancellation
to the failing task's business date, so a challenge on one day no longer
cancels queued tasks for another day. Monitor authorization-failure
cancellation now requires a business date and no-ops without one. Also
stop writing the nonexistent desktop_tasks.completed_at column when
canceling queued tasks for an authorization failure.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-22 23:57:10 +08:00
parent 7988a65018
commit fc7495aba0
4 changed files with 141 additions and 19 deletions
@@ -146,6 +146,11 @@ func loadQueuedDesktopAuthorizationFailureCandidates(
tx pgx.Tx,
task *repository.DesktopTask,
) ([]queuedDesktopAuthorizationFailureCandidate, error) {
businessDate := desktopAuthorizationTaskBusinessDate(task)
if task != nil && task.Kind == "monitor" && businessDate == "" {
return nil, nil
}
rows, err := tx.Query(ctx, `
SELECT desktop_id, monitor_task_id
FROM desktop_tasks
@@ -157,9 +162,19 @@ func loadQueuedDesktopAuthorizationFailureCandidates(
AND (kind = 'monitor' OR target_account_id = $5)
AND platform_id = $6
AND desktop_id <> $7
AND (
kind <> 'monitor'
OR COALESCE(
NULLIF(payload ->> 'business_date', ''),
NULLIF(payload ->> 'businessDate', ''),
NULLIF(payload ->> 'metric_date', ''),
NULLIF(payload ->> 'date', ''),
''
) = $8
)
ORDER BY COALESCE(enqueued_at, created_at) ASC, created_at ASC, desktop_id ASC
FOR UPDATE SKIP LOCKED
`, task.TenantID, task.WorkspaceID, task.Kind, task.TargetClientID, task.TargetAccountID, task.Platform, task.DesktopID)
`, task.TenantID, task.WorkspaceID, task.Kind, task.TargetClientID, task.TargetAccountID, task.Platform, task.DesktopID, businessDate)
if err != nil {
return nil, response.ErrInternal(50121, "desktop_task_authorization_failure_query_failed", "failed to select queued desktop tasks blocked by authorization failure")
}
@@ -186,6 +201,19 @@ func loadQueuedDesktopAuthorizationFailureCandidates(
return candidates, nil
}
func desktopAuthorizationTaskBusinessDate(task *repository.DesktopTask) string {
if task == nil || len(task.Payload) == 0 {
return ""
}
payload := unmarshalJSONObject(task.Payload)
for _, key := range []string{"business_date", "businessDate", "metric_date", "date"} {
if value := desktopAuthorizationFailureString(payload[key]); value != "" {
return value
}
}
return ""
}
func finishQueuedDesktopTaskForAuthorizationFailure(
ctx context.Context,
tx pgx.Tx,
@@ -193,19 +221,7 @@ func finishQueuedDesktopTaskForAuthorizationFailure(
status string,
errorJSON []byte,
) (*repository.DesktopTask, error) {
row := tx.QueryRow(ctx, `
UPDATE desktop_tasks AS t
SET status = $2,
result = NULL,
error = $3,
active_attempt_id = NULL,
lease_token_hash = NULL,
lease_expires_at = NULL,
completed_at = NOW(),
updated_at = NOW()
WHERE desktop_id = $1
AND status = 'queued'
RETURNING `+desktopTaskRepositoryReturningColumns,
row := tx.QueryRow(ctx, finishQueuedDesktopTaskForAuthorizationFailureSQL(),
desktopID,
status,
errorJSON,
@@ -220,6 +236,21 @@ func finishQueuedDesktopTaskForAuthorizationFailure(
return updated, nil
}
func finishQueuedDesktopTaskForAuthorizationFailureSQL() string {
return `
UPDATE desktop_tasks AS t
SET status = $2,
result = NULL,
error = $3,
active_attempt_id = NULL,
lease_token_hash = NULL,
lease_expires_at = NULL,
updated_at = NOW()
WHERE desktop_id = $1
AND status = 'queued'
RETURNING ` + desktopTaskRepositoryReturningColumns
}
func desktopAuthorizationFailureDispositionFromPayload(
task *repository.DesktopTask,
payload map[string]any,