* 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
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/krau/SaveAny-Bot/config"
|
|
"github.com/krau/SaveAny-Bot/pkg/queue"
|
|
)
|
|
|
|
var queueInstance *queue.TaskQueue[Exectable]
|
|
|
|
type Exectable interface {
|
|
TaskID() string
|
|
Execute(ctx context.Context) error
|
|
}
|
|
|
|
func worker(ctx context.Context, qe *queue.TaskQueue[Exectable], semaphore chan struct{}) {
|
|
for {
|
|
semaphore <- struct{}{}
|
|
qtask, err := qe.Get()
|
|
if err != nil {
|
|
break // queue closed and empty
|
|
}
|
|
log.FromContext(ctx).Infof("Processing task: %s", qtask.ID)
|
|
task := qtask.Data
|
|
if err := task.Execute(qtask.Context()); err != nil {
|
|
log.FromContext(ctx).Errorf("Failed to execute task %s: %v", qtask.ID, err)
|
|
} else {
|
|
log.FromContext(ctx).Infof("Task %s completed successfully", qtask.ID)
|
|
}
|
|
qe.Done(qtask.ID)
|
|
<-semaphore
|
|
}
|
|
}
|
|
|
|
func Run(ctx context.Context) {
|
|
log.FromContext(ctx).Info("Start processing tasks...")
|
|
semaphore := make(chan struct{}, config.Cfg.Workers)
|
|
if queueInstance == nil {
|
|
queueInstance = queue.NewTaskQueue[Exectable]()
|
|
}
|
|
for range config.Cfg.Workers {
|
|
go worker(ctx, queueInstance, semaphore)
|
|
}
|
|
|
|
}
|
|
|
|
func AddTask(ctx context.Context, task Exectable) error {
|
|
return queueInstance.Add(queue.NewTask(ctx, task.TaskID(), task))
|
|
}
|
|
|
|
func CancelTask(ctx context.Context, id string) error {
|
|
err := queueInstance.CancelTask(id)
|
|
return err
|
|
}
|
|
|
|
func GetLength(ctx context.Context) int {
|
|
if queueInstance == nil {
|
|
return 0
|
|
}
|
|
return queueInstance.ActiveLength()
|
|
}
|