初始化
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"juhe-factory/api/internal/config"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
func NewClient(cfg config.Config) *asynq.Client {
|
||||
return asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: cfg.RedisAddress,
|
||||
Password: cfg.RedisPassword,
|
||||
DB: cfg.AsynqRedisDB,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"juhe-factory/api/internal/config"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
func NewServer(cfg config.Config) *asynq.Server {
|
||||
concurrency := cfg.AIWorkerConcurrency
|
||||
if concurrency < 1 {
|
||||
concurrency = 1
|
||||
}
|
||||
return asynq.NewServer(asynq.RedisClientOpt{
|
||||
Addr: cfg.RedisAddress, Password: cfg.RedisPassword, DB: cfg.AsynqRedisDB,
|
||||
}, asynq.Config{Concurrency: concurrency, Queues: map[string]int{"ai": 10, "default": 1}})
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
const (
|
||||
TypeDispatchChannel = "ai:dispatch-channel"
|
||||
TypeSubmitTask = "ai:submit-task"
|
||||
TypePollTask = "ai:poll-task"
|
||||
TypeDownloadTask = "ai:download-task"
|
||||
TypeAnalyzeEpisode = "media:analyze-episode"
|
||||
TypeParseDramaEpisode = "drama:parse-episode"
|
||||
TypeAnalyzeScript = "script:analyze"
|
||||
)
|
||||
|
||||
type IDPayload struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
func NewIDTask(taskType string, id uuid.UUID) (*asynq.Task, error) {
|
||||
payload, err := json.Marshal(IDPayload{ID: id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return asynq.NewTask(taskType, payload), nil
|
||||
}
|
||||
|
||||
func EnqueueID(client *asynq.Client, taskType string, id uuid.UUID, delay time.Duration) error {
|
||||
task, err := NewIDTask(taskType, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
maxRetry := 0
|
||||
if taskType == TypeDispatchChannel || taskType == TypePollTask || taskType == TypeDownloadTask {
|
||||
maxRetry = 5
|
||||
}
|
||||
options := []asynq.Option{asynq.MaxRetry(maxRetry), asynq.Queue("ai")}
|
||||
if delay > 0 {
|
||||
options = append(options, asynq.ProcessIn(delay))
|
||||
}
|
||||
_, err = client.Enqueue(task, options...)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user