* refactor: a big refactor. wip * refactor: port handle file * refactor: place all handlers * fix: task info nil pointer * feat: enhance task progress tracking and context management * feat: cancel task * feat: stream mode * feat: silent mode * feat: dir cmd * refactor: remove unused old file * feat: rule cmd * feat: handle silent mode * feat: batch task * fix: batch task progress and temp file cleanup * refactor: update file creation and cleanup methods for better resource management * feat: add save command with silent mode handling * feat: message link * feat: update message prompts to include file count in storage selection * feat: slient save links * refactor: reduce dup code * feat: rule type * feat: chose dir * feat: refactor file handling and storage rules, improve error handling and logging * feat: rule mode * feat: telegraph pics * fix: tphpics nil pointer and inaccurate dirpath * feat: silent save telegraph * feat: add suffix to avoid file overwrite * feat: new storage telegram * chore: tidy go mod
45 lines
674 B
Go
45 lines
674 B
Go
package queue
|
|
|
|
import (
|
|
"container/list"
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
type Task[T any] struct {
|
|
ID string
|
|
Data T
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
created time.Time
|
|
element *list.Element
|
|
}
|
|
|
|
func NewTask[T any](ctx context.Context, id string, data T) *Task[T] {
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
return &Task[T]{
|
|
ID: id,
|
|
Data: data,
|
|
ctx: cancelCtx,
|
|
cancel: cancel,
|
|
created: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (t *Task[T]) IsCancelled() bool {
|
|
select {
|
|
case <-t.ctx.Done():
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (t *Task[T]) Cancel() {
|
|
t.cancel()
|
|
}
|
|
|
|
func (t *Task[T]) Context() context.Context {
|
|
return t.ctx
|
|
}
|