update cicd
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
proto "stream.api/internal/api/proto/agent/v1"
|
||||
"stream.api/internal/dto"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterAgent(ctx context.Context, req *proto.RegisterAgentRequest) (*proto.RegisterAgentResponse, error) {
|
||||
@@ -39,7 +38,7 @@ func (s *Server) UnregisterAgent(ctx context.Context, _ *proto.Empty) (*proto.Em
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid session")
|
||||
}
|
||||
for _, jobID := range s.getAgentJobs(agentID) {
|
||||
_ = s.jobService.UpdateJobStatus(ctx, jobID, dto.JobStatusFailure)
|
||||
_ = s.jobService.HandleAgentDisconnect(ctx, jobID)
|
||||
s.untrackJobAssignment(agentID, jobID)
|
||||
}
|
||||
s.sessions.Delete(token)
|
||||
|
||||
@@ -41,7 +41,7 @@ func (s *Server) getAgentIDFromContext(ctx context.Context) (string, string, boo
|
||||
|
||||
func (s *Server) Auth(ctx context.Context, req *proto.AuthRequest) (*proto.AuthResponse, error) {
|
||||
if s.agentSecret != "" && req.AgentToken != s.agentSecret {
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid agent secret")
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid internal marker")
|
||||
}
|
||||
agentID := req.AgentId
|
||||
if len(agentID) > 6 && agentID[:6] == "agent-" {
|
||||
|
||||
@@ -3,6 +3,7 @@ package grpc
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
grpcpkg "google.golang.org/grpc"
|
||||
"gorm.io/gorm"
|
||||
@@ -21,11 +22,14 @@ type GRPCModule struct {
|
||||
mqttPublisher *mqtt.MQTTBootstrap
|
||||
grpcServer *grpcpkg.Server
|
||||
cfg *config.Config
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewGRPCModule(ctx context.Context, cfg *config.Config, db *gorm.DB, rds *redisadapter.RedisAdapter, appLogger logger.Logger) (*GRPCModule, error) {
|
||||
jobService := service.NewJobService(db, rds, rds)
|
||||
agentRuntime := NewServer(jobService, cfg.Render.AgentSecret)
|
||||
moduleCtx, cancel := context.WithCancel(ctx)
|
||||
jobService := service.NewJobService(db, rds, rds, redisadapter.NewDeadLetterQueue(rds.Client()))
|
||||
jobService.SetLogger(appLogger)
|
||||
agentRuntime := NewServer(jobService, cfg.Internal.Marker)
|
||||
videoService := renderworkflow.New(db, jobService)
|
||||
grpcServer := grpcpkg.NewServer()
|
||||
|
||||
@@ -34,6 +38,7 @@ func NewGRPCModule(ctx context.Context, cfg *config.Config, db *gorm.DB, rds *re
|
||||
agentRuntime: agentRuntime,
|
||||
grpcServer: grpcServer,
|
||||
cfg: cfg,
|
||||
cancel: cancel,
|
||||
}
|
||||
|
||||
var notificationPublisher service.NotificationEventPublisher = nil
|
||||
@@ -50,8 +55,9 @@ func NewGRPCModule(ctx context.Context, cfg *config.Config, db *gorm.DB, rds *re
|
||||
agentRuntime.Register(grpcServer)
|
||||
service.Register(grpcServer, service.NewServices(rds, db, appLogger, cfg, videoService, agentRuntime, notificationPublisher))
|
||||
if module.mqttPublisher != nil {
|
||||
module.mqttPublisher.Start(ctx)
|
||||
module.mqttPublisher.Start(moduleCtx)
|
||||
}
|
||||
go jobService.StartInflightReclaimLoop(moduleCtx, 30*time.Second, 100)
|
||||
|
||||
return module, nil
|
||||
}
|
||||
@@ -62,6 +68,9 @@ func (m *GRPCModule) GRPCServer() *grpcpkg.Server { return m.grpcServe
|
||||
func (m *GRPCModule) GRPCAddress() string { return ":" + m.cfg.Server.GRPCPort }
|
||||
func (m *GRPCModule) ServeGRPC(listener net.Listener) error { return m.grpcServer.Serve(listener) }
|
||||
func (m *GRPCModule) Shutdown() {
|
||||
if m.cancel != nil {
|
||||
m.cancel()
|
||||
}
|
||||
if m.grpcServer != nil {
|
||||
m.grpcServer.GracefulStop()
|
||||
}
|
||||
|
||||
@@ -53,12 +53,14 @@ func (s *Server) StreamJobs(_ *proto.StreamOptions, stream grpcpkg.ServerStreami
|
||||
}
|
||||
s.trackJobAssignment(agentID, job.ID)
|
||||
if err := s.jobService.AssignJob(ctx, job.ID, agentID); err != nil {
|
||||
_ = s.jobService.HandleDispatchFailure(ctx, job.ID, "assign_failed", true)
|
||||
s.untrackJobAssignment(agentID, job.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
var config map[string]any
|
||||
if err := json.Unmarshal([]byte(*job.Config), &config); err != nil {
|
||||
_ = s.jobService.UpdateJobStatus(ctx, job.ID, dto.JobStatusFailure)
|
||||
if job.Config == nil || json.Unmarshal([]byte(*job.Config), &config) != nil {
|
||||
_ = s.jobService.HandleDispatchFailure(ctx, job.ID, "invalid_config", false)
|
||||
s.untrackJobAssignment(agentID, job.ID)
|
||||
continue
|
||||
}
|
||||
@@ -80,7 +82,7 @@ func (s *Server) StreamJobs(_ *proto.StreamOptions, stream grpcpkg.ServerStreami
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]any{"image": image, "commands": commands, "environment": map[string]string{}})
|
||||
if err := stream.Send(&proto.Workflow{Id: job.ID, Timeout: 60 * 60 * 1000, Payload: payload}); err != nil {
|
||||
_ = s.jobService.UpdateJobStatus(ctx, job.ID, dto.JobStatusPending)
|
||||
_ = s.jobService.HandleDispatchFailure(ctx, job.ID, "stream_send_failed", true)
|
||||
s.untrackJobAssignment(agentID, job.ID)
|
||||
return err
|
||||
}
|
||||
@@ -101,8 +103,10 @@ func (s *Server) SubmitStatus(stream grpcpkg.ClientStreamingServer[proto.StatusU
|
||||
}
|
||||
switch update.Type {
|
||||
case 0, 1:
|
||||
_ = s.jobService.RenewJobLease(ctx, update.StepUuid)
|
||||
_ = s.jobService.ProcessLog(ctx, update.StepUuid, update.Data)
|
||||
case 4:
|
||||
_ = s.jobService.RenewJobLease(ctx, update.StepUuid)
|
||||
var progress float64
|
||||
fmt.Sscanf(string(update.Data), "%f", &progress)
|
||||
_ = s.jobService.UpdateJobProgress(ctx, update.StepUuid, progress)
|
||||
@@ -126,6 +130,7 @@ func (s *Server) Init(ctx context.Context, req *proto.InitRequest) (*proto.Empty
|
||||
if err := s.jobService.UpdateJobStatus(ctx, req.Id, dto.JobStatusRunning); err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to update job status")
|
||||
}
|
||||
_ = s.jobService.RenewJobLease(ctx, req.Id)
|
||||
return &proto.Empty{}, nil
|
||||
}
|
||||
|
||||
@@ -138,11 +143,13 @@ func (s *Server) Done(ctx context.Context, req *proto.DoneRequest) (*proto.Empty
|
||||
if !ok {
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid session")
|
||||
}
|
||||
jobStatus := dto.JobStatusSuccess
|
||||
var err error
|
||||
if req.State != nil && req.State.Error != "" {
|
||||
jobStatus = dto.JobStatusFailure
|
||||
err = s.jobService.HandleJobFailure(ctx, req.Id, req.State.Error)
|
||||
} else {
|
||||
err = s.jobService.UpdateJobStatus(ctx, req.Id, dto.JobStatusSuccess)
|
||||
}
|
||||
if err := s.jobService.UpdateJobStatus(ctx, req.Id, jobStatus); err != nil {
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to update job status")
|
||||
}
|
||||
s.untrackJobAssignment(agentID, req.Id)
|
||||
@@ -165,6 +172,12 @@ func (s *Server) Log(ctx context.Context, req *proto.LogRequest) (*proto.Empty,
|
||||
return &proto.Empty{}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Extend(context.Context, *proto.ExtendRequest) (*proto.Empty, error) {
|
||||
func (s *Server) Extend(ctx context.Context, req *proto.ExtendRequest) (*proto.Empty, error) {
|
||||
if _, _, ok := s.getAgentIDFromContext(ctx); !ok {
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid session")
|
||||
}
|
||||
if req != nil && req.Id != "" {
|
||||
_ = s.jobService.RenewJobLease(ctx, req.Id)
|
||||
}
|
||||
return &proto.Empty{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user