fix: patchsched hasActiveRun must not treat a real DB error as no active run
This commit is contained in:
@@ -2,12 +2,14 @@ package patchsched
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gitea.hostxtra.co.uk/mrhid6/vantage/server/internal/db"
|
||||
"gitea.hostxtra.co.uk/mrhid6/vantage/server/internal/models"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
@@ -97,7 +99,12 @@ func process(ctx context.Context, deps Deps, p models.PatchPolicy, now time.Time
|
||||
recordSkip(ctx, deps, p, "error: "+err.Error(), due, now)
|
||||
return
|
||||
}
|
||||
switch d := Decide(due, end, now, hasActiveRun(ctx, p), n); d {
|
||||
active, err := hasActiveRun(ctx, p)
|
||||
if err != nil {
|
||||
recordSkip(ctx, deps, p, "error: "+err.Error(), due, now)
|
||||
return
|
||||
}
|
||||
switch d := Decide(due, end, now, active, n); d {
|
||||
case Fire:
|
||||
if err := deps.StartPolicyRun(p, end); err != nil {
|
||||
recordSkip(ctx, deps, p, "error: "+err.Error(), due, now)
|
||||
@@ -110,11 +117,17 @@ func process(ctx context.Context, deps Deps, p models.PatchPolicy, now time.Time
|
||||
}
|
||||
}
|
||||
|
||||
func hasActiveRun(ctx context.Context, p models.PatchPolicy) bool {
|
||||
func hasActiveRun(ctx context.Context, p models.PatchPolicy) (bool, error) {
|
||||
err := db.Col("patch_runs").FindOne(ctx,
|
||||
bson.M{"instance_id": p.InstanceID, "policy_id": p.PolicyID, "status": models.PatchRunRunning},
|
||||
options.FindOne().SetProjection(bson.M{"_id": 1})).Err()
|
||||
return err == nil
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func recordSkip(ctx context.Context, deps Deps, p models.PatchPolicy, reason string, due, at time.Time) {
|
||||
|
||||
Reference in New Issue
Block a user