func (s *Service) registerTask(ctx context.Context, req *rpcscheduler.PeerTaskRequest) (*resource.Task, error) {
	task := resource.NewTask(idgen.TaskID(req.Url, req.UrlMeta), req.Url, s.config.Scheduler.BackSourceCount, req.UrlMeta)
	task, loaded := s.resource.TaskManager().LoadOrStore(task)
	if loaded && (task.FSM.Is(resource.TaskStateRunning) || task.FSM.Is(resource.TaskStateSucceeded)) && task.LenAvailablePeers() != 0 {
		// Task is healthy and can be reused
		task.UpdateAt.Store(time.Now())
		task.Log.Infof("reuse task and status is %s", task.FSM.Current())
		return task, nil
	}

	// Trigger task
	if err := task.FSM.Event(resource.TaskEventDownload); err != nil {
		return nil, err
	}

	// Start seed cdn task
	go func() {
		task.Log.Infof("trigger cdn download task and task status is %s", task.FSM.Current())
		peer, endOfPiece, err := s.resource.CDN().TriggerTask(context.Background(), task)
		if err != nil {
			task.Log.Errorf("trigger cdn download task failed: %v", err)
			s.handleTaskFail(ctx, task)
			return
		}

		// Update the task status first to help peer scheduling evaluation and scoring
		s.handleTaskSuccess(ctx, task, endOfPiece)
		s.handlePeerSuccess(ctx, peer)
	}()

	return task, nil
}
// registerTask creates a new task or reuses a previous task
registerTask creates a new task or reuses a previous task
// registerTask creates a new task or reuses a previous task
func (s *Service) registerTask(ctx context.Context, req *rpcscheduler.PeerTaskRequest) (*resource.Task, error) {
	task := resource.NewTask(idgen.TaskID(req.Url, req.UrlMeta), req.Url, s.config.Scheduler.BackSourceCount, req.UrlMeta)
	task, loaded := s.resource.TaskManager().LoadOrStore(task)
	if loaded && (task.FSM.Is(resource.TaskStateRunning) || task.FSM.Is(resource.TaskStateSucceeded)) && task.LenAvailablePeers() != 0 {
		task.UpdateAt.Store(time.Now())
		task.Log.Infof("reuse task and status is %s", task.FSM.Current())
		return task, nil
	}
	if err := task.FSM.Event(resource.TaskEventDownload); err != nil {
		return nil, err
	}
	go func() {
		task.Log.Infof("trigger cdn download task and task status is %s", task.FSM.Current())
		peer, endOfPiece, err := s.resource.CDN().TriggerTask(context.Background(), task)
		if err != nil {
			task.Log.Errorf("trigger cdn download task failed: %v", err)
			s.handleTaskFail(ctx, task)
			return
		}
		s.handleTaskSuccess(ctx, task, endOfPiece)
		s.handlePeerSuccess(ctx, peer)
	}()
	return task, nil
}
func ( s * Service ) registerTask ( ctx context . Context , req * rpcscheduler . PeerTaskRequest ) ( * resource . Task , error ) { task := resource . NewTask ( idgen . TaskID ( req . Url , req . UrlMeta ) , req . Url , s . config . Scheduler . BackSourceCount , req . UrlMeta ) task , loaded := s . resource . TaskManager ( ) . LoadOrStore ( task ) if loaded && ( task . FSM . Is ( resource . TaskStateRunning ) || task . FSM . Is ( resource . TaskStateSucceeded ) ) && task . LenAvailablePeers ( ) != 0 { task . UpdateAt . Store ( time . Now ( ) ) task . Log . Infof ( " " , task . FSM . Current ( ) ) return task , nil } if err := task . FSM . Event ( resource . TaskEventDownload ) ; err != nil { return nil , err } go func ( ) { task . Log . Infof ( " " , task . FSM . Current ( ) ) peer , endOfPiece , err := s . resource . CDN ( ) . TriggerTask ( context . Background ( ) , task ) if err != nil { task . Log . Errorf ( " " , err ) s . handleTaskFail ( ctx , task ) return } s . handleTaskSuccess ( ctx , task , endOfPiece ) s . handlePeerSuccess ( ctx , peer ) } ( ) return task , nil }
registerTask creates a new task or reuses a previous task
registerTask creates a new task or reuses a previous task
// Task is healthy and can be reused // Trigger task // Start seed cdn task // Update the task status first to help peer scheduling evaluation and scoring