Skip to content

Commit

Permalink
feat: added fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Apr 23, 2024
1 parent 76f3127 commit e83bcf2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
17 changes: 12 additions & 5 deletions entity/fs_file_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package entity

import (
"github.com/crawlab-team/crawlab-core/interfaces"
"os"
"time"
)

type FsFileInfo struct {
Name string `json:"name"` // file name
Path string `json:"path"` // file path
FullPath string `json:"full_path"` // file full path
Extension string `json:"extension"` // file extension
Md5 string `json:"md5"` // MD5 hash
IsDir bool `json:"is_dir"` // whether it is directory
FileSize int64 `json:"file_size"` // file size (bytes)
Children []interfaces.FsFileInfo `json:"children"` // children for sub-directory
ModTime time.Time `json:"mod_time"` // modification time
Mode os.FileMode `json:"mode"` // file mode
}

func (f *FsFileInfo) GetName() string {
Expand All @@ -31,10 +34,6 @@ func (f *FsFileInfo) GetExtension() string {
return f.Extension
}

func (f *FsFileInfo) GetMd5() string {
return f.Md5
}

func (f *FsFileInfo) GetIsDir() bool {
return f.IsDir
}
Expand All @@ -43,6 +42,14 @@ func (f *FsFileInfo) GetFileSize() int64 {
return f.FileSize
}

func (f *FsFileInfo) GetModTime() time.Time {
return f.ModTime
}

func (f *FsFileInfo) GetMode() os.FileMode {
return f.Mode
}

func (f *FsFileInfo) GetChildren() []interfaces.FsFileInfo {
return f.Children
}
6 changes: 4 additions & 2 deletions fs/service_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func (svc *ServiceV2) List(path string) (files []interfaces.FsFileInfo, err erro
Path: filepath.ToSlash(relPath),
FullPath: p,
Extension: filepath.Ext(p),
Md5: "",
IsDir: info.IsDir(),
FileSize: info.Size(),
ModTime: info.ModTime(),
Mode: info.Mode(),
Children: nil,
}

Expand Down Expand Up @@ -90,9 +91,10 @@ func (svc *ServiceV2) GetFileInfo(path string) (file interfaces.FsFileInfo, err
Path: path,
FullPath: filepath.Join(svc.rootPath, path),
Extension: filepath.Ext(path),
Md5: "",
IsDir: f.IsDir(),
FileSize: f.Size(),
ModTime: f.ModTime(),
Mode: f.Mode(),
Children: nil,
}, nil
}
Expand Down
8 changes: 7 additions & 1 deletion interfaces/fs_file_info.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package interfaces

import (
"os"
"time"
)

type FsFileInfo interface {
GetName() string
GetPath() string
GetFullPath() string
GetExtension() string
GetMd5() string
GetIsDir() bool
GetFileSize() int64
GetModTime() time.Time
GetMode() os.FileMode
GetChildren() []FsFileInfo
}
15 changes: 8 additions & 7 deletions task/handler/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package handler
import (
"context"
"encoding/json"
"errors"
"github.com/apex/log"
config2 "github.com/crawlab-team/crawlab-core/config"
"github.com/crawlab-team/crawlab-core/constants"
"github.com/crawlab-team/crawlab-core/errors"
errors2 "github.com/crawlab-team/crawlab-core/errors"
client2 "github.com/crawlab-team/crawlab-core/grpc/client"
"github.com/crawlab-team/crawlab-core/interfaces"
"github.com/crawlab-team/crawlab-core/models/client"
Expand Down Expand Up @@ -311,13 +312,13 @@ func (svc *Service) getRunner(taskId primitive.ObjectID) (r interfaces.TaskRunne
log.Debugf("[TaskHandlerService] getRunner: taskId[%v]", taskId)
v, ok := svc.runners.Load(taskId)
if !ok {
return nil, trace.TraceError(errors.ErrorTaskNotExists)
return nil, trace.TraceError(errors2.ErrorTaskNotExists)
}
switch v.(type) {
case interfaces.TaskRunner:
r = v.(interfaces.TaskRunner)
default:
return nil, trace.TraceError(errors.ErrorModelInvalidType)
return nil, trace.TraceError(errors2.ErrorModelInvalidType)
}
return r, nil
}
Expand Down Expand Up @@ -405,7 +406,7 @@ func (svc *Service) run(taskId primitive.ObjectID) (err error) {
// attempt to get runner from pool
_, ok := svc.runners.Load(taskId)
if ok {
return trace.TraceError(errors.ErrorTaskAlreadyExists)
return trace.TraceError(errors2.ErrorTaskAlreadyExists)
}

// create a new task runner
Expand All @@ -430,10 +431,10 @@ func (svc *Service) run(taskId primitive.ObjectID) (err error) {
// run task process (blocking)
// error or finish after task runner ends
if err := r.Run(); err != nil {
switch err {
case constants.ErrTaskError:
switch {
case errors.Is(err, constants.ErrTaskError):
log.Errorf("task[%s] finished with error: %v", r.GetTaskId().Hex(), err)
case constants.ErrTaskCancelled:
case errors.Is(err, constants.ErrTaskCancelled):
log.Errorf("task[%s] cancelled", r.GetTaskId().Hex())
default:
log.Errorf("task[%s] finished with unknown error: %v", r.GetTaskId().Hex(), err)
Expand Down

0 comments on commit e83bcf2

Please sign in to comment.