mirror of https://github.com/harness/drone.git
feat: [CODE-888]: minor export improvements
parent
1b79309f4a
commit
c7971bbbb1
|
@ -30,6 +30,7 @@ var (
|
||||||
ErrNotFound = fmt.Errorf("not found")
|
ErrNotFound = fmt.Errorf("not found")
|
||||||
ErrBadRequest = fmt.Errorf("bad request")
|
ErrBadRequest = fmt.Errorf("bad request")
|
||||||
ErrInternal = fmt.Errorf("internal error")
|
ErrInternal = fmt.Errorf("internal error")
|
||||||
|
ErrDuplicate = fmt.Errorf("resource already exists")
|
||||||
)
|
)
|
||||||
|
|
||||||
type HarnessCodeClient struct {
|
type HarnessCodeClient struct {
|
||||||
|
@ -203,6 +204,8 @@ func mapStatusCodeToError(statusCode int) error {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
case statusCode == 400:
|
case statusCode == 400:
|
||||||
return ErrBadRequest
|
return ErrBadRequest
|
||||||
|
case statusCode == 409:
|
||||||
|
return ErrDuplicate
|
||||||
case statusCode >= 400:
|
case statusCode >= 400:
|
||||||
return fmt.Errorf("received client side error status code %d", statusCode)
|
return fmt.Errorf("received client side error status code %d", statusCode)
|
||||||
case statusCode >= 300:
|
case statusCode >= 300:
|
||||||
|
|
|
@ -8,12 +8,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/harness/gitness/encrypt"
|
"github.com/harness/gitness/encrypt"
|
||||||
"github.com/harness/gitness/internal/api/controller/repo"
|
"github.com/harness/gitness/internal/api/controller/repo"
|
||||||
"github.com/harness/gitness/internal/sse"
|
"github.com/harness/gitness/internal/sse"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -56,9 +58,10 @@ const (
|
||||||
exportJobMaxDuration = 45 * time.Minute
|
exportJobMaxDuration = 45 * time.Minute
|
||||||
exportRepoJobUid = "export_repo_%d"
|
exportRepoJobUid = "export_repo_%d"
|
||||||
exportSpaceJobUid = "export_space_%d"
|
exportSpaceJobUid = "export_space_%d"
|
||||||
|
jobType = "repository_export"
|
||||||
)
|
)
|
||||||
|
|
||||||
const jobType = "repository_export"
|
var ErrJobRunning = errors.New("an export job is already running")
|
||||||
|
|
||||||
func (r *Repository) Register(executor *job.Executor) error {
|
func (r *Repository) Register(executor *job.Executor) error {
|
||||||
return executor.Register(jobType, r)
|
return executor.Register(jobType, r)
|
||||||
|
@ -66,6 +69,23 @@ func (r *Repository) Register(executor *job.Executor) error {
|
||||||
|
|
||||||
func (r *Repository) RunMany(ctx context.Context, spaceId int64, harnessCodeInfo *HarnessCodeInfo, repos []*types.Repository) error {
|
func (r *Repository) RunMany(ctx context.Context, spaceId int64, harnessCodeInfo *HarnessCodeInfo, repos []*types.Repository) error {
|
||||||
jobGroupId := getJobGroupId(spaceId)
|
jobGroupId := getJobGroupId(spaceId)
|
||||||
|
|
||||||
|
jobs, err := r.scheduler.GetJobProgressForGroup(ctx, jobGroupId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot get job progress before starting. %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = checkJobAlreadyRunning(jobs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := r.scheduler.PurgeJobsByGroupId(ctx, jobGroupId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Ctx(ctx).Info().Msgf("deleted %d old jobs", n)
|
||||||
|
|
||||||
jobDefinitions := make([]job.Definition, len(repos))
|
jobDefinitions := make([]job.Definition, len(repos))
|
||||||
for i, repository := range repos {
|
for i, repository := range repos {
|
||||||
repoJobData := Input{
|
repoJobData := Input{
|
||||||
|
@ -100,6 +120,17 @@ func (r *Repository) RunMany(ctx context.Context, spaceId int64, harnessCodeInfo
|
||||||
return r.scheduler.RunJobs(ctx, jobGroupId, jobDefinitions)
|
return r.scheduler.RunJobs(ctx, jobGroupId, jobDefinitions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkJobAlreadyRunning(jobs []types.JobProgress) error {
|
||||||
|
if jobs != nil {
|
||||||
|
for _, j := range jobs {
|
||||||
|
if !slices.Contains(enum.GetCompletedJobState(), j.State) {
|
||||||
|
return ErrJobRunning
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getJobGroupId(spaceId int64) string {
|
func getJobGroupId(spaceId int64) string {
|
||||||
return fmt.Sprintf(exportSpaceJobUid, spaceId)
|
return fmt.Sprintf(exportSpaceJobUid, spaceId)
|
||||||
}
|
}
|
||||||
|
@ -156,7 +187,6 @@ func (r *Repository) Handle(ctx context.Context, data string, _ job.ProgressRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msgf("completed repository export for repo", repository.UID)
|
log.Info().Msgf("completed repository export for repo", repository.UID)
|
||||||
|
|
||||||
publishSSE(ctx, r, repository)
|
publishSSE(ctx, r, repository)
|
||||||
|
|
||||||
return "", nil
|
return "", nil
|
||||||
|
|
|
@ -624,6 +624,14 @@ func (s *Scheduler) GetJobProgressForGroup(ctx context.Context, jobGroupUID stri
|
||||||
return mapToProgressMany(job), nil
|
return mapToProgressMany(job), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) PurgeJobsByGroupId(ctx context.Context, jobGroupUID string) (int64, error) {
|
||||||
|
n, err := s.store.DeleteByGroupID(ctx, jobGroupUID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
func mapToProgressMany(jobs []*types.Job) []types.JobProgress {
|
func mapToProgressMany(jobs []*types.Job) []types.JobProgress {
|
||||||
if jobs == nil {
|
if jobs == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -448,6 +448,9 @@ type (
|
||||||
// ListByGroupID fetches all jobs for a group id
|
// ListByGroupID fetches all jobs for a group id
|
||||||
ListByGroupID(ctx context.Context, groupId string) ([]*types.Job, error)
|
ListByGroupID(ctx context.Context, groupId string) ([]*types.Job, error)
|
||||||
|
|
||||||
|
// DeleteByGroupID deletes all jobs for a group id
|
||||||
|
DeleteByGroupID(ctx context.Context, groupId string) (int64, error)
|
||||||
|
|
||||||
// Create is used to create a new job.
|
// Create is used to create a new job.
|
||||||
Create(ctx context.Context, job *types.Job) error
|
Create(ctx context.Context, job *types.Job) error
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,32 @@ func (s *JobStore) ListByGroupID(ctx context.Context, groupId string) ([]*types.
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteByGroupID deletes all jobs for a group id
|
||||||
|
func (s *JobStore) DeleteByGroupID(ctx context.Context, groupId string) (int64, error) {
|
||||||
|
stmt := database.Builder.
|
||||||
|
Delete("jobs").
|
||||||
|
Where("(job_group_id = ?)", groupId)
|
||||||
|
|
||||||
|
sql, args, err := stmt.ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to convert delete by group id jobs query to sql: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db := dbtx.GetAccessor(ctx, s.db)
|
||||||
|
|
||||||
|
result, err := db.ExecContext(ctx, sql, args...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, database.ProcessSQLErrorf(err, "failed to execute delete jobs by group id query")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return 0, database.ProcessSQLErrorf(err, "failed to get number of deleted jobs in group")
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Create creates a new job.
|
// Create creates a new job.
|
||||||
func (s *JobStore) Create(ctx context.Context, job *types.Job) error {
|
func (s *JobStore) Create(ctx context.Context, job *types.Job) error {
|
||||||
const sqlQuery = `
|
const sqlQuery = `
|
||||||
|
|
|
@ -24,3 +24,7 @@ const (
|
||||||
JobPriorityNormal JobPriority = 0
|
JobPriorityNormal JobPriority = 0
|
||||||
JobPriorityElevated JobPriority = 1
|
JobPriorityElevated JobPriority = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetCompletedJobState() []JobState {
|
||||||
|
return []JobState{JobStateFinished, JobStateCanceled, JobStateFinished}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue