forked from mtusi2026/mtusi2026lab5
Реализация на 5
This commit is contained in:
@@ -27,6 +27,7 @@ import (
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/auth"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/chat"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/outbox"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/user"
|
||||
"github.com/google/uuid"
|
||||
stdhttp "net/http"
|
||||
@@ -56,6 +57,9 @@ func run() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(os.Args) > 1 && (os.Args[1] == "worker" || os.Args[1] == "dispatcher" || os.Args[1] == "jobs" || os.Args[1] == "outbox") {
|
||||
return runPipeline(cfg, attachmentConfig, os.Args[1:])
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "storage" {
|
||||
return runStorage(cfg, attachmentConfig, os.Args[2:])
|
||||
}
|
||||
@@ -91,6 +95,12 @@ func run() error {
|
||||
chatSvc := chat.New(tm, chatsRepo, messageRepo, chatCache)
|
||||
objectStore := objectstore.New(attachmentConfig)
|
||||
attachmentSvc := attachment.New(&storage.AttachmentsRepo{Pool: pgx}, objectStore, attachmentConfig, chatSvc)
|
||||
pipelineConfig, err := config.LoadPipeline()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outboxSvc := outbox.New(&storage.OutboxRepo{Pool: pgx}, tm, nil)
|
||||
attachmentSvc.Configure(tm, outboxSvc, pipelineConfig)
|
||||
|
||||
httpRouter := router.New(
|
||||
middleware.NewAuthMiddleware(authSvc),
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/broker"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/storage"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/objectstore"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres/txmanager"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/outbox"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func runPipeline(cfg *config.Config, a *config.Attachment, args []string) error {
|
||||
c, e := config.LoadPipeline()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
pool := postgres.New(cfg.Postgres)
|
||||
defer pool.Close()
|
||||
tm := txmanager.New(pool)
|
||||
repo := &storage.AttachmentsRepo{Pool: pool}
|
||||
rabbit := &broker.Rabbit{URL: c.BrokerURL}
|
||||
ob := outbox.New(&storage.OutboxRepo{Pool: pool}, tm, rabbit)
|
||||
svc := attachment.New(repo, objectstore.New(a), a, nil)
|
||||
svc.Configure(tm, ob, c)
|
||||
ob.OnDead = svc.PublishFailed
|
||||
if args[0] == "jobs" || args[0] == "outbox" {
|
||||
if len(args) < 2 {
|
||||
return errors.New("operator action required")
|
||||
}
|
||||
fs := flag.NewFlagSet(args[1], flag.ContinueOnError)
|
||||
idText := fs.String("id", "", "job ID (attachment ID for reprocess)")
|
||||
version := fs.Int("pipeline-version", 2, "new pipeline version")
|
||||
if e = fs.Parse(args[2:]); e != nil {
|
||||
return e
|
||||
}
|
||||
if args[1] == "list" {
|
||||
jobs, e := repo.ListJobs(ctx)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(jobs)
|
||||
}
|
||||
id, e := uuid.Parse(*idText)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
switch args[1] {
|
||||
case "redeliver":
|
||||
if os.Getenv("LAB5_FAULTS") != "1" {
|
||||
return errors.New("redeliver is restricted to the explicit fault profile")
|
||||
}
|
||||
var payload []byte
|
||||
e = tm.RunInTransaction(ctx, nil, func(tx pgx.Tx) error {
|
||||
j, e := repo.JobTx(ctx, tx, id)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
payload, e = json.Marshal(j.Message)
|
||||
return e
|
||||
})
|
||||
if e == nil {
|
||||
e = rabbit.Publish(ctx, outbox.DeliveryMessage{ID: uuid.New(), AggregateID: id, Payload: payload})
|
||||
}
|
||||
case "replay":
|
||||
e = svc.Replay(ctx, id)
|
||||
case "reprocess":
|
||||
e = svc.Reprocess(ctx, id, *version)
|
||||
default:
|
||||
return errors.New("unknown operator action")
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"accepted": true, "id": id})
|
||||
}
|
||||
if c.BrokerURL == "" {
|
||||
return errors.New("BROKER_URL_FILE is required")
|
||||
}
|
||||
if args[0] == "worker" {
|
||||
// Recovery continues while the consumer is disconnected or a delivery is paused.
|
||||
recoveryDone := make(chan struct{})
|
||||
defer func() { stop(); <-recoveryDone }()
|
||||
go func() {
|
||||
defer close(recoveryDone)
|
||||
tick := time.NewTicker(time.Second)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
if e := svc.Recover(ctx); e != nil {
|
||||
slog.Warn("job_recovery_failed")
|
||||
}
|
||||
if e := svc.Cleanup(ctx); e != nil {
|
||||
slog.Warn("source_cleanup_failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
for ctx.Err() == nil {
|
||||
if e = rabbit.Consume(ctx, c.Concurrency, c.Prefetch, svc.Process); e != nil && ctx.Err() == nil {
|
||||
slog.Warn("consumer_reconnecting")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for ctx.Err() == nil {
|
||||
_, e = ob.ProcessOnce(ctx)
|
||||
if e != nil {
|
||||
slog.Warn("dispatcher_retry")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -20,7 +20,9 @@ import (
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/objectstore"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres/txmanager"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/outbox"
|
||||
)
|
||||
|
||||
func runStorage(cfg *config.Config, c *config.Attachment, args []string) error {
|
||||
@@ -93,6 +95,12 @@ func runStorage(cfg *config.Config, c *config.Attachment, args []string) error {
|
||||
pool := postgres.New(cfg.Postgres)
|
||||
defer pool.Close()
|
||||
svc := attachment.New(&storage.AttachmentsRepo{Pool: pool}, store, c, nil)
|
||||
jobs, e := config.LoadPipeline()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
tm := txmanager.New(pool)
|
||||
svc.Configure(tm, outbox.New(&storage.OutboxRepo{Pool: pool}, tm, nil), jobs)
|
||||
switch args[0] {
|
||||
case "reconcile":
|
||||
fs := flag.NewFlagSet("reconcile", flag.ContinueOnError)
|
||||
|
||||
Reference in New Issue
Block a user