Доработайте публикацию и сверку вложений по итогам ревью
This commit is contained in:
@@ -43,7 +43,7 @@ func run(ctx context.Context, args []string, out, errOut io.Writer) int {
|
||||
fmt.Fprintln(errOut, "reconcile: no positional arguments allowed; --grace must be at least 2m")
|
||||
return 2
|
||||
}
|
||||
dbConfig, err := config.LoadMigrate(os.LookupEnv)
|
||||
databaseURL, err := config.LoadDatabaseURL(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
@@ -54,12 +54,12 @@ func run(ctx context.Context, args []string, out, errOut io.Writer) int {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
}
|
||||
blobs, err := s3store.New(s3Config.Endpoint, s3Config.Region, s3Config.Bucket, s3Config.AccessKey, s3Config.SecretKey)
|
||||
blobs, err := s3store.New(s3Config)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "configuration error:", err)
|
||||
return 2
|
||||
}
|
||||
db, err := postgres.NewPool(ctx, dbConfig.DatabaseURL, 10)
|
||||
db, err := postgres.NewPool(ctx, databaseURL, 10)
|
||||
if err != nil {
|
||||
fmt.Fprintln(errOut, "reconcile:", err)
|
||||
return 1
|
||||
@@ -76,7 +76,8 @@ func run(ctx context.Context, args []string, out, errOut io.Writer) int {
|
||||
defer cancel()
|
||||
encoder := json.NewEncoder(out)
|
||||
var outputErr error
|
||||
stats, runErr := r.Run(scanCtx, *apply, *deep, func(f attachment.Finding) {
|
||||
options := attachment.ReconcileOptions{Apply: *apply, Deep: *deep}
|
||||
stats, runErr := r.Run(scanCtx, options, func(f attachment.Finding) {
|
||||
if outputErr == nil {
|
||||
outputErr = encoder.Encode(f)
|
||||
if outputErr != nil {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReconcileDoesNotValidateUnusedLogging(t *testing.T) {
|
||||
for key, value := range map[string]string{
|
||||
"DATABASE_URL_FILE": "postgres://test:test@127.0.0.1:5432/messenger?sslmode=disable",
|
||||
"S3_ACCESS_KEY_ID_FILE": "test-access",
|
||||
"S3_SECRET_ACCESS_KEY_FILE": "test-secret",
|
||||
} {
|
||||
path := filepath.Join(t.TempDir(), key)
|
||||
if err := os.WriteFile(path, []byte(value), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv(key, path)
|
||||
}
|
||||
t.Setenv("S3_ENDPOINT", "http://127.0.0.1:8333")
|
||||
t.Setenv("S3_REGION", "us-east-1")
|
||||
t.Setenv("S3_BUCKET", "attachments")
|
||||
t.Setenv("APP_LOG_LEVEL", "not-a-log-level")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // Stop before dialing: only configuration relevant to this CLI is checked.
|
||||
var out, errOut bytes.Buffer
|
||||
code := run(ctx, nil, &out, &errOut)
|
||||
if code != 1 || strings.Contains(errOut.String(), "APP_LOG_LEVEL") {
|
||||
t.Fatalf("unused logging prevented the database operation: code=%d error=%s", code, errOut.String())
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -3,10 +3,11 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/s3store"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/s3store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -20,7 +21,7 @@ func run() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s, err := s3store.New(cfg.Endpoint, cfg.Region, cfg.Bucket, cfg.AccessKey, cfg.SecretKey)
|
||||
s, err := s3store.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+32
-23
@@ -1,4 +1,4 @@
|
||||
// Command server starts the messenger API over PostgreSQL and Valkey.
|
||||
// Command server starts the messenger API over PostgreSQL, Valkey and S3.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -61,25 +61,9 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
return err
|
||||
}
|
||||
defer sessions.Close()
|
||||
var blobs attachment.Blobs
|
||||
var reconciler *attachment.Reconciler
|
||||
if cfg.S3.Endpoint != "" {
|
||||
blobs, err = s3store.New(cfg.S3.Endpoint, cfg.S3.Region, cfg.S3.Bucket, cfg.S3.AccessKey, cfg.S3.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reconCfg, e := config.LoadS3(config.ReconcileS3(os.LookupEnv))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
reconBlobs, e := s3store.New(reconCfg.Endpoint, reconCfg.Region, reconCfg.Bucket, reconCfg.AccessKey, reconCfg.SecretKey)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
reconciler, err = attachment.NewReconciler(pool, reconBlobs, time.Hour)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
blobs, reconciler, err := newAttachmentStorage(pool, cfg.S3, os.LookupEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
@@ -100,8 +84,10 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
Blobs: blobs,
|
||||
MaxUploadBytes: cfg.S3.MaxBytes,
|
||||
MaxImagePixels: cfg.S3.MaxPixels,
|
||||
AttachmentsCfg: attachment.Config{
|
||||
MaxBytes: cfg.S3.MaxBytes,
|
||||
MaxPixels: cfg.S3.MaxPixels,
|
||||
},
|
||||
})
|
||||
var debugLn net.Listener
|
||||
if cfg.DebugAddr != "" {
|
||||
@@ -124,7 +110,7 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
return
|
||||
case <-ticker.C:
|
||||
scan, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
||||
stats, err := reconciler.Run(scan, true, false, nil)
|
||||
stats, err := reconciler.Run(scan, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Warn("attachment cleanup incomplete", "error", err)
|
||||
@@ -149,6 +135,29 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
return err
|
||||
}
|
||||
|
||||
func newAttachmentStorage(pool *pgxpool.Pool, cfg config.S3, lookup config.Lookup) (attachment.Blobs, *attachment.Reconciler, error) {
|
||||
if cfg.Endpoint == "" {
|
||||
return nil, nil, nil
|
||||
}
|
||||
blobs, err := s3store.New(cfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconCfg, err := config.LoadS3(config.ReconcileS3(lookup))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconBlobs, err := s3store.New(reconCfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconciler, err := attachment.NewReconciler(pool, reconBlobs, time.Hour)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return blobs, reconciler, nil
|
||||
}
|
||||
|
||||
// publishDebugVars adds to expvar what the load tests read from a replica.
|
||||
func publishDebugVars(alias string, pool *pgxpool.Pool) {
|
||||
expvar.NewString("instance").Set(alias)
|
||||
|
||||
Reference in New Issue
Block a user