-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
143 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Package task manage task, such as file upload, file copy between accounts, offline download, etc. | ||
package task | ||
|
||
import ( | ||
"context" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
PENDING = "pending" | ||
RUNNING = "running" | ||
FINISHED = "finished" | ||
CANCELING = "canceling" | ||
CANCELED = "canceled" | ||
) | ||
|
||
type Func func(task *Task) error | ||
|
||
type Task struct { | ||
ID int64 | ||
Name string | ||
Status string | ||
Error error | ||
Func Func | ||
Ctx context.Context | ||
cancel context.CancelFunc | ||
} | ||
|
||
func newTask(name string, func_ Func) *Task { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
return &Task{ | ||
Name: name, | ||
Status: PENDING, | ||
Func: func_, | ||
Ctx: ctx, | ||
cancel: cancel, | ||
} | ||
} | ||
|
||
func (t *Task) SetStatus(status string) { | ||
t.Status = status | ||
} | ||
|
||
func (t *Task) Run() { | ||
t.Status = RUNNING | ||
t.Error = t.Func(t) | ||
if errors.Is(t.Ctx.Err(), context.Canceled) { | ||
t.Status = CANCELED | ||
} else { | ||
t.Status = FINISHED | ||
} | ||
} | ||
|
||
func (t *Task) Retry() { | ||
t.Run() | ||
} | ||
|
||
func (t *Task) Cancel() { | ||
if t.cancel != nil { | ||
t.cancel() | ||
} | ||
// maybe can't cancel | ||
t.Status = CANCELING | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package utils | ||
|
||
import "context" | ||
|
||
func IsCanceled(ctx context.Context) bool { | ||
select { | ||
case <-ctx.Done(): | ||
return true | ||
default: | ||
return false | ||
} | ||
} |