feat: add task command to client and Title method to Task for tasks queue managing, #157

This commit is contained in:
krau
2025-12-15 11:29:55 +08:00
parent 51198a1e3d
commit 0ca3d97711
12 changed files with 210 additions and 24 deletions

View File

@@ -125,6 +125,7 @@ func (tq *TaskQueue[T]) RunningTasks() []TaskInfo {
}
tasks = append(tasks, TaskInfo{
ID: task.ID,
Title: task.Title,
Created: task.created,
Cancelled: task.Cancelled(),
})
@@ -144,6 +145,7 @@ func (tq *TaskQueue[T]) QueuedTasks() []TaskInfo {
if !task.Cancelled() {
tasks = append(tasks, TaskInfo{
ID: task.ID,
Title: task.Title,
Created: task.created,
Cancelled: task.Cancelled(),
})

View File

@@ -11,7 +11,7 @@ import (
// helper to create a simple Task with integer payload
func newTask(id string) *queue.Task[int] {
return queue.NewTask(context.Background(), id, 0)
return queue.NewTask(context.Background(), id, "testing", 0)
}
func TestAddAndLength(t *testing.T) {
@@ -103,4 +103,4 @@ func TestConcurrencySafety(t *testing.T) {
}
}()
wg.Wait()
}
}

View File

@@ -8,6 +8,7 @@ import (
type Task[T any] struct {
ID string
Title string
Data T
ctx context.Context
cancel context.CancelFunc
@@ -20,12 +21,14 @@ type TaskInfo struct {
ID string
Created time.Time
Cancelled bool
Title string
}
func NewTask[T any](ctx context.Context, id string, data T) *Task[T] {
func NewTask[T any](ctx context.Context, id string, title string, data T) *Task[T] {
cancelCtx, cancel := context.WithCancel(ctx)
return &Task[T]{
ID: id,
Title: title,
Data: data,
ctx: cancelCtx,
cancel: cancel,