Skip to content

Commit 3ebf7a3

Browse files
committed
feat: 实现任务的休眠逻辑
1 parent 590879e commit 3ebf7a3

20 files changed

Lines changed: 780 additions & 218 deletions

File tree

backend/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ image:
1919
--output ${OUTPUT} \
2020
.
2121

22+
# make ingress PLATFORM= TAG= OUTPUT_INGRESS=
23+
ingress:
24+
docker buildx build \
25+
-f build/Dockerfile.ingress \
26+
--platform ${PLATFORM} \
27+
--tag ${REGISTRY}/monkeycode-ai-ingress:${TAG} \
28+
--output ${OUTPUT} \
29+
.
30+
2231
swag:
2332
swag fmt && swag init -ot json --pd -g cmd/server/main.go
2433

@@ -32,4 +41,4 @@ check-generate:
3241
@echo "Generated code is up to date."
3342

3443
migrate_sql:
35-
migrate create -ext sql -dir migration -seq ${SEQ}
44+
migrate create -ext sql -dir migration -seq ${SEQ}

backend/biz/host/handler/v1/host.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ func (h *HostHandler) ConnectVMTerminal(c *web.Context, req domain.TerminalReq)
371371
ctx, cancel := context.WithCancel(c.Request().Context())
372372
defer cancel()
373373

374-
var vmInfo *domain.VirtualMachine
375374
if err := h.usecase.WithVMPermission(ctx, user.ID, req.ID, func(v *domain.VirtualMachine) error {
376-
vmInfo = v
377375
return nil
378376
}); err != nil {
379377
logger.With("error", err).ErrorContext(ctx, "failed to check permission")
@@ -404,20 +402,6 @@ func (h *HostHandler) ConnectVMTerminal(c *web.Context, req domain.TerminalReq)
404402
}
405403
defer shell.Stop()
406404

407-
// 刷新空闲计时器
408-
if vmInfo != nil {
409-
hostID := ""
410-
if vmInfo.Host != nil {
411-
hostID = vmInfo.Host.ID
412-
}
413-
_ = h.usecase.RefreshIdleTimers(ctx, vmInfo.ID, &domain.VmIdleInfo{
414-
UID: user.ID,
415-
VmID: vmInfo.ID,
416-
HostID: hostID,
417-
EnvID: vmInfo.EnvironmentID,
418-
})
419-
}
420-
421405
go func() {
422406
defer cancel()
423407
for {
@@ -577,17 +561,6 @@ func (h *HostHandler) ShareTerminal(c *web.Context, req domain.ShareTerminalReq)
577561
if err != nil {
578562
return err
579563
}
580-
// 刷新空闲计时器
581-
hostID := ""
582-
if v.Host != nil {
583-
hostID = v.Host.ID
584-
}
585-
_ = h.usecase.RefreshIdleTimers(c.Request().Context(), v.ID, &domain.VmIdleInfo{
586-
UID: user.ID,
587-
VmID: v.ID,
588-
HostID: hostID,
589-
EnvID: v.EnvironmentID,
590-
})
591564
return c.Success(resp)
592565
})
593566
}
@@ -752,21 +725,6 @@ func (h *HostHandler) ApplyPort(c *web.Context, req domain.ApplyPortReq) error {
752725
h.logger.With("error", err).ErrorContext(c.Request().Context(), "failed to apply port")
753726
return errcode.ErrApplyPortFailed.Wrap(err)
754727
}
755-
756-
// 刷新空闲计时器
757-
_ = h.usecase.WithVMPermission(c.Request().Context(), user.ID, req.ID, func(v *domain.VirtualMachine) error {
758-
hostID := ""
759-
if v.Host != nil {
760-
hostID = v.Host.ID
761-
}
762-
return h.usecase.RefreshIdleTimers(c.Request().Context(), v.ID, &domain.VmIdleInfo{
763-
UID: user.ID,
764-
VmID: v.ID,
765-
HostID: hostID,
766-
EnvID: v.EnvironmentID,
767-
})
768-
})
769-
770728
return c.Success(port)
771729
}
772730

backend/biz/host/handler/v1/internal.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ return nil
230230

231231
// 通过 hook 获取关联的 TaskID(内部项目注入时生效)
232232
taskID := uuid.Nil
233-
if h.hook != nil {
234-
taskID = h.hook.OnAgentAuth(ctx, vm.ID)
233+
if len(vm.Edges.Tasks) > 0 {
234+
taskID = vm.Edges.Tasks[0].ID
235235
}
236236

237237
return &taskflow.Token{
@@ -347,6 +347,15 @@ func (h *InternalHostHandler) VmReady(c *web.Context, req taskflow.VirtualMachin
347347
h.logger.With("task", t, "error", err).ErrorContext(c.Request().Context(), "failed to transition task to processing")
348348
}
349349
}
350+
351+
go func() {
352+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
353+
defer cancel()
354+
if err := h.hostUsecase.RefreshIdleTimers(ctx, vm.ID); err != nil {
355+
h.logger.With("error", err).ErrorContext(ctx, "failed to refresh idel timers")
356+
}
357+
}()
358+
350359
}
351360

352361
return c.Success(nil)
@@ -421,17 +430,12 @@ type VMActivityReq struct {
421430

422431
// VMActivity VM 活动回调,用于刷新空闲计时器
423432
func (h *InternalHostHandler) VMActivity(c *web.Context, req VMActivityReq) error {
424-
vm, err := h.repo.GetVirtualMachine(c.Request().Context(), req.VMID)
425-
if err != nil {
426-
h.logger.ErrorContext(c.Request().Context(), "vm activity: vm not found", "vmID", req.VMID, "error", err)
427-
return err
428-
}
429-
430-
payload := &domain.VmIdleInfo{
431-
UID: vm.UserID,
432-
VmID: vm.ID,
433-
HostID: vm.HostID,
434-
EnvID: vm.EnvironmentID,
435-
}
436-
return h.hostUsecase.RefreshIdleTimers(c.Request().Context(), req.VMID, payload)
433+
go func() {
434+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
435+
defer cancel()
436+
if err := h.hostUsecase.RefreshIdleTimers(ctx, req.VMID); err != nil {
437+
h.logger.With("error", err).ErrorContext(ctx, "failed to refresh idel timers")
438+
}
439+
}()
440+
return c.Success(nil)
437441
}

backend/biz/host/repo/host.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ func (h *HostRepo) GetVirtualMachineWithUser(ctx context.Context, uid uuid.UUID,
215215
ForUpdate().
216216
WithHost().
217217
WithModel().
218+
WithTasks().
218219
WithUser().
219220
Where(virtualmachine.HasHostWith(hostWithUserPredicate(uid))).
220221
Where(virtualmachine.UserID(uid)).
@@ -233,6 +234,7 @@ func (h *HostRepo) GetVirtualMachine(ctx context.Context, id string) (*db.Virtua
233234
ForUpdate().
234235
WithHost().
235236
WithModel().
237+
WithTasks().
236238
WithUser().
237239
Where(virtualmachine.ID(id)).
238240
First(ctx)
@@ -548,6 +550,7 @@ func (h *HostRepo) UpdateVM(ctx context.Context, req domain.UpdateVMReq, fn func
548550
// GetVirtualMachineByEnvID implements domain.HostRepo.
549551
func (h *HostRepo) GetVirtualMachineByEnvID(ctx context.Context, envID string) (*db.VirtualMachine, error) {
550552
return h.db.VirtualMachine.Query().
553+
WithTasks().
551554
Where(virtualmachine.EnvironmentID(envID)).
552555
First(ctx)
553556
}

0 commit comments

Comments
 (0)