From ba1bfa816c98cceb24fd4b43f15d00bd1cb0db08 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 19 Nov 2026 15:23:46 -0800 Subject: [PATCH] admin: show a pruned job's detail from its run record (#12795) * admin: show a pruned job's detail from its run record Tracking keeps the newest 1000 jobs across all job types and deletes the details of older finished ones, while each job type's run history keeps its last 10 successful and 10 failed runs. A job listed in the run history could therefore open as "job detail not found". Fall back to the run record when the job is no longer tracked. Claude-Session: https://claude.ai/code/session_011UpvafcfUuC2mLxNAXLQJi * admin: show 100% progress for a pruned successful run Co-Authored-By: Claude Opus 5.5 * admin: use the newest run record for a job across both outcomes Co-Authored-By: Claude Opus 5.5 * admin: keep the run record a pruned job's detail was built from Co-Authored-By: Claude Opus 5.5 * admin: keep looking for a run record past an unreadable history Co-Authored-By: Claude Opus 5.5 * admin: pick a pruned job's newest run record across job types Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- weed/admin/plugin/plugin_monitor.go | 108 ++++++++++++--- weed/admin/plugin/plugin_monitor_test.go | 160 +++++++++++++++++++++++ 2 files changed, 247 insertions(+), 21 deletions(-) diff --git a/weed/admin/plugin/plugin_monitor.go b/weed/admin/plugin/plugin_monitor.go index 32359e3161..0e4505a883 100644 --- a/weed/admin/plugin/plugin_monitor.go +++ b/weed/admin/plugin/plugin_monitor.go @@ -617,8 +617,15 @@ func (r *Plugin) BuildJobDetail(jobID string, activityLimit int, relatedLimit in return nil, false, err } + var runRecord *JobRunRecord if trackedSnapshot == nil && detailJob == nil { - return nil, false, nil + // Run history keeps the last runs per job type, so it can still + // list a job pruned from tracking; show what the record holds. + runRecord, err = r.findRunRecord(normalizedJobID) + if err != nil || runRecord == nil { + return nil, false, err + } + detailJob = trackedJobFromRunRecord(runRecord) } if detailJob == nil && trackedSnapshot != nil { clone := cloneTrackedJob(*trackedSnapshot) @@ -639,30 +646,16 @@ func (r *Plugin) BuildJobDetail(jobID string, activityLimit int, relatedLimit in detail := &JobDetail{ Job: detailJob, Activities: filterJobActivitiesFromSlice(activities, normalizedJobID, activityLimit), + RunRecord: runRecord, LastUpdated: timeToPtr(time.Now().UTC()), } - if history, err := r.store.LoadRunHistory(detailJob.JobType); err != nil { - return nil, true, err - } else if history != nil { - for i := range history.SuccessfulRuns { - record := history.SuccessfulRuns[i] - if strings.TrimSpace(record.JobID) == normalizedJobID { - recordCopy := record - detail.RunRecord = &recordCopy - break - } - } - if detail.RunRecord == nil { - for i := range history.ErrorRuns { - record := history.ErrorRuns[i] - if strings.TrimSpace(record.JobID) == normalizedJobID { - recordCopy := record - detail.RunRecord = &recordCopy - break - } - } + if runRecord == nil { + history, err := r.store.LoadRunHistory(detailJob.JobType) + if err != nil { + return nil, true, err } + detail.RunRecord = newestRunRecord(history, normalizedJobID) } if relatedLimit > 0 { @@ -1231,6 +1224,79 @@ func (r *Plugin) appendActivity(activity JobActivity) { r.activitiesMu.Unlock() } +func (r *Plugin) findRunRecord(jobID string) (*JobRunRecord, error) { + jobTypes, err := r.store.ListJobTypes() + if err != nil { + return nil, err + } + var newest *JobRunRecord + var loadErr error + for _, jobType := range jobTypes { + history, err := r.store.LoadRunHistory(jobType) + if err != nil { + if loadErr == nil { + loadErr = err + } + continue + } + record := newestRunRecord(history, jobID) + if record != nil && (newest == nil || runCompletedAt(*record).After(runCompletedAt(*newest))) { + newest = record + } + } + if newest != nil { + return newest, nil + } + return nil, loadErr +} + +// newestRunRecord picks across both outcome lists, since a job id can be +// run again and end the other way. +func newestRunRecord(history *JobTypeRunHistory, jobID string) *JobRunRecord { + if history == nil { + return nil + } + var newest *JobRunRecord + for _, runs := range [][]JobRunRecord{history.SuccessfulRuns, history.ErrorRuns} { + for i := range runs { + if strings.TrimSpace(runs[i].JobID) != jobID { + continue + } + if newest == nil || runCompletedAt(runs[i]).After(runCompletedAt(*newest)) { + record := runs[i] + newest = &record + } + } + } + return newest +} + +func runCompletedAt(record JobRunRecord) time.Time { + if record.CompletedAt == nil { + return time.Time{} + } + return *record.CompletedAt +} + +func trackedJobFromRunRecord(record *JobRunRecord) *TrackedJob { + job := &TrackedJob{ + JobID: record.JobID, + JobType: record.JobType, + WorkerID: record.WorkerID, + State: StateSucceeded, + Message: record.Message, + UpdatedAt: record.CompletedAt, + CompletedAt: record.CompletedAt, + } + if record.Outcome == RunOutcomeError { + job.State = StateFailed + job.ErrorMessage = record.Message + } else { + job.Progress = 100 + } + return job +} + func (r *Plugin) pruneTrackedJobsLocked() { if len(r.jobs) <= maxTrackedJobsTotal { return diff --git a/weed/admin/plugin/plugin_monitor_test.go b/weed/admin/plugin/plugin_monitor_test.go index e936709c3b..4ef9f04454 100644 --- a/weed/admin/plugin/plugin_monitor_test.go +++ b/weed/admin/plugin/plugin_monitor_test.go @@ -1,6 +1,8 @@ package plugin import ( + "os" + "path/filepath" "testing" "time" @@ -594,6 +596,164 @@ func TestBuildJobDetailIncludesActivitiesAndRunRecord(t *testing.T) { } } +func TestBuildJobDetailFallsBackToRunRecordAfterPrune(t *testing.T) { + t.Parallel() + + pluginSvc, err := New(Options{DataDir: t.TempDir()}) + if err != nil { + t.Fatalf("New: %v", err) + } + defer pluginSvc.Shutdown() + + pluginSvc.trackExecutionStart("req-pruned", "worker-p", &plugin_pb.JobSpec{ + JobId: "job-pruned", + JobType: "vacuum", + }, 1) + pluginSvc.handleJobCompleted(&plugin_pb.JobCompleted{ + RequestId: "req-pruned", + JobId: "job-pruned", + JobType: "vacuum", + Success: false, + ErrorMessage: "volume busy", + CompletedAt: timestamppb.Now(), + }) + + // What pruneTrackedJobsLocked does once newer jobs push this one out. + pluginSvc.jobsMu.Lock() + delete(pluginSvc.jobs, "job-pruned") + pluginSvc.jobsMu.Unlock() + if err := pluginSvc.store.DeleteJobDetail("job-pruned"); err != nil { + t.Fatalf("DeleteJobDetail: %v", err) + } + + detail, found, err := pluginSvc.BuildJobDetail("job-pruned", 100, 5) + if err != nil { + t.Fatalf("BuildJobDetail error: %v", err) + } + if !found || detail == nil || detail.Job == nil { + t.Fatalf("expected a detail built from the run record") + } + if detail.Job.JobType != "vacuum" || detail.Job.WorkerID != "worker-p" || detail.Job.State != StateFailed { + t.Fatalf("unexpected job from run record: %+v", detail.Job) + } + if detail.RunRecord == nil || detail.RunRecord.Outcome != RunOutcomeError { + t.Fatalf("expected the error run record, got=%+v", detail.RunRecord) + } + + if detail.Job.Progress != 0 { + t.Fatalf("failed job progress = %v, want 0", detail.Job.Progress) + } + + pluginSvc.trackExecutionStart("req-pruned-ok", "worker-p", &plugin_pb.JobSpec{ + JobId: "job-pruned-ok", + JobType: "vacuum", + }, 1) + pluginSvc.handleJobCompleted(&plugin_pb.JobCompleted{ + RequestId: "req-pruned-ok", + JobId: "job-pruned-ok", + JobType: "vacuum", + Success: true, + CompletedAt: timestamppb.Now(), + }) + pluginSvc.jobsMu.Lock() + delete(pluginSvc.jobs, "job-pruned-ok") + pluginSvc.jobsMu.Unlock() + if err := pluginSvc.store.DeleteJobDetail("job-pruned-ok"); err != nil { + t.Fatalf("DeleteJobDetail: %v", err) + } + detail, found, err = pluginSvc.BuildJobDetail("job-pruned-ok", 100, 0) + if err != nil || !found || detail == nil || detail.Job == nil { + t.Fatalf("pruned successful job: found=%v err=%v", found, err) + } + if detail.Job.State != StateSucceeded || detail.Job.Progress != 100 { + t.Fatalf("pruned successful job: state=%q progress=%v", detail.Job.State, detail.Job.Progress) + } + + if _, found, err := pluginSvc.BuildJobDetail("job-unknown", 100, 0); err != nil || found { + t.Fatalf("unknown job: found=%v err=%v", found, err) + } +} + +func TestBuildJobDetailUsesNewestRunRecord(t *testing.T) { + t.Parallel() + + pluginSvc, err := New(Options{DataDir: t.TempDir()}) + if err != nil { + t.Fatalf("New: %v", err) + } + defer pluginSvc.Shutdown() + + first := time.Now().UTC().Add(-time.Minute) + second := first.Add(30 * time.Second) + for _, record := range []*JobRunRecord{ + {RunID: "req-1", JobID: "job-rerun", JobType: "vacuum", Outcome: RunOutcomeSuccess, CompletedAt: &first}, + {RunID: "req-2", JobID: "job-rerun", JobType: "vacuum", Outcome: RunOutcomeError, Message: "volume busy", CompletedAt: &second}, + } { + if err := pluginSvc.store.AppendRunRecord("vacuum", record); err != nil { + t.Fatalf("AppendRunRecord: %v", err) + } + } + + detail, found, err := pluginSvc.BuildJobDetail("job-rerun", 100, 0) + if err != nil || !found || detail == nil || detail.Job == nil { + t.Fatalf("BuildJobDetail: found=%v err=%v", found, err) + } + if detail.Job.State != StateFailed { + t.Fatalf("job state = %q, want %q", detail.Job.State, StateFailed) + } + if detail.RunRecord == nil || detail.RunRecord.RunID != "req-2" { + t.Fatalf("expected the newer error run, got=%+v", detail.RunRecord) + } + + third := second.Add(30 * time.Second) + if err := pluginSvc.store.AppendRunRecord("volume_balance", &JobRunRecord{ + RunID: "req-3", JobID: "job-rerun", JobType: "volume_balance", Outcome: RunOutcomeSuccess, CompletedAt: &third, + }); err != nil { + t.Fatalf("AppendRunRecord: %v", err) + } + detail, found, err = pluginSvc.BuildJobDetail("job-rerun", 100, 0) + if err != nil || !found || detail == nil || detail.Job == nil { + t.Fatalf("BuildJobDetail: found=%v err=%v", found, err) + } + if detail.Job.JobType != "volume_balance" || detail.RunRecord == nil || detail.RunRecord.RunID != "req-3" { + t.Fatalf("expected the newest run across job types, job=%+v record=%+v", detail.Job, detail.RunRecord) + } +} + +func TestBuildJobDetailSkipsUnreadableRunHistory(t *testing.T) { + t.Parallel() + + pluginSvc, err := New(Options{DataDir: t.TempDir()}) + if err != nil { + t.Fatalf("New: %v", err) + } + defer pluginSvc.Shutdown() + + brokenDir := filepath.Join(pluginSvc.store.baseDir, jobTypesDirName, "aaa_broken") + if err := os.MkdirAll(brokenDir, 0o755); err != nil { + t.Fatalf("MkdirAll: %v", err) + } + if err := os.WriteFile(filepath.Join(brokenDir, runsJSONFileName), []byte("{"), 0o644); err != nil { + t.Fatalf("WriteFile: %v", err) + } + if err := pluginSvc.store.AppendRunRecord("vacuum", &JobRunRecord{ + RunID: "req-1", JobID: "job-kept", JobType: "vacuum", Outcome: RunOutcomeSuccess, + }); err != nil { + t.Fatalf("AppendRunRecord: %v", err) + } + + detail, found, err := pluginSvc.BuildJobDetail("job-kept", 100, 0) + if err != nil || !found || detail == nil || detail.Job == nil { + t.Fatalf("BuildJobDetail: found=%v err=%v", found, err) + } + if detail.Job.JobType != "vacuum" { + t.Fatalf("job type = %q, want vacuum", detail.Job.JobType) + } + if _, found, err := pluginSvc.BuildJobDetail("job-unknown", 100, 0); err == nil || found { + t.Fatalf("unknown job with an unreadable history: found=%v err=%v", found, err) + } +} + func TestBuildJobDetailKeepsFailedJobResult(t *testing.T) { t.Parallel()