mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-09 06:21:52 +08:00
refactor: replace logger usage with common.Log for consistent logging
This commit is contained in:
26
core/core.go
26
core/core.go
@@ -7,8 +7,8 @@ import (
|
||||
|
||||
"github.com/celestix/gotgproto/ext"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
"github.com/krau/SaveAny-Bot/config"
|
||||
"github.com/krau/SaveAny-Bot/logger"
|
||||
"github.com/krau/SaveAny-Bot/queue"
|
||||
"github.com/krau/SaveAny-Bot/types"
|
||||
)
|
||||
@@ -17,17 +17,17 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
for {
|
||||
semaphore <- struct{}{}
|
||||
task := queue.GetTask()
|
||||
logger.L.Debugf("Got task: %s", task.String())
|
||||
common.Log.Debugf("Got task: %s", task.String())
|
||||
|
||||
switch task.Status {
|
||||
case types.Pending:
|
||||
logger.L.Infof("Processing task: %s", task.String())
|
||||
common.Log.Infof("Processing task: %s", task.String())
|
||||
if err := processPendingTask(task); err != nil {
|
||||
task.Error = err
|
||||
if errors.Is(err, context.Canceled) {
|
||||
task.Status = types.Canceled
|
||||
} else {
|
||||
logger.L.Errorf("Failed to do task: %s", err)
|
||||
common.Log.Errorf("Failed to do task: %s", err)
|
||||
task.Status = types.Failed
|
||||
}
|
||||
} else {
|
||||
@@ -35,10 +35,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
}
|
||||
queue.AddTask(task)
|
||||
case types.Succeeded:
|
||||
logger.L.Infof("Task succeeded: %s", task.String())
|
||||
common.Log.Infof("Task succeeded: %s", task.String())
|
||||
extCtx, ok := task.Ctx.(*ext.Context)
|
||||
if !ok {
|
||||
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
} else {
|
||||
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
|
||||
Message: fmt.Sprintf("文件保存成功\n [%s]: %s", task.StorageName, task.StoragePath),
|
||||
@@ -46,10 +46,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
})
|
||||
}
|
||||
case types.Failed:
|
||||
logger.L.Errorf("Task failed: %s", task.String())
|
||||
common.Log.Errorf("Task failed: %s", task.String())
|
||||
extCtx, ok := task.Ctx.(*ext.Context)
|
||||
if !ok {
|
||||
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
} else {
|
||||
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
|
||||
Message: "文件保存失败\n" + task.Error.Error(),
|
||||
@@ -57,10 +57,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
})
|
||||
}
|
||||
case types.Canceled:
|
||||
logger.L.Infof("Task canceled: %s", task.String())
|
||||
common.Log.Infof("Task canceled: %s", task.String())
|
||||
extCtx, ok := task.Ctx.(*ext.Context)
|
||||
if !ok {
|
||||
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
|
||||
} else {
|
||||
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
|
||||
Message: "任务已取消",
|
||||
@@ -68,16 +68,16 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
})
|
||||
}
|
||||
default:
|
||||
logger.L.Errorf("Unknown task status: %s", task.Status)
|
||||
common.Log.Errorf("Unknown task status: %s", task.Status)
|
||||
}
|
||||
<-semaphore
|
||||
logger.L.Debugf("Task done: %s; status: %s", task.String(), task.Status)
|
||||
common.Log.Debugf("Task done: %s; status: %s", task.String(), task.Status)
|
||||
queue.DoneTask(task)
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
logger.L.Info("Start processing tasks...")
|
||||
common.Log.Info("Start processing tasks...")
|
||||
semaphore := make(chan struct{}, config.Cfg.Workers)
|
||||
for i := 0; i < config.Cfg.Workers; i++ {
|
||||
go worker(queue.Queue, semaphore)
|
||||
|
||||
Reference in New Issue
Block a user