when server restarts, we should unlock all locked jobs, and set their status to failed

make sure we can customize the limit value for Background Job query.

fixes #267
This commit is contained in:
Jason Kulatunga 2023-10-09 17:16:18 -07:00
parent cae3afce72
commit e5f920c2ec
No known key found for this signature in database
2 changed files with 34 additions and 2 deletions

View File

@ -106,6 +106,12 @@ func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger,
return nil, fmt.Errorf("Failed to create admin user! - %v", err) 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 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 // Utilities
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -151,8 +151,21 @@ func ListBackgroundJobs(c *gin.Context) {
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry) logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository) databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
backgroundJobQueryOptions := models.BackgroundJobQueryOptions{ backgroundJobQueryOptions := models.BackgroundJobQueryOptions{}
Limit: pkg.ResourceListPageSize, 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 { if len(c.Query("jobType")) > 0 {
jobType := pkg.BackgroundJobType(c.Query("jobType")) jobType := pkg.BackgroundJobType(c.Query("jobType"))