statuses update
This commit is contained in:
57
watcher/internal/pipelinestatus/status.go
Normal file
57
watcher/internal/pipelinestatus/status.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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
|
||||
}
|
||||
65
watcher/internal/pipelinestatus/status_test.go
Normal file
65
watcher/internal/pipelinestatus/status_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package pipelinestatus
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEventJSON(t *testing.T) {
|
||||
ev := Event{
|
||||
TaskID: "01HXABCDEF",
|
||||
Filename: "call.wav",
|
||||
Status: StatusPending,
|
||||
Stage: StageQueued,
|
||||
Timestamp: 1717843200,
|
||||
}
|
||||
body, err := json.Marshal(ev)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var got Event
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.TaskID != ev.TaskID || got.Status != StatusPending || got.Stage != StageQueued {
|
||||
t.Fatalf("unexpected event: %+v", got)
|
||||
}
|
||||
if got.Error != "" {
|
||||
t.Fatalf("expected empty error, got %q", got.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventJSONWithError(t *testing.T) {
|
||||
ev := Event{
|
||||
TaskID: "01HX",
|
||||
Status: StatusError,
|
||||
Stage: StageTranscribing,
|
||||
Error: "nexara timeout",
|
||||
}
|
||||
body, err := json.Marshal(ev)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !json.Valid(body) {
|
||||
t.Fatal("invalid json")
|
||||
}
|
||||
var got Event
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Error != "nexara timeout" {
|
||||
t.Fatalf("error field: got %q", got.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusConstants(t *testing.T) {
|
||||
statuses := []string{StatusPending, StatusInProgress, StatusDone, StatusError}
|
||||
seen := make(map[string]bool)
|
||||
for _, s := range statuses {
|
||||
if seen[s] {
|
||||
t.Fatalf("duplicate status %q", s)
|
||||
}
|
||||
seen[s] = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user