DeleteInactionableJobs will delete jobs that are not in queued state and have no runner associated with them. This can happen if we have a pool that matches labels defined on a job, but the job itself was picked up by a runner we don't manage. When a job transitions from queued to anything else, GAR
(_ context.Context, olderThan time.Duration)
| 495 | // purposes. So they are safe to delete. |
| 496 | // Also deletes completed jobs with GARM runners attached as they are no longer needed. |
| 497 | func (s *sqlDatabase) DeleteInactionableJobs(_ context.Context, olderThan time.Duration) error { |
| 498 | // Fetch and delete within a transaction to avoid races. |
| 499 | var jobs []WorkflowJob |
| 500 | |
| 501 | err := s.conn.Transaction(func(tx *gorm.DB) error { |
| 502 | q := tx. |
| 503 | Model(&WorkflowJob{}). |
| 504 | Preload("Instance"). |
| 505 | Where("(status != ? AND instance_id IS NULL) OR (status = ? AND instance_id IS NOT NULL)", params.JobStatusQueued, params.JobStatusCompleted) |
| 506 | if olderThan > 0 { |
| 507 | q = q.Where("created_at < ?", time.Now().Add(-olderThan)) |
| 508 | } |
| 509 | if err := q.Find(&jobs).Error; err != nil { |
| 510 | return fmt.Errorf("fetching inactionable jobs: %w", err) |
| 511 | } |
| 512 | |
| 513 | if len(jobs) == 0 { |
| 514 | return nil |
| 515 | } |
| 516 | |
| 517 | ids := make([]int64, len(jobs)) |
| 518 | for i, j := range jobs { |
| 519 | ids[i] = j.ID |
| 520 | } |
| 521 | |
| 522 | if err := tx.Unscoped().Where("id IN ?", ids).Delete(&WorkflowJob{}).Error; err != nil { |
| 523 | return fmt.Errorf("deleting inactionable jobs: %w", err) |
| 524 | } |
| 525 | |
| 526 | return nil |
| 527 | }) |
| 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | |
| 532 | for _, j := range jobs { |
| 533 | asParams, err := sqlWorkflowJobToParamsJob(j) |
| 534 | if err != nil { |
| 535 | slog.With(slog.Any("error", err)).Error("failed to convert job for notify") |
| 536 | continue |
| 537 | } |
| 538 | if notifyErr := s.sendNotify(common.JobEntityType, common.DeleteOperation, asParams); notifyErr != nil { |
| 539 | slog.With(slog.Any("error", notifyErr)).Error("failed to send delete notify for job") |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return nil |
| 544 | } |
nothing calls this directly
no test coverage detected