chore: add callback for task

refactor/fs
Noah Hsu 2022-06-20 17:13:19 +08:00
parent d665cce739
commit 31ff31d3dd
4 changed files with 21 additions and 8 deletions

View File

@ -13,6 +13,7 @@ type User struct {
BasePath string `json:"base_path"` // base path
AllowUpload bool `json:"allow_upload"` // allow upload
Role int `json:"role"` // user's role
//OfflineDownload bool `json:"offline_download"` // TODO? allow offline download
}
func (u User) IsGuest() bool {

View File

@ -14,12 +14,12 @@ type StatusInfo struct {
UploadSpeed string `json:"uploadSpeed"` // Upload speed of this download measured in bytes/sec.
InfoHash string `json:"infoHash"` // InfoHash. BitTorrent only.
NumSeeders string `json:"numSeeders"` // The number of seeders aria2 has connected to. BitTorrent only.
Seeder string `json:"seeder"` // true if the local endpoint is a seeder. Otherwise false. BitTorrent only.
Seeder string `json:"seeder"` // true if the local endpoint is a seeder. Otherwise, false. BitTorrent only.
PieceLength string `json:"pieceLength"` // Piece length in bytes.
NumPieces string `json:"numPieces"` // The number of pieces.
Connections string `json:"connections"` // The number of peers/servers aria2 has connected to.
ErrorCode string `json:"errorCode"` // The code of the last error for this item, if any. The value is a string. The error codes are defined in the EXIT STATUS section. This value is only available for stopped/completed downloads.
ErrorMessage string `json:"errorMessage"` // The (hopefully) human readable error message associated to errorCode.
ErrorMessage string `json:"errorMessage"` // The (hopefully) human-readable error message associated to errorCode.
FollowedBy []string `json:"followedBy"` // List of GIDs which are generated as the result of this download. For example, when aria2 downloads a Metalink file, it generates downloads described in the Metalink (see the --follow-metalink option). This value is useful to track auto-generated downloads. If there are no such downloads, this key will not be included in the response.
BelongsTo string `json:"belongsTo"` // GID of a parent download. Some downloads are a part of another download. For example, if a file in a Metalink has BitTorrent resources, the downloads of ".torrent" files are parts of that parent. If this download has no parent, this key will not be included in the response.
Dir string `json:"dir"` // Directory to save files.

View File

@ -13,8 +13,8 @@ type Manager struct {
tasks generic_sync.MapOf[uint64, *Task]
}
func (tm *Manager) Submit(name string, f Func) uint64 {
task := newTask(name, f)
func (tm *Manager) Submit(name string, f Func, callbacks ...Callback) uint64 {
task := newTask(name, f, callbacks...)
tm.addTask(task)
tm.do(task.ID)
return task.ID

View File

@ -17,6 +17,7 @@ var (
)
type Func func(task *Task) error
type Callback func(task *Task)
type Task struct {
ID uint64
@ -24,20 +25,25 @@ type Task struct {
Status string
Error error
Func Func
Progress int
Ctx context.Context
progress int
callback Callback
cancel context.CancelFunc
}
func newTask(name string, func_ Func) *Task {
func newTask(name string, func_ Func, callbacks ...Callback) *Task {
ctx, cancel := context.WithCancel(context.Background())
return &Task{
t := &Task{
Name: name,
Status: PENDING,
Func: func_,
Ctx: ctx,
cancel: cancel,
}
if len(callbacks) > 0 {
t.callback = callbacks[0]
}
return t
}
func (t *Task) SetStatus(status string) {
@ -45,7 +51,7 @@ func (t *Task) SetStatus(status string) {
}
func (t *Task) SetProgress(percentage int) {
t.Progress = percentage
t.progress = percentage
}
func (t *Task) run() {
@ -64,6 +70,9 @@ func (t *Task) run() {
t.Status = ERRORED
} else {
t.Status = FINISHED
if t.callback != nil {
t.callback(t)
}
}
}
@ -72,6 +81,9 @@ func (t *Task) retry() {
}
func (t *Task) Cancel() {
if t.Status == FINISHED || t.Status == CANCELED {
return
}
if t.cancel != nil {
t.cancel()
}