diff --git a/backend/pkg/database/sqlite_repository.go b/backend/pkg/database/sqlite_repository.go index 00acc271..e0160af8 100644 --- a/backend/pkg/database/sqlite_repository.go +++ b/backend/pkg/database/sqlite_repository.go @@ -106,6 +106,12 @@ func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger, return nil, fmt.Errorf("Failed to create admin user! - %v", err) } + //fail any Locked jobs. This is necessary because the job may have been locked by a process that was killed. + err = fastenRepo.CancelAllLockedBackgroundJobsAndFail() + if err != nil { + return nil, err + } + return &fastenRepo, nil } @@ -1146,6 +1152,19 @@ func (sr *SqliteRepository) BackgroundJobCheckpoint(ctx context.Context, checkpo } +// when server restarts, we should unlock all locked jobs, and set their status to failed +// SECURITY: this is global, and effects all users. +func (sr *SqliteRepository) CancelAllLockedBackgroundJobsAndFail() error { + now := time.Now() + return sr.GormClient. + Where(models.BackgroundJob{JobStatus: pkg.BackgroundJobStatusLocked}). + Updates(models.BackgroundJob{ + JobStatus: pkg.BackgroundJobStatusFailed, + DoneTime: &now, + }).Error + +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Utilities //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/backend/pkg/web/handler/background_jobs.go b/backend/pkg/web/handler/background_jobs.go index cc0c2db2..cd3d2568 100644 --- a/backend/pkg/web/handler/background_jobs.go +++ b/backend/pkg/web/handler/background_jobs.go @@ -151,8 +151,21 @@ func ListBackgroundJobs(c *gin.Context) { logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry) databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository) - backgroundJobQueryOptions := models.BackgroundJobQueryOptions{ - Limit: pkg.ResourceListPageSize, + backgroundJobQueryOptions := models.BackgroundJobQueryOptions{} + if len(c.Query("limit")) == 0 { + backgroundJobQueryOptions.Limit = pkg.ResourceListPageSize + } else { + limit, err := strconv.Atoi(c.Query("limit")) + if err != nil { + logger.Errorln("An error occurred while calculating limit", err) + c.JSON(http.StatusInternalServerError, gin.H{"success": false}) + return + } + if limit == 0 { + backgroundJobQueryOptions.Limit = pkg.ResourceListPageSize + } else { + backgroundJobQueryOptions.Limit = limit + } } if len(c.Query("jobType")) > 0 { jobType := pkg.BackgroundJobType(c.Query("jobType"))