ЛР 5 — асинхронная обработка и версии pipeline
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
PostgresURL string
|
||||
RedisAddr string
|
||||
}
|
||||
|
||||
type S3 struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AccessKeyID string
|
||||
SecretAccessKey string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
JobMaxAttempts int
|
||||
HTTP struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
Database Database
|
||||
|
||||
Session struct {
|
||||
TTL time.Duration
|
||||
Store string
|
||||
}
|
||||
|
||||
S3 S3
|
||||
|
||||
AppEnv string
|
||||
CacheURL string
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
config := &Config{}
|
||||
sessionTTL, err := getSessionTTL()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Session.TTL = sessionTTL
|
||||
config.JobMaxAttempts, err = EnvInt("JOB_MAX_ATTEMPTS", 5, 1, 20)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.HTTP.Host = os.Getenv("APP_HOST")
|
||||
if config.HTTP.Host == "" {
|
||||
config.HTTP.Host = "0.0.0.0"
|
||||
}
|
||||
|
||||
config.HTTP.Port = os.Getenv("APP_PORT")
|
||||
if config.HTTP.Port == "" {
|
||||
config.HTTP.Port = "8080"
|
||||
}
|
||||
|
||||
config.AppEnv = os.Getenv("APP_ENV")
|
||||
if config.AppEnv == "" {
|
||||
config.AppEnv = "lab"
|
||||
}
|
||||
|
||||
store := os.Getenv("SESSION_STORE")
|
||||
|
||||
config.Session.Store = strings.TrimSpace(string(store))
|
||||
if config.Session.Store == "" {
|
||||
return nil, fmt.Errorf("empty session store data")
|
||||
}
|
||||
|
||||
config.Database, err = getDatabaseURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.S3, err = getS3Data()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
|
||||
}
|
||||
|
||||
func getSessionTTL() (time.Duration, error) {
|
||||
value, ok := os.LookupEnv("SESSION_TTL_SECONDS")
|
||||
if !ok {
|
||||
return time.Hour, nil
|
||||
}
|
||||
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/int64(time.Second)) {
|
||||
return 0, fmt.Errorf("invalid SESSION_TTL_SECONDS: %q", value)
|
||||
}
|
||||
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
|
||||
func LoadAttachmentCleanup() (postgresURL string, objectStorage S3, err error) {
|
||||
postgresURL, err = LoadPostgresURL()
|
||||
if err != nil {
|
||||
return "", S3{}, err
|
||||
}
|
||||
objectStorage, err = getS3Data()
|
||||
return postgresURL, objectStorage, err
|
||||
}
|
||||
|
||||
func LoadPostgresURL() (string, error) {
|
||||
postgresData, err := os.ReadFile(os.Getenv("DATABASE_URL_FILE"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read database URL: %w", err)
|
||||
}
|
||||
postgresURL := strings.TrimSpace(string(postgresData))
|
||||
if postgresURL == "" {
|
||||
return "", fmt.Errorf("empty database URL")
|
||||
}
|
||||
return postgresURL, nil
|
||||
}
|
||||
|
||||
func getDatabaseURL() (Database, error) {
|
||||
postgresURL, err := LoadPostgresURL()
|
||||
if err != nil {
|
||||
return Database{}, err
|
||||
}
|
||||
db := Database{PostgresURL: postgresURL}
|
||||
|
||||
redisData := os.Getenv("REDIS_ADDR")
|
||||
|
||||
db.RedisAddr = strings.TrimSpace(string(redisData))
|
||||
|
||||
if db.RedisAddr == "" {
|
||||
return db, fmt.Errorf("redis addres is empty")
|
||||
}
|
||||
|
||||
return db, nil
|
||||
|
||||
}
|
||||
|
||||
func getS3Data() (S3, error) {
|
||||
s3 := S3{
|
||||
Endpoint: os.Getenv("S3_ENDPOINT"),
|
||||
Region: os.Getenv("S3_REGION"),
|
||||
Bucket: os.Getenv("S3_BUCKET"),
|
||||
}
|
||||
|
||||
if s3.Endpoint == "" {
|
||||
return s3, fmt.Errorf("empty s3 endpoint")
|
||||
}
|
||||
if s3.Region == "" {
|
||||
return s3, fmt.Errorf("empty s3 region")
|
||||
}
|
||||
if s3.Bucket == "" {
|
||||
return s3, fmt.Errorf("emtpy s3 bucket")
|
||||
}
|
||||
accessKeyIDPath := os.Getenv("S3_ACCESS_KEY_ID_FILE")
|
||||
|
||||
accessKeyID, err := os.ReadFile(accessKeyIDPath)
|
||||
if err != nil {
|
||||
return s3, fmt.Errorf("empty s3 access_key_id")
|
||||
}
|
||||
s3.AccessKeyID = strings.TrimSpace(string(accessKeyID))
|
||||
|
||||
secretAccessKeyPath := os.Getenv("S3_SECRET_ACCESS_KEY_FILE")
|
||||
secretAccessKey, err := os.ReadFile(secretAccessKeyPath)
|
||||
if err != nil {
|
||||
return s3, fmt.Errorf("empty s3 secret_access_key_file")
|
||||
}
|
||||
s3.SecretAccessKey = strings.TrimSpace(string(secretAccessKey))
|
||||
|
||||
return s3, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pipeline struct {
|
||||
BrokerURL string
|
||||
Concurrency int
|
||||
Prefetch int
|
||||
Timeout time.Duration
|
||||
Lease time.Duration
|
||||
Retention time.Duration
|
||||
CleanupGrace time.Duration
|
||||
CleanupPeriod time.Duration
|
||||
RetryBase time.Duration
|
||||
}
|
||||
|
||||
func EnvInt(name string, fallback, minimum, maximum int) (int, error) {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
n, err := strconv.Atoi(value)
|
||||
if err != nil || n < minimum || n > maximum {
|
||||
return 0, fmt.Errorf("%s must be between %d and %d", name, minimum, maximum)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func LoadPipeline() (Pipeline, error) {
|
||||
var cfg Pipeline
|
||||
path := os.Getenv("BROKER_URL_FILE")
|
||||
if path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return cfg, fmt.Errorf("cannot read broker URL file: %w", err)
|
||||
}
|
||||
cfg.BrokerURL = strings.TrimSpace(string(data))
|
||||
}
|
||||
duration := func(dst *time.Duration, name string, fallback, minimum, maximum int) error {
|
||||
n, err := EnvInt(name, fallback, minimum, maximum)
|
||||
*dst = time.Duration(n) * time.Second
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
if cfg.Concurrency, err = EnvInt("WORKER_CONCURRENCY", 1, 1, 4); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
if cfg.Prefetch, err = EnvInt("WORKER_PREFETCH", 1, 1, 16); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
for _, d := range []struct {
|
||||
dst *time.Duration
|
||||
name string
|
||||
fallback, minimum, maximum int
|
||||
}{
|
||||
{&cfg.Timeout, "JOB_TIMEOUT_SECONDS", 60, 1, 600},
|
||||
{&cfg.Lease, "JOB_LEASE_SECONDS", 90, 2, 900},
|
||||
{&cfg.Retention, "STAGING_RETENTION_SECONDS", 86400, 1, 604800},
|
||||
{&cfg.CleanupGrace, "CLEANUP_GRACE_SECONDS", 600, 1, 604800},
|
||||
{&cfg.CleanupPeriod, "CLEANUP_INTERVAL_SECONDS", 5, 1, 600},
|
||||
{&cfg.RetryBase, "JOB_RETRY_BASE_SECONDS", 2, 1, 15},
|
||||
} {
|
||||
if err := duration(d.dst, d.name, d.fallback, d.minimum, d.maximum); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
}
|
||||
if cfg.Lease < cfg.Timeout+time.Second {
|
||||
return cfg, fmt.Errorf("JOB_LEASE_SECONDS must exceed JOB_TIMEOUT_SECONDS")
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user