feat: port verified messenger from lab 6

This commit is contained in:
Dmitrii Grishko
2026-11-14 19:16:28 +03:00
parent 20afc2c790
commit e3d7df4e72
142 changed files with 13702 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
package commands
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"messenger/src/backend/repositories"
)
func runUploadCleanup(args []string) error {
flags := flag.NewFlagSet("attachments-cleanup", flag.ContinueOnError)
loop := flags.Bool("loop", false, "run every 60 seconds")
dryRun := flags.Bool("dry-run", false, "report without deleting anything")
if err := flags.Parse(args); err != nil {
return err
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
init, cancel := context.WithTimeout(ctx, 20*time.Second)
db, err := repositories.OpenDatabase(init)
cancel()
if err != nil {
return err
}
defer db.Close()
objects, err := repositories.NewObjectStoreFromEnvironment()
if err != nil {
return err
}
store := repositories.NewAttachmentStore(db)
for {
pass, cancel := context.WithTimeout(ctx, 30*time.Second)
result, err := store.CleanupUploads(pass, *dryRun, objects.Delete)
direct, directErr := store.CleanupDirectUploads(pass, *dryRun, objects.Delete)
jobs, jobsErr := store.CleanupJobs(pass, 24*time.Hour, *dryRun, objects.Delete)
cancel()
if encodeErr := json.NewEncoder(os.Stdout).Encode(map[string]any{"originals": result, "direct": direct, "job_staging": jobs}); encodeErr != nil {
return encodeErr
}
if !*loop {
if err == nil {
err = directErr
if err == nil {
err = jobsErr
}
}
return err
}
if err != nil || directErr != nil || jobsErr != nil {
log.Print("upload cleanup pass failed; journal retained for retry")
}
select {
case <-ctx.Done():
return nil
case <-time.After(60 * time.Second):
}
}
}
+76
View File
@@ -0,0 +1,76 @@
package commands
import (
"context"
"encoding/json"
"errors"
"os"
"strconv"
"time"
"github.com/google/uuid"
"messenger/src/backend/repositories"
)
func runJobAdmin(args []string) error {
if len(args) < 1 {
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
db, err := repositories.OpenDatabase(ctx)
if err != nil {
return errors.New("database unavailable")
}
defer db.Close()
store := repositories.NewAttachmentStore(db)
if args[0] == "list-failed" && len(args) == 1 {
jobs, e := store.FailedJobs(ctx)
if e != nil {
return errors.New("cannot read failed registry")
}
return json.NewEncoder(os.Stdout).Encode(jobs)
}
if len(args) < 2 {
return errors.New("job id required")
}
if _, err = uuid.Parse(args[1]); err != nil {
return errors.New("invalid job id")
}
objects, err := repositories.NewObjectStoreFromEnvironment()
if err != nil {
return err
}
var changed bool
switch {
case args[0] == "upgrade" && len(args) == 4:
version, e := strconv.Atoi(args[2])
if e != nil {
return errors.New("invalid pipeline version")
}
changed, err = store.UpgradePipeline(ctx, args[1], version, args[3], func(ctx context.Context, key string) error {
r, e := objects.Get(ctx, key)
if e == nil {
r.Close()
}
return e
})
case args[0] == "replay" && len(args) == 3:
changed, err = store.ReplayJob(ctx, args[1], args[2], func(ctx context.Context, key string) error {
r, e := objects.Get(ctx, key)
if e == nil {
r.Close()
}
return e
})
case args[0] == "cleanup-staging" && len(args) == 2:
changed, err = store.CleanupJobStaging(ctx, args[1], 24*time.Hour, false, objects.Delete)
default:
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
}
if err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(map[string]bool{"changed": changed})
}
+79
View File
@@ -0,0 +1,79 @@
package commands
import (
"context"
"errors"
"log"
"net"
"net/http"
"os"
"time"
"messenger/src/backend/app"
"messenger/src/backend/repositories"
"messenger/src/backend/workers"
)
// Run dispatches the existing executable modes; the binary and CLI stay the same.
func Run(args []string) error {
if len(args) == 0 {
if environment := os.Getenv("APP_ENV"); environment != "" {
log.SetPrefix("[" + environment + "] ")
}
return app.RunHTTP()
}
switch args[0] {
case "image-process":
return workers.RunImageChild(args[1:])
case "jobs":
return runJobAdmin(args[1:])
case "worker", "dispatcher", "broker-publish":
return workers.RunPipeline(args[0], args[1:])
}
if environment := os.Getenv("APP_ENV"); environment != "" {
log.SetPrefix("[" + environment + "] ")
}
switch {
case args[0] == "s3-bootstrap" && len(args) == 1:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := repositories.BootstrapS3(ctx); err != nil {
return err
}
log.Print("private S3 bucket ready")
return nil
case args[0] == "attachments-cleanup":
return runUploadCleanup(args[1:])
case args[0] == "healthcheck" && len(args) == 1:
return healthcheck()
case args[0] == "migrate" && len(args) == 2 && (args[1] == "up" || args[1] == "down"):
url, err := repositories.DatabaseURL()
if err != nil {
return err
}
if err = repositories.RunMigrations(url, args[1]); err != nil {
return err
}
log.Printf("migration %s complete", args[1])
return nil
default:
return errors.New("usage: messenger migrate up|down")
}
}
func healthcheck() error {
client := http.Client{Timeout: 2 * time.Second}
port := os.Getenv("APP_PORT")
if port == "" {
port = "8080"
}
response, err := client.Get("http://" + net.JoinHostPort("127.0.0.1", port) + "/health/ready")
if err != nil {
return err
}
response.Body.Close()
if response.StatusCode != http.StatusOK {
return errors.New("API is not ready")
}
return nil
}