Skip to content

Commit

Permalink
Merge branch 'main' into eng-291-improve-startup-time-by-optimizing-i…
Browse files Browse the repository at this point in the history
…mds-access
  • Loading branch information
wdbaruni authored Oct 24, 2024
2 parents 370bc66 + fb0232a commit 413fa8d
Show file tree
Hide file tree
Showing 8 changed files with 849 additions and 120 deletions.
28 changes: 14 additions & 14 deletions pkg/orchestrator/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,18 @@ func (e *BaseEndpoint) SubmitJob(ctx context.Context, request *SubmitJobRequest)
}

func (e *BaseEndpoint) StopJob(ctx context.Context, request *StopJobRequest) (StopJobResponse, error) {
job, err := e.store.GetJob(ctx, request.JobID)
txContext, err := e.store.BeginTx(ctx)
if err != nil {
return StopJobResponse{}, jobstore.NewJobStoreError(err.Error())
}

defer func() {
if err != nil {
_ = txContext.Rollback()
}
}()

job, err := e.store.GetJob(txContext, request.JobID)
if err != nil {
return StopJobResponse{}, err
}
Expand All @@ -124,21 +135,10 @@ func (e *BaseEndpoint) StopJob(ctx context.Context, request *StopJobRequest) (St
// continue
}

txContext, err := e.store.BeginTx(ctx)
if err != nil {
return StopJobResponse{}, jobstore.NewJobStoreError(err.Error())
}

defer func() {
if err != nil {
_ = txContext.Rollback()
}
}()

// update the job state, except if the job is already completed
// we allow marking a failed job as canceled
if err = e.store.UpdateJobState(txContext, jobstore.UpdateJobStateRequest{
JobID: request.JobID,
JobID: job.ID, // use the job ID from the store in case the request had a short ID
Condition: jobstore.UpdateJobCondition{
UnexpectedStates: []models.JobStateType{
models.JobStateTypeCompleted,
Expand All @@ -161,7 +161,7 @@ func (e *BaseEndpoint) StopJob(ctx context.Context, request *StopJobRequest) (St
now := time.Now().UTC().UnixNano()
eval := &models.Evaluation{
ID: uuid.NewString(),
JobID: request.JobID,
JobID: job.ID,
TriggeredBy: models.EvalTriggerJobCancel,
Type: job.Type,
Status: models.EvalStatusPending,
Expand Down
17 changes: 16 additions & 1 deletion pkg/publicapi/apimodels/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ type APIError struct {

// Component is the component that caused the error.
Component string `json:"Component"`

// Hint is a string providing additional context or suggestions related to the error.
Hint string `json:"Hint,omitempty"`

// Details is a map of string key-value pairs providing additional error context.
Details map[string]string `json:"Details,omitempty"`
}

// NewAPIError creates a new APIError with the given HTTP status code and message.
func NewAPIError(statusCode int, message string) *APIError {
return &APIError{
HTTPStatusCode: statusCode,
Message: message,
Details: make(map[string]string),
}
}

Expand Down Expand Up @@ -106,14 +113,22 @@ func FromBacError(err bacerrors.Error) *APIError {
Message: err.Error(),
Code: string(err.Code()),
Component: err.Component(),
Hint: err.Hint(),
Details: err.Details(),
}
}

// ToBacError converts an APIError to a bacerror.Error
func (e *APIError) ToBacError() bacerrors.Error {
details := e.Details
if details == nil {
details = make(map[string]string)
}
details["request_id"] = e.RequestID
return bacerrors.New(e.Error()).
WithHTTPStatusCode(e.HTTPStatusCode).
WithCode(bacerrors.Code(e.Code)).
WithComponent(e.Component).
WithDetails(map[string]string{"request_id": e.RequestID})
WithHint(e.Hint).
WithDetails(details)
}
Loading

0 comments on commit 413fa8d

Please sign in to comment.