@@ -34,8 +34,7 @@ type InternalHostHandler struct {
3434 teamRepo domain.TeamHostRepo
3535 redis * redis.Client
3636 cache * cache.Cache
37- hook domain.InternalHook // 可选,由内部项目通过 WithInternalHook 注入
38- taskLifecycle * lifecycle.Manager [uuid.UUID , consts.TaskStatus , lifecycle.TaskMetadata ]
37+ vmLifecycle * lifecycle.Manager [string , lifecycle.VMState , lifecycle.VMMetadata ]
3938 hostUsecase domain.HostUsecase
4039 taskConns * ws.TaskConn
4140 projectUsecase domain.ProjectUsecase
@@ -51,18 +50,13 @@ func NewInternalHostHandler(i *do.Injector) (*InternalHostHandler, error) {
5150 teamRepo : do.MustInvoke [domain.TeamHostRepo ](i ),
5251 redis : do.MustInvoke [* redis.Client ](i ),
5352 cache : cache .New (15 * time .Minute , 10 * time .Minute ),
54- taskLifecycle : do.MustInvoke [* lifecycle.Manager [uuid. UUID , consts. TaskStatus , lifecycle.TaskMetadata ]](i ),
53+ vmLifecycle : do.MustInvoke [* lifecycle.Manager [string , lifecycle. VMState , lifecycle.VMMetadata ]](i ),
5554 hostUsecase : do.MustInvoke [domain.HostUsecase ](i ),
5655 taskConns : do.MustInvoke [* ws.TaskConn ](i ),
5756 projectUsecase : do.MustInvoke [domain.ProjectUsecase ](i ),
5857 tokenProvider : do.MustInvoke [* gituc.TokenProvider ](i ),
5958 }
6059
61- // 可选注入 InternalHook
62- if hook , err := do.Invoke [domain.InternalHook ](i ); err == nil {
63- h .hook = hook
64- }
65-
6660 g := w .Group ("/internal" )
6761 g .POST ("/check-token" , web .BindHandler (h .CheckToken ))
6862 g .POST ("/host-info" , web .BindHandler (h .ReportHostInfo ))
@@ -350,16 +344,13 @@ func (h *InternalHostHandler) VmReady(c *web.Context, req taskflow.VirtualMachin
350344 continue
351345 }
352346
353- if t .Kind == consts .TaskTypeReview && t .SubType == consts .TaskSubTypePrReview {
354- } else {
355- if err := h .taskLifecycle .Transition (c .Request ().Context (), t .ID , consts .TaskStatusProcessing , lifecycle.TaskMetadata {
356- TaskID : t .ID ,
357- UserID : t .UserID ,
358- }); err != nil {
359- h .logger .With ("task" , t , "error" , err ).ErrorContext (c .Request ().Context (), "failed to transition task to processing" )
360- }
347+ if err := h .vmLifecycle .Transition (c .Request ().Context (), vm .ID , lifecycle .VMStateRunning , lifecycle.VMMetadata {
348+ VMID : vm .ID ,
349+ TaskID : & t .ID ,
350+ UserID : t .UserID ,
351+ }); err != nil {
352+ h .logger .With ("task" , t , "error" , err ).ErrorContext (c .Request ().Context (), "failed to transition vm to running" )
361353 }
362-
363354 }
364355
365356 return c .Success (nil )
@@ -384,15 +375,19 @@ func (h *InternalHostHandler) VmConditions(c *web.Context, req taskflow.VirtualM
384375 return err
385376 }
386377
387- // 条件失败时通过 hook 通知内部项目(任务状态转换等)
388- if h .hook != nil {
378+ for _ , task := range vm .Edges .Tasks {
389379 for _ , cond := range req .Conditions {
390- if cond .Type == string (etypes .ConditionTypeFailed ) {
391- if err := h .hook .OnVmConditionFailed (c .Request ().Context (), vm .ID ); err != nil {
392- h .logger .With ("error" , err ).ErrorContext (c .Request ().Context (), "hook OnVmConditionFailed failed" )
393- }
394- break
380+ if cond .Type != string (etypes .ConditionTypeFailed ) {
381+ continue
382+ }
383+ if err := h .vmLifecycle .Transition (c .Request ().Context (), vm .ID , lifecycle .VMStateFailed , lifecycle.VMMetadata {
384+ VMID : vm .ID ,
385+ TaskID : & task .ID ,
386+ UserID : task .UserID ,
387+ }); err != nil {
388+ h .logger .With ("task" , task , "error" , err ).ErrorContext (c .Request ().Context (), "failed to transition vm to failed" )
395389 }
390+ break
396391 }
397392 }
398393
0 commit comments