100 lines
4.1 KiB
Go
100 lines
4.1 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// LibraryMaintenance 是目标维护窗口的持有记录:一个 scope 只能有一个持有者。
|
|
type LibraryMaintenance struct {
|
|
Scope string `json:"scope"`
|
|
OwnerJobID string `json:"owner_job_id"`
|
|
TargetIdentity string `json:"target_identity"`
|
|
State string `json:"state"`
|
|
OriginalServiceState string `json:"original_service_state"`
|
|
Note string `json:"note"`
|
|
AcquiredAt time.Time `json:"acquired_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
ReleasedAt *time.Time `json:"released_at,omitempty"`
|
|
}
|
|
|
|
// ErrMaintenanceHeld 表示目标维护窗口被别的作业占着:任何新的写入都必须拒绝。
|
|
var ErrMaintenanceHeld = errors.New("目标正在维护中,被其他作业占用")
|
|
|
|
// AcquireLibraryMaintenance 原子地拿到维护权:
|
|
// 同一作业可以重入(接管后继续),别的作业一律拒绝——这不是"尽力而为"的锁。
|
|
func (s *Store) AcquireLibraryMaintenance(ctx context.Context, item LibraryMaintenance, now time.Time) (LibraryMaintenance, error) {
|
|
row := s.db.QueryRow(ctx, `
|
|
INSERT INTO library_maintenance (scope, owner_job_id, target_identity, state, original_service_state, note, acquired_at, updated_at)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$7)
|
|
ON CONFLICT (scope) DO UPDATE SET
|
|
owner_job_id = EXCLUDED.owner_job_id,
|
|
target_identity = EXCLUDED.target_identity,
|
|
state = EXCLUDED.state,
|
|
note = EXCLUDED.note,
|
|
updated_at = EXCLUDED.updated_at,
|
|
released_at = NULL
|
|
WHERE library_maintenance.owner_job_id = EXCLUDED.owner_job_id
|
|
RETURNING scope, owner_job_id, target_identity, state, original_service_state, note, acquired_at, updated_at, released_at`,
|
|
item.Scope, item.OwnerJobID, item.TargetIdentity, item.State, item.OriginalServiceState, item.Note, now.UTC())
|
|
acquired, err := scanMaintenance(row)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return LibraryMaintenance{}, ErrMaintenanceHeld
|
|
}
|
|
if err != nil {
|
|
return LibraryMaintenance{}, fmt.Errorf("acquire library maintenance: %w", err)
|
|
}
|
|
return acquired, nil
|
|
}
|
|
|
|
// UpdateLibraryMaintenanceState 只允许持有者改自己的窗口状态。
|
|
func (s *Store) UpdateLibraryMaintenanceState(ctx context.Context, scope, ownerJobID, state, note string, now time.Time) error {
|
|
tag, err := s.db.Exec(ctx, `UPDATE library_maintenance SET state=$3, note=$4, updated_at=$5
|
|
WHERE scope=$1 AND owner_job_id=$2 AND released_at IS NULL`, scope, ownerJobID, state, note, now.UTC())
|
|
if err != nil {
|
|
return fmt.Errorf("update library maintenance: %w", err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return errors.New("library maintenance not held by this job")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ReleaseLibraryMaintenance 只在完整验证通过后调用;失败路径必须保持窗口。
|
|
func (s *Store) ReleaseLibraryMaintenance(ctx context.Context, scope, ownerJobID string, now time.Time) error {
|
|
tag, err := s.db.Exec(ctx, `UPDATE library_maintenance SET state='released', released_at=$3, updated_at=$3
|
|
WHERE scope=$1 AND owner_job_id=$2 AND released_at IS NULL`, scope, ownerJobID, now.UTC())
|
|
if err != nil {
|
|
return fmt.Errorf("release library maintenance: %w", err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return errors.New("library maintenance not held by this job")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LibraryMaintenanceOf 读当前窗口状态(页面与诊断用)。
|
|
func (s *Store) LibraryMaintenanceOf(ctx context.Context, scope string) (*LibraryMaintenance, error) {
|
|
row := s.db.QueryRow(ctx, `SELECT scope, owner_job_id, target_identity, state, original_service_state, note,
|
|
acquired_at, updated_at, released_at FROM library_maintenance WHERE scope=$1`, scope)
|
|
item, err := scanMaintenance(row)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func scanMaintenance(row pgx.Row) (LibraryMaintenance, error) {
|
|
var item LibraryMaintenance
|
|
err := row.Scan(&item.Scope, &item.OwnerJobID, &item.TargetIdentity, &item.State,
|
|
&item.OriginalServiceState, &item.Note, &item.AcquiredAt, &item.UpdatedAt, &item.ReleasedAt)
|
|
return item, err
|
|
}
|