Partial support for agent dispatch management (#2872)
- Store agent dispaches independently of room agents on rtc.Room - Serialize agent dispatches in rtc.Room - Support for agent dispatch and job serialization in redis The agent Job object references denormalized Room and ParticipantInfo object. When storing Jobs, this sets the Room to nil, and only stores the Participant identity field. When read back, these fields need to be set to their current value.
This commit is contained in:
@@ -30,6 +30,7 @@ import (
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/guid"
|
||||
"github.com/livekit/psrpc"
|
||||
|
||||
"github.com/livekit/livekit-server/version"
|
||||
)
|
||||
@@ -58,6 +59,10 @@ const (
|
||||
// RoomLockPrefix is a simple key containing a provided lock uid
|
||||
RoomLockPrefix = "room_lock:"
|
||||
|
||||
// Agents
|
||||
AgentDispatchPrefix = "agent_dispatch:"
|
||||
AgentJobPrefix = "agent_job:"
|
||||
|
||||
maxRetries = 5
|
||||
)
|
||||
|
||||
@@ -231,6 +236,8 @@ func (s *RedisStore) DeleteRoom(ctx context.Context, roomName livekit.RoomName)
|
||||
pp.HDel(s.ctx, RoomsKey, string(roomName))
|
||||
pp.HDel(s.ctx, RoomInternalKey, string(roomName))
|
||||
pp.Del(s.ctx, RoomParticipantsPrefix+string(roomName))
|
||||
pp.Del(s.ctx, AgentDispatchPrefix+string(roomName))
|
||||
pp.Del(s.ctx, AgentJobPrefix+string(roomName))
|
||||
|
||||
_, err = pp.Exec(s.ctx)
|
||||
return err
|
||||
@@ -822,6 +829,99 @@ func (s *RedisStore) DeleteIngress(_ context.Context, info *livekit.IngressInfo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RedisStore) StoreAgentDispatch(_ context.Context, dispatch *livekit.AgentDispatch) error {
|
||||
di := proto.Clone(dispatch).(*livekit.AgentDispatch)
|
||||
|
||||
// Do not store jobs with the dispatch
|
||||
if di.State != nil {
|
||||
di.State.Jobs = nil
|
||||
}
|
||||
|
||||
key := AgentDispatchPrefix + string(dispatch.Room)
|
||||
|
||||
data, err := proto.Marshal(di)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.rc.HSet(s.ctx, key, di.Id, data).Err()
|
||||
}
|
||||
|
||||
// This will not delete the jobs created by the dispatch
|
||||
func (s *RedisStore) DeleteAgentDispatch(_ context.Context, dispatch *livekit.AgentDispatch) error {
|
||||
key := AgentDispatchPrefix + string(dispatch.Room)
|
||||
return s.rc.HDel(s.ctx, key, dispatch.Id).Err()
|
||||
}
|
||||
|
||||
func (s *RedisStore) ListAgentDispatches(_ context.Context, roomName livekit.RoomName) ([]*livekit.AgentDispatch, error) {
|
||||
key := AgentDispatchPrefix + string(roomName)
|
||||
dispatches, err := redisLoadMany[livekit.AgentDispatch](s.ctx, s, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dMap := make(map[string]*livekit.AgentDispatch)
|
||||
for _, di := range dispatches {
|
||||
dMap[di.Id] = di
|
||||
}
|
||||
|
||||
key = AgentJobPrefix + string(roomName)
|
||||
jobs, err := redisLoadMany[livekit.Job](s.ctx, s, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Associate job to dispatch
|
||||
for _, jb := range jobs {
|
||||
di := dMap[jb.DispatchId]
|
||||
if di == nil {
|
||||
continue
|
||||
}
|
||||
if di.State == nil {
|
||||
di.State = &livekit.AgentDispatchState{}
|
||||
}
|
||||
di.State.Jobs = append(di.State.Jobs, jb)
|
||||
}
|
||||
|
||||
return dispatches, nil
|
||||
}
|
||||
|
||||
func (s *RedisStore) StoreAgentJob(_ context.Context, job *livekit.Job) error {
|
||||
if job.Room == nil {
|
||||
return psrpc.NewErrorf(psrpc.InvalidArgument, "job doesn't have a valid Room field")
|
||||
}
|
||||
|
||||
key := AgentJobPrefix + string(job.Room.Name)
|
||||
|
||||
jb := proto.Clone(job).(*livekit.Job)
|
||||
|
||||
// Do not store room with the job
|
||||
jb.Room = nil
|
||||
|
||||
// Only store the participant identity
|
||||
if jb.Participant != nil {
|
||||
jb.Participant = &livekit.ParticipantInfo{
|
||||
Identity: jb.Participant.Identity,
|
||||
}
|
||||
}
|
||||
|
||||
data, err := proto.Marshal(jb)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.rc.HSet(s.ctx, key, job.Id, data).Err()
|
||||
}
|
||||
|
||||
func (s *RedisStore) DeleteAgentJob(_ context.Context, job *livekit.Job) error {
|
||||
if job.Room == nil {
|
||||
return psrpc.NewErrorf(psrpc.InvalidArgument, "job doesn't have a valid Room field")
|
||||
}
|
||||
|
||||
key := AgentJobPrefix + string(job.Room.Name)
|
||||
return s.rc.HDel(s.ctx, key, job.Id).Err()
|
||||
}
|
||||
|
||||
func redisStoreOne(ctx context.Context, s *RedisStore, key, id string, p proto.Message) error {
|
||||
if id == "" {
|
||||
return errors.New("id is not set")
|
||||
|
||||
Reference in New Issue
Block a user