@@ -27,8 +27,6 @@ type RayLogsHandler struct {
2727 LogFiles chan string
2828 RootDir string
2929 SessionDir string
30- HttpClient * http.Client
31- GCSEndpoint string
3230 RayClusterName string
3331 RayClusterID string
3432 RayNodeName string
@@ -50,7 +48,7 @@ func (h *RayLogsHandler) CreateDirectory(directoryPath string) error {
5048 _ , err := h .StorageClient .Bucket (h .GCSBucket ).Object (objectPath ).Attrs (ctx )
5149 if errors .Is (err , gstorage .ErrObjectNotExist ) {
5250 writer := h .StorageClient .Bucket (h .GCSBucket ).Object (objectPath ).NewWriter (ctx )
53- if createErr := writer .Close (); err != nil {
51+ if createErr := writer .Close (); createErr != nil {
5452 logrus .Errorf ("Failed to create directory: %s, error: %v" , objectPath , createErr )
5553 return createErr
5654 }
@@ -73,6 +71,7 @@ func (h *RayLogsHandler) WriteFile(file string, reader io.ReadSeeker) error {
7371 _ , err := io .Copy (writer , reader )
7472 if err != nil {
7573 logrus .Error ("GCS Client failed to Copy from source." )
74+ writer .Close ()
7675 return err
7776 }
7877
@@ -88,7 +87,7 @@ func (h *RayLogsHandler) ListFiles(clusterId string, directory string) []string
8887 // TODO(chiayi): Look into potential timeout issues
8988 ctx := context .Background ()
9089
91- pathPrefix := path .Join (h . RootDir , clusterId , directory )
90+ pathPrefix := strings . TrimPrefix ( path .Join (clusterId , directory ), "/" ) + "/"
9291
9392 query := & gstorage.Query {
9493 Prefix : pathPrefix ,
@@ -121,7 +120,7 @@ func (h *RayLogsHandler) ListFiles(clusterId string, directory string) []string
121120 // We only want files, so check if attrs.Name is non-empty.
122121 // Exclude the placeholder object if it exists for the directory itself.
123122 // attrs.Name contains the whole object path.
124- if ! strings .HasSuffix (attrs .Name , "/" ) {
123+ if attrs . Name != "" && ! strings .HasSuffix (attrs .Name , "/" ) {
125124 fileList = append (fileList , attrs .Name )
126125 }
127126 }
@@ -149,7 +148,8 @@ func (h *RayLogsHandler) List() []utils.ClusterInfo {
149148 break
150149 }
151150 if err != nil {
152- logrus .Fatalf ("Failed to get attribute of ray clusters: %v" , err )
151+ logrus .Errorf ("Failed to get attribute of ray clusters: %v" , err )
152+ return nil
153153 }
154154
155155 fullObjectPath := objectAttr .Name
@@ -161,7 +161,7 @@ func (h *RayLogsHandler) List() []utils.ClusterInfo {
161161 clusterMeta := strings .Split (metaInfo [0 ], "_" )
162162 if len (clusterMeta ) != 2 {
163163 logrus .Errorf ("Unable to get cluster name and namespace from directory: %s" , metaInfo [0 ])
164- return nil
164+ continue
165165 }
166166 cluster .Name = clusterMeta [0 ]
167167 cluster .Namespace = clusterMeta [1 ]
@@ -170,6 +170,7 @@ func (h *RayLogsHandler) List() []utils.ClusterInfo {
170170 time , err := utils .GetDateTimeFromSessionID (metaInfo [1 ])
171171 if err != nil {
172172 logrus .Errorf ("Failed to get date time from the given sessionID: %s, error: %v" , metaInfo [1 ], err )
173+ continue
173174 }
174175 cluster .CreateTimeStamp = time .Unix ()
175176 cluster .CreateTime = time .UTC ().Format (("2006-01-02T15:04:05Z" ))
@@ -210,14 +211,20 @@ func (h *RayLogsHandler) GetContent(clusterId string, fileName string) io.Reader
210211 // Change into bufio.Scanner if needed or limit the size of the read
211212 data , err := io .ReadAll (reader )
212213 if err != nil {
213- logrus .Fatalf ("Failed to get all content of the file: %s, %v" , fileName , err )
214+ logrus .Errorf ("Failed to get all content of the file: %s, %v" , fileName , err )
215+ return nil
214216 }
215217 return bytes .NewReader (data )
216218}
217219
218220func createGCSBucket (gcsClient * gstorage.Client , projectID , bucketName string ) error {
219221 ctx := context .Background ()
220222
223+ if projectID == "" {
224+ log .Errorf ("Project ID cannot be empty. Failed to create GCS bucket: %s" , bucketName )
225+ return errors .New ("Project ID cannot be empty when creating a GCS Bucket" )
226+ }
227+
221228 bucket := gcsClient .Bucket (bucketName )
222229 if err := bucket .Create (ctx , projectID , nil ); err != nil {
223230 log .Errorf ("Failed to create GCS bucket: %s" , bucketName )
@@ -262,6 +269,10 @@ func New(c *config) (*RayLogsHandler, error) {
262269 baseTransport ,
263270 option .WithScopes (gstorage .ScopeFullControl ),
264271 )
272+ if err != nil {
273+ logrus .Errorf ("Failed to create authentication transport object" )
274+ return nil , err
275+ }
265276
266277 // Create a custom client with the authenticated transport
267278 customHttpTransportClient := & http.Client {
@@ -281,7 +292,7 @@ func New(c *config) (*RayLogsHandler, error) {
281292 // Check if bucket exists
282293 _ , err = storageClient .Bucket (c .Bucket ).Attrs (ctx )
283294 if err == gstorage .ErrBucketNotExist {
284- logrus .Warn ("Bucket %s does not exist, will attempt to create bucket" , c .Bucket )
295+ logrus .Warnf ("Bucket %s does not exist, will attempt to create bucket" , c .Bucket )
285296 err = createGCSBucket (storageClient , c .GCPProjectID , c .Bucket )
286297 if err != nil {
287298 return nil , err
0 commit comments