1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
package network
import (
"net/http"
"strconv"
"time"
"github.com/sirupsen/logrus"
)
const (
updateIntervalHeader = "X-GitLab-Trace-Update-Interval"
remoteStateHeader = "Job-Status"
statusCanceling = "canceling"
statusCanceled = "canceled"
statusFailed = "failed"
)
type RemoteJobStateResponse struct {
StatusCode int
RemoteState string
RemoteUpdateInterval time.Duration
}
func (r *RemoteJobStateResponse) IsFailed() bool {
if r.RemoteState == statusCanceled || r.RemoteState == statusFailed {
return true
}
if r.StatusCode == http.StatusForbidden {
return true
}
return false
}
func (r *RemoteJobStateResponse) IsCanceled() bool {
return r.RemoteState == statusCanceling
}
func NewRemoteJobStateResponse(response *http.Response, logger logrus.FieldLogger) *RemoteJobStateResponse {
if response == nil {
return &RemoteJobStateResponse{}
}
result := &RemoteJobStateResponse{
StatusCode: response.StatusCode,
RemoteState: response.Header.Get(remoteStateHeader),
}
if updateIntervalRaw := response.Header.Get(updateIntervalHeader); updateIntervalRaw != "" {
if updateInterval, err := strconv.Atoi(updateIntervalRaw); err == nil {
result.RemoteUpdateInterval = time.Duration(updateInterval) * time.Second
} else {
logger.WithError(err).
WithField("header-value", updateIntervalRaw).
Warningf("Failed to parse %q header", updateIntervalHeader)
}
}
return result
}
|