build_jobs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dbstore
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "gorm.io/gorm"
  7. )
  8. // CreateBuildJob creates a new build job using GORM
  9. func (s *SQLiteStore) CreateBuildJob(ctx context.Context, job *models.BuildJob) error {
  10. result := s.db.WithContext(ctx).Create(job)
  11. if result.Error != nil {
  12. return fmt.Errorf("failed to create build job: %w", result.Error)
  13. }
  14. return nil
  15. }
  16. // GetBuildJobByID retrieves a build job by ID using GORM
  17. func (s *SQLiteStore) GetBuildJobByID(ctx context.Context, id uint) (*models.BuildJob, error) {
  18. var job models.BuildJob
  19. result := s.db.WithContext(ctx).First(&job, id)
  20. if result.Error != nil {
  21. if result.Error == gorm.ErrRecordNotFound {
  22. return nil, models.NewErrNotFound(fmt.Sprintf("build job with ID %d not found", id), result.Error)
  23. }
  24. return nil, fmt.Errorf("failed to get build job by ID: %w", result.Error)
  25. }
  26. return &job, nil
  27. }
  28. // UpdateBuildJob updates an existing build job using GORM
  29. func (s *SQLiteStore) UpdateBuildJob(ctx context.Context, job *models.BuildJob) error {
  30. result := s.db.WithContext(ctx).Save(job)
  31. if result.Error != nil {
  32. return fmt.Errorf("failed to update build job: %w", result.Error)
  33. }
  34. if result.RowsAffected == 0 {
  35. return models.NewErrNotFound(fmt.Sprintf("build job with ID %d not found for update", job.ID), nil)
  36. }
  37. return nil
  38. }
  39. // UpdateBuildJobStatus updates the status and error message of a build job using GORM
  40. func (s *SQLiteStore) UpdateBuildJobStatus(ctx context.Context, id uint, status models.BuildStatus, errorMessage string) error {
  41. result := s.db.WithContext(ctx).Model(&models.BuildJob{}).Where("id = ?", id).Updates(map[string]interface{}{
  42. "status": status,
  43. "error_message": errorMessage,
  44. })
  45. if result.Error != nil {
  46. return fmt.Errorf("failed to update build job status: %w", result.Error)
  47. }
  48. if result.RowsAffected == 0 {
  49. return models.NewErrNotFound(fmt.Sprintf("build job with ID %d not found for status update", id), nil)
  50. }
  51. return nil
  52. }
  53. // AppendBuildJobLog appends a log message to a build job using GORM
  54. func (s *SQLiteStore) AppendBuildJobLog(ctx context.Context, id uint, logMessage string) error {
  55. // Get current logs first
  56. var job models.BuildJob
  57. result := s.db.WithContext(ctx).Select("logs").First(&job, id)
  58. if result.Error != nil {
  59. if result.Error == gorm.ErrRecordNotFound {
  60. return models.NewErrNotFound(fmt.Sprintf("build job with ID %d not found", id), result.Error)
  61. }
  62. return fmt.Errorf("failed to get build job for log append: %w", result.Error)
  63. }
  64. // Append new log message
  65. newLogs := job.Logs + logMessage + "\n"
  66. result = s.db.WithContext(ctx).Model(&models.BuildJob{}).Where("id = ?", id).Update("logs", newLogs)
  67. if result.Error != nil {
  68. return fmt.Errorf("failed to append build job log: %w", result.Error)
  69. }
  70. return nil
  71. }
  72. // GetQueuedBuildJobs retrieves queued build jobs using GORM
  73. func (s *SQLiteStore) GetQueuedBuildJobs(ctx context.Context, limit int) ([]*models.BuildJob, error) {
  74. var jobs []*models.BuildJob
  75. result := s.db.WithContext(ctx).Where("status = ?", models.BuildStatusPending).Limit(limit).Find(&jobs)
  76. if result.Error != nil {
  77. return nil, fmt.Errorf("failed to get queued build jobs: %w", result.Error)
  78. }
  79. return jobs, nil
  80. }
  81. // GetBuildJobsByAppID retrieves build jobs for a specific app using GORM (with pagination)
  82. func (s *SQLiteStore) GetBuildJobsByAppID(ctx context.Context, appID uint, page, pageSize int) ([]*models.BuildJob, int64, error) {
  83. var jobs []*models.BuildJob
  84. var total int64
  85. // Count total records
  86. s.db.WithContext(ctx).Model(&models.BuildJob{}).Where("component_id IN (SELECT id FROM components WHERE user_id = (SELECT user_id FROM apps WHERE id = ?))", appID).Count(&total)
  87. // Get paginated results
  88. offset := (page - 1) * pageSize
  89. result := s.db.WithContext(ctx).Where("component_id IN (SELECT id FROM components WHERE user_id = (SELECT user_id FROM apps WHERE id = ?))", appID).
  90. Offset(offset).Limit(pageSize).Find(&jobs)
  91. if result.Error != nil {
  92. return nil, 0, fmt.Errorf("failed to get build jobs by app ID: %w", result.Error)
  93. }
  94. return jobs, total, nil
  95. }