chore: add callback for task
This commit is contained in:
parent
d665cce739
commit
31ff31d3dd
@ -13,6 +13,7 @@ type User struct {
|
|||||||
BasePath string `json:"base_path"` // base path
|
BasePath string `json:"base_path"` // base path
|
||||||
AllowUpload bool `json:"allow_upload"` // allow upload
|
AllowUpload bool `json:"allow_upload"` // allow upload
|
||||||
Role int `json:"role"` // user's role
|
Role int `json:"role"` // user's role
|
||||||
|
//OfflineDownload bool `json:"offline_download"` // TODO? allow offline download
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u User) IsGuest() bool {
|
func (u User) IsGuest() bool {
|
||||||
|
@ -14,12 +14,12 @@ type StatusInfo struct {
|
|||||||
UploadSpeed string `json:"uploadSpeed"` // Upload speed of this download measured in bytes/sec.
|
UploadSpeed string `json:"uploadSpeed"` // Upload speed of this download measured in bytes/sec.
|
||||||
InfoHash string `json:"infoHash"` // InfoHash. BitTorrent only.
|
InfoHash string `json:"infoHash"` // InfoHash. BitTorrent only.
|
||||||
NumSeeders string `json:"numSeeders"` // The number of seeders aria2 has connected to. 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.
|
PieceLength string `json:"pieceLength"` // Piece length in bytes.
|
||||||
NumPieces string `json:"numPieces"` // The number of pieces.
|
NumPieces string `json:"numPieces"` // The number of pieces.
|
||||||
Connections string `json:"connections"` // The number of peers/servers aria2 has connected to.
|
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.
|
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.
|
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.
|
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.
|
Dir string `json:"dir"` // Directory to save files.
|
||||||
|
@ -13,8 +13,8 @@ type Manager struct {
|
|||||||
tasks generic_sync.MapOf[uint64, *Task]
|
tasks generic_sync.MapOf[uint64, *Task]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Submit(name string, f Func) uint64 {
|
func (tm *Manager) Submit(name string, f Func, callbacks ...Callback) uint64 {
|
||||||
task := newTask(name, f)
|
task := newTask(name, f, callbacks...)
|
||||||
tm.addTask(task)
|
tm.addTask(task)
|
||||||
tm.do(task.ID)
|
tm.do(task.ID)
|
||||||
return task.ID
|
return task.ID
|
||||||
|
@ -17,6 +17,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Func func(task *Task) error
|
type Func func(task *Task) error
|
||||||
|
type Callback func(task *Task)
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
ID uint64
|
ID uint64
|
||||||
@ -24,20 +25,25 @@ type Task struct {
|
|||||||
Status string
|
Status string
|
||||||
Error error
|
Error error
|
||||||
Func Func
|
Func Func
|
||||||
Progress int
|
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
|
progress int
|
||||||
|
callback Callback
|
||||||
cancel context.CancelFunc
|
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())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
return &Task{
|
t := &Task{
|
||||||
Name: name,
|
Name: name,
|
||||||
Status: PENDING,
|
Status: PENDING,
|
||||||
Func: func_,
|
Func: func_,
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
if len(callbacks) > 0 {
|
||||||
|
t.callback = callbacks[0]
|
||||||
|
}
|
||||||
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) SetStatus(status string) {
|
func (t *Task) SetStatus(status string) {
|
||||||
@ -45,7 +51,7 @@ func (t *Task) SetStatus(status string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) SetProgress(percentage int) {
|
func (t *Task) SetProgress(percentage int) {
|
||||||
t.Progress = percentage
|
t.progress = percentage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) run() {
|
func (t *Task) run() {
|
||||||
@ -64,6 +70,9 @@ func (t *Task) run() {
|
|||||||
t.Status = ERRORED
|
t.Status = ERRORED
|
||||||
} else {
|
} else {
|
||||||
t.Status = FINISHED
|
t.Status = FINISHED
|
||||||
|
if t.callback != nil {
|
||||||
|
t.callback(t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +81,9 @@ func (t *Task) retry() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Cancel() {
|
func (t *Task) Cancel() {
|
||||||
|
if t.Status == FINISHED || t.Status == CANCELED {
|
||||||
|
return
|
||||||
|
}
|
||||||
if t.cancel != nil {
|
if t.cancel != nil {
|
||||||
t.cancel()
|
t.cancel()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user