Skip to content

Commit

Permalink
when server restarts, we should unlock all locked jobs, and set their…
Browse files Browse the repository at this point in the history
… status to failed

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

fixes #267
  • Loading branch information
AnalogJ committed Oct 10, 2023
1 parent cae3afc commit e5f920c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 19 additions & 0 deletions backend/pkg/database/sqlite_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
17 changes: 15 additions & 2 deletions backend/pkg/web/handler/background_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit e5f920c

Please sign in to comment.