mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 22:26:38 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package driver_asynq_worker
|
|
|
|
import (
|
|
"Wavelet/pkg/idgen"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
const (
|
|
taskExecutionLogRedisKeyPrefix = "task:execution:log:"
|
|
taskExecutionLogExpiration = 24 * time.Hour
|
|
taskExecutionLogMaxLines = 1000
|
|
)
|
|
|
|
func taskExecutionLogRedisKey(taskID string) string {
|
|
return taskExecutionLogRedisKeyPrefix + taskID
|
|
}
|
|
|
|
func createTaskExecution(ctx context.Context, execution *TaskExecution) error {
|
|
if execution.ID == 0 {
|
|
execution.ID = idgen.NextUint64ID()
|
|
}
|
|
return getDB(ctx).Create(execution).Error
|
|
}
|
|
|
|
func updateTaskExecution(ctx context.Context, execution *TaskExecution) error {
|
|
return getDB(ctx).Omit("log").Save(execution).Error
|
|
}
|
|
|
|
func getTaskExecutionByID(ctx context.Context, id uint64) (*TaskExecution, error) {
|
|
var execution TaskExecution
|
|
if err := getDB(ctx).Where("id = ?", id).First(&execution).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
_ = loadTaskExecutionLog(ctx, &execution)
|
|
return &execution, nil
|
|
}
|
|
|
|
func getTaskExecutionByTaskID(ctx context.Context, taskID string) (*TaskExecution, error) {
|
|
var execution TaskExecution
|
|
if err := getDB(ctx).Where("task_id = ?", taskID).First(&execution).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
_ = loadTaskExecutionLog(ctx, &execution)
|
|
return &execution, nil
|
|
}
|
|
|
|
func appendTaskExecutionLog(ctx context.Context, taskID, logLine string) error {
|
|
rdb := getRedisClient()
|
|
if rdb == nil {
|
|
return errors.New("redis client is not initialized")
|
|
}
|
|
|
|
now := time.Now().Format("15:04:05")
|
|
line := fmt.Sprintf("[%s] %s\n", now, logLine)
|
|
key := taskExecutionLogRedisKey(taskID)
|
|
|
|
_, err := rdb.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
|
pipe.RPush(ctx, key, line)
|
|
pipe.LTrim(ctx, key, -taskExecutionLogMaxLines, -1)
|
|
pipe.Expire(ctx, key, taskExecutionLogExpiration)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("append task execution log to redis: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func flushTaskExecutionLog(ctx context.Context, taskID string) error {
|
|
rdb := getRedisClient()
|
|
if rdb == nil {
|
|
return errors.New("redis client is not initialized")
|
|
}
|
|
|
|
key := taskExecutionLogRedisKey(taskID)
|
|
logLines, err := rdb.LRange(ctx, key, 0, -1).Result()
|
|
if err != nil {
|
|
return fmt.Errorf("get task execution log from redis: %w", err)
|
|
}
|
|
if len(logLines) == 0 {
|
|
return nil
|
|
}
|
|
logText := strings.Join(logLines, "")
|
|
|
|
result := getDB(ctx).Model(&TaskExecution{}).
|
|
Where("task_id = ?", taskID).
|
|
Update("log", logText)
|
|
if result.Error != nil {
|
|
return fmt.Errorf("persist task execution log: %w", result.Error)
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("persist task execution log: task %q not found", taskID)
|
|
}
|
|
|
|
if err := rdb.Del(ctx, key).Err(); err != nil {
|
|
return fmt.Errorf("delete persisted task execution log from redis: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func loadTaskExecutionLog(ctx context.Context, execution *TaskExecution) error {
|
|
rdb := getRedisClient()
|
|
if rdb == nil {
|
|
return nil
|
|
}
|
|
|
|
logLines, err := rdb.LRange(ctx, taskExecutionLogRedisKey(execution.TaskID), 0, -1).Result()
|
|
if err != nil {
|
|
return fmt.Errorf("get task execution log from redis: %w", err)
|
|
}
|
|
if len(logLines) == 0 {
|
|
return nil
|
|
}
|
|
|
|
execution.Log = strings.Join(logLines, "")
|
|
return nil
|
|
}
|