58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
|
|
package pipelinestatus
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
StatusPending = "pending"
|
||
|
|
StatusInProgress = "in_progress"
|
||
|
|
StatusDone = "done"
|
||
|
|
StatusError = "error"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
StageQueued = "queued"
|
||
|
|
StageTranscribing = "transcribing"
|
||
|
|
StageAnalysing = "analysing"
|
||
|
|
StageTagging = "tagging"
|
||
|
|
StageCompleted = "completed"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Event struct {
|
||
|
|
TaskID string `json:"task_id"`
|
||
|
|
Filename string `json:"filename,omitempty"`
|
||
|
|
Status string `json:"status"`
|
||
|
|
Stage string `json:"stage"`
|
||
|
|
Error string `json:"error,omitempty"`
|
||
|
|
Timestamp int64 `json:"timestamp"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func DeclareQueue(ch *amqp.Channel, queue string) error {
|
||
|
|
_, err := ch.QueueDeclare(queue, true, false, false, false, nil)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func Publish(ctx context.Context, ch *amqp.Channel, queue string, ev Event) error {
|
||
|
|
if ev.Timestamp == 0 {
|
||
|
|
ev.Timestamp = time.Now().Unix()
|
||
|
|
}
|
||
|
|
body, err := json.Marshal(ev)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := ch.PublishWithContext(ctx, "", queue, false, false, amqp.Publishing{
|
||
|
|
ContentType: "application/json",
|
||
|
|
Body: body,
|
||
|
|
DeliveryMode: amqp.Persistent,
|
||
|
|
}); err != nil {
|
||
|
|
return fmt.Errorf("publish status: %w", err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|