github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ext/dload/notif.go (about)

     1  // Package cmn provides common low-level types and utilities for all aistore projects
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package dload
     6  
     7  import (
     8  	"net/http"
     9  	"net/url"
    10  	"time"
    11  
    12  	"github.com/NVIDIA/aistore/api/apc"
    13  	"github.com/NVIDIA/aistore/cmn"
    14  	"github.com/NVIDIA/aistore/cmn/cos"
    15  	"github.com/NVIDIA/aistore/core"
    16  	"github.com/NVIDIA/aistore/core/meta"
    17  	"github.com/NVIDIA/aistore/nl"
    18  	jsoniter "github.com/json-iterator/go"
    19  )
    20  
    21  type (
    22  	NotifDownloadListerner struct {
    23  		nl.ListenerBase
    24  	}
    25  	NotifDownload struct {
    26  		nl.Base
    27  		job jobif
    28  	}
    29  )
    30  
    31  // interface guard
    32  var (
    33  	_ nl.Listener = (*NotifDownloadListerner)(nil)
    34  	_ core.Notif  = (*NotifDownload)(nil)
    35  )
    36  
    37  func NewDownloadNL(jobID, kind string, smap *meta.Smap, progressInterval time.Duration) *NotifDownloadListerner {
    38  	return &NotifDownloadListerner{
    39  		ListenerBase: *nl.NewNLB(jobID, kind, "" /*causal action*/, smap.Tmap.ActiveMap(), progressInterval),
    40  	}
    41  }
    42  
    43  func (*NotifDownloadListerner) UnmarshalStats(rawMsg []byte) (stats any, finished, aborted bool, err error) {
    44  	dlStatus := &StatusResp{}
    45  	if err = jsoniter.Unmarshal(rawMsg, dlStatus); err != nil {
    46  		return
    47  	}
    48  	stats = dlStatus
    49  	aborted = dlStatus.Aborted
    50  	finished = dlStatus.JobFinished()
    51  	return
    52  }
    53  
    54  func (nd *NotifDownloadListerner) QueryArgs() cmn.HreqArgs {
    55  	var (
    56  		xid    = "nqui-" + cos.GenUUID()
    57  		q      = url.Values{apc.QparamUUID: []string{xid}} // compare w/ p.dladm
    58  		args   = cmn.HreqArgs{Method: http.MethodGet, Query: q}
    59  		dlBody = AdminBody{
    60  			ID: nd.UUID(), // jobID
    61  		}
    62  	)
    63  	args.Path = apc.URLPathDownload.S
    64  	args.Body = cos.MustMarshal(dlBody)
    65  	return args
    66  }
    67  
    68  //
    69  // NotifDownloader
    70  //
    71  
    72  func (nd *NotifDownload) ToNotifMsg(aborted bool) core.NotifMsg {
    73  	msg := core.NotifMsg{UUID: nd.job.ID(), Kind: apc.ActDownload, AbortedX: aborted}
    74  	stats, err := nd.job.ActiveStats()
    75  	if err != nil {
    76  		msg.ErrMsg = err.Error()
    77  	} else {
    78  		msg.Data = cos.MustMarshal(stats)
    79  	}
    80  	return msg
    81  }