49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
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
|
|
}
|