From 36e4f538aeb2c88d0e50dbb35627b7a987a5f997 Mon Sep 17 00:00:00 2001 From: David Colburn Date: Fri, 8 Nov 2024 12:26:46 -0800 Subject: [PATCH] EgressInfo helpers --- livekit/livekit_egress.go | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 livekit/livekit_egress.go diff --git a/livekit/livekit_egress.go b/livekit/livekit_egress.go new file mode 100644 index 00000000..dfbba883 --- /dev/null +++ b/livekit/livekit_egress.go @@ -0,0 +1,85 @@ +package livekit + +import ( + "errors" + "fmt" + "net/http" + "time" + + "github.com/livekit/psrpc" +) + +const ( + MsgBackupUsed = "At least one file went to backup storage" + MsgLimitReached = "Session limit reached" + MsgStartNotReceived = "Start signal not received" + MsgLimitReachedWithoutStart = "Session limit reached before start signal" + MsgStoppedBeforeStarted = "Stop called before pipeline could start" + + EndReasonAPI = "StopEgress API" + EndReasonKilled = "Process killed" + EndReasonSrcClosed = "Source closed" + EndReasonLimitReached = "Session limit reached" + EndReasonStreamsStopped = "All streams stopped" + EndReasonFailure = "Failure" +) + +func (e *EgressInfo) UpdateStatus(status EgressStatus) { + e.Status = status + e.UpdatedAt = time.Now().UnixNano() +} + +func (e *EgressInfo) SetBackupUsed() { + e.Status = EgressStatus_EGRESS_FAILED + e.UpdatedAt = time.Now().UnixNano() + e.Error = MsgBackupUsed + e.ErrorCode = int32(http.StatusPermanentRedirect) +} + +func (e *EgressInfo) SetEndReason(reason string) { + e.Details = fmt.Sprintf("End reason: %s", reason) +} + +func (e *EgressInfo) SetLimitReached() { + now := time.Now().UnixNano() + e.Status = EgressStatus_EGRESS_LIMIT_REACHED + e.Error = MsgLimitReached + e.ErrorCode = int32(http.StatusRequestEntityTooLarge) + e.UpdatedAt = now + e.EndedAt = now +} + +func (e *EgressInfo) SetAborted(msg string) { + now := time.Now().UnixNano() + e.Status = EgressStatus_EGRESS_ABORTED + e.Error = msg + e.ErrorCode = int32(http.StatusPreconditionFailed) + e.UpdatedAt = now + e.EndedAt = now +} + +func (e *EgressInfo) SetFailed(err error) { + now := time.Now().UnixNano() + e.Status = EgressStatus_EGRESS_FAILED + e.UpdatedAt = now + e.EndedAt = now + e.Error = err.Error() + if e.Details == "" { + e.SetEndReason(EndReasonFailure) + } + + var p psrpc.Error + if errors.As(err, &p) { + // unknown is treated the same as an internal error (500) + if !errors.Is(p.Code(), psrpc.Unknown) { + e.ErrorCode = int32(p.ToHttp()) + } + } +} + +func (e *EgressInfo) SetComplete() { + now := time.Now().UnixNano() + e.Status = EgressStatus_EGRESS_COMPLETE + e.UpdatedAt = now + e.EndedAt = now +}