func (c *cdn) TriggerTask(ctx context.Context, task *Task) (*Peer, *rpcscheduler.PeerResult, error) {
	stream, err := c.client.ObtainSeeds(ctx, &cdnsystem.SeedRequest{
		TaskId:  task.ID,
		Url:     task.URL,
		UrlMeta: task.URLMeta,
	})
	if err != nil {
		return nil, nil, err
	}

	var (
		peer        *Peer
		initialized bool
	)

	for {
		piece, err := stream.Recv()
		if err != nil {
			// If the peer initialization succeeds and the download fails,
			// set peer status is PeerStateFailed.
			if peer != nil {
				if err := peer.FSM.Event(PeerEventDownloadFailed); err != nil {
					return nil, nil, err
				}
			}

			return nil, nil, err
		}

		if !initialized {
			initialized = true

			// Initialize cdn peer
			peer, err = c.initPeer(task, piece)
			if err != nil {
				return nil, nil, err
			}
		}

		peer.Log.Infof("receive piece: %#v %#v", piece, piece.PieceInfo)

		// Handle begin of piece
		if piece.PieceInfo != nil && piece.PieceInfo.PieceNum == common.BeginOfPiece {
			if err := peer.FSM.Event(PeerEventDownload); err != nil {
				return nil, nil, err
			}
			continue
		}

		// Handle end of piece
		if piece.Done {
			return peer, &rpcscheduler.PeerResult{
				TotalPieceCount: piece.TotalPieceCount,
				ContentLength:   piece.ContentLength,
			}, nil
		}

		// Handle piece download successfully
		peer.Pieces.Set(uint(piece.PieceInfo.PieceNum))
		// TODO(244372610) CDN should set piece cost
		peer.AppendPieceCost(0)
		task.StorePiece(piece.PieceInfo)
	}
}
// TriggerTask start to trigger cdn task
TriggerTask start to trigger cdn task
// TriggerTask start to trigger cdn task
func (c *cdn) TriggerTask(ctx context.Context, task *Task) (*Peer, *rpcscheduler.PeerResult, error) {
	stream, err := c.client.ObtainSeeds(ctx, &cdnsystem.SeedRequest{
		TaskId:  task.ID,
		Url:     task.URL,
		UrlMeta: task.URLMeta,
	})
	if err != nil {
		return nil, nil, err
	}
	var (
		peer        *Peer
		initialized bool
	)
	for {
		piece, err := stream.Recv()
		if err != nil {
			if peer != nil {
				if err := peer.FSM.Event(PeerEventDownloadFailed); err != nil {
					return nil, nil, err
				}
			}
			return nil, nil, err
		}
		if !initialized {
			initialized = true
			peer, err = c.initPeer(task, piece)
			if err != nil {
				return nil, nil, err
			}
		}
		peer.Log.Infof("receive piece: %#v %#v", piece, piece.PieceInfo)
		if piece.PieceInfo != nil && piece.PieceInfo.PieceNum == common.BeginOfPiece {
			if err := peer.FSM.Event(PeerEventDownload); err != nil {
				return nil, nil, err
			}
			continue
		}
		if piece.Done {
			return peer, &rpcscheduler.PeerResult{
				TotalPieceCount: piece.TotalPieceCount,
				ContentLength:   piece.ContentLength,
			}, nil
		}
		peer.Pieces.Set(uint(piece.PieceInfo.PieceNum))
		peer.AppendPieceCost(0)
		task.StorePiece(piece.PieceInfo)
	}
}
func ( c * cdn ) TriggerTask ( ctx context . Context , task * Task ) ( * Peer , * rpcscheduler . PeerResult , error ) { stream , err := c . client . ObtainSeeds ( ctx , & cdnsystem . SeedRequest { TaskId : task . ID , Url : task . URL , UrlMeta : task . URLMeta , } ) if err != nil { return nil , nil , err } var ( peer * Peer initialized bool ) for { piece , err := stream . Recv ( ) if err != nil { if peer != nil { if err := peer . FSM . Event ( PeerEventDownloadFailed ) ; err != nil { return nil , nil , err } } return nil , nil , err } if ! initialized { initialized = true peer , err = c . initPeer ( task , piece ) if err != nil { return nil , nil , err } } peer . Log . Infof ( " " , piece , piece . PieceInfo ) if piece . PieceInfo != nil && piece . PieceInfo . PieceNum == common . BeginOfPiece { if err := peer . FSM . Event ( PeerEventDownload ) ; err != nil { return nil , nil , err } continue } if piece . Done { return peer , & rpcscheduler . PeerResult { TotalPieceCount : piece . TotalPieceCount , ContentLength : piece . ContentLength , } , nil } peer . Pieces . Set ( uint ( piece . PieceInfo . PieceNum ) ) peer . AppendPieceCost ( 0 ) task . StorePiece ( piece . PieceInfo ) } }
TriggerTask start to trigger cdn task
TriggerTask start to trigger cdn task
// If the peer initialization succeeds and the download fails, // set peer status is PeerStateFailed. // Initialize cdn peer // Handle begin of piece // Handle end of piece // Handle piece download successfully // TODO(244372610) CDN should set piece cost