main.go97 lines, 2.7 KB
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "cmp" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "log/slog" |
| 9 | "os" |
| 10 | "os/signal" |
| 11 | "strconv" |
| 12 | "syscall" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/fiston-user/murmur/internal/jobs" |
| 16 | "github.com/fiston-user/murmur/internal/likes" |
| 17 | "github.com/fiston-user/murmur/internal/notify" |
| 18 | "github.com/fiston-user/murmur/internal/platform/health" |
| 19 | "github.com/fiston-user/murmur/internal/platform/metrics" |
| 20 | "github.com/fiston-user/murmur/internal/platform/postgres" |
| 21 | "github.com/fiston-user/murmur/internal/platform/redisx" |
| 22 | "github.com/fiston-user/murmur/internal/timeline" |
| 23 | ) |
| 24 | |
| 25 | func main() { |
| 26 | slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))) |
| 27 | private := cmp.Or(os.Getenv("PRIVATE_ADDR"), ":9101") |
| 28 | if len(os.Args) > 1 && os.Args[1] == "ready" { |
| 29 | if err := health.Probe(private); err != nil { |
| 30 | fmt.Fprintln(os.Stderr, err) |
| 31 | os.Exit(1) |
| 32 | } |
| 33 | return |
| 34 | } |
| 35 | if err := run(private); err != nil { |
| 36 | slog.Error("worker stopped", "error", err) |
| 37 | os.Exit(1) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func run(private string) error { |
| 42 | ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 43 | defer stop() |
| 44 | |
| 45 | url := os.Getenv("DATABASE_URL") |
| 46 | if url == "" { |
| 47 | return errors.New("DATABASE_URL is not set") |
| 48 | } |
| 49 | workers, err := strconv.Atoi(cmp.Or(os.Getenv("WORKERS"), "4")) |
| 50 | if err != nil || workers < 1 { |
| 51 | return fmt.Errorf("WORKERS: %q is not a number of workers", os.Getenv("WORKERS")) |
| 52 | } |
| 53 | pool, err := postgres.Connect(ctx, url, workers+2) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("connect to postgres: %w", err) |
| 56 | } |
| 57 | defer pool.Close() |
| 58 | reg := metrics.New() |
| 59 | metrics.Pool(reg, pool) |
| 60 | |
| 61 | rdb, err := redisx.Connect(ctx, os.Getenv("REDIS_URL")) |
| 62 | if err != nil { |
| 63 | return fmt.Errorf("connect to redis: %w", err) |
| 64 | } |
| 65 | defer rdb.Close() |
| 66 | metrics.Redis(reg, rdb) |
| 67 | big, err := strconv.Atoi(cmp.Or(os.Getenv("BIG_ACCOUNT"), "1000")) |
| 68 | if err != nil { |
| 69 | return fmt.Errorf("BIG_ACCOUNT: %w", err) |
| 70 | } |
| 71 | timelines := timeline.New(pool, rdb, big) |
| 72 | timeout, err := time.ParseDuration(cmp.Or(os.Getenv("JOB_TIMEOUT"), "1m")) |
| 73 | if err != nil { |
| 74 | return fmt.Errorf("JOB_TIMEOUT: %w", err) |
| 75 | } |
| 76 | grace, err := time.ParseDuration(cmp.Or(os.Getenv("JOB_GRACE"), "20s")) |
| 77 | if err != nil { |
| 78 | return fmt.Errorf("JOB_GRACE: %w", err) |
| 79 | } |
| 80 | |
| 81 | w := jobs.NewWorker(pool) |
| 82 | w.Ended = metrics.Jobs(reg) |
| 83 | w.Handle(timeline.FanOut, timelines.RunFanOut) |
| 84 | w.Handle(notify.Like, notify.New(pool, rdb).RunLike) |
| 85 | go likes.FoldEvery(ctx, pool, time.Second) |
| 86 | hl := health.New(pool.Ping) |
| 87 | go func() { |
| 88 | if err := metrics.Serve(ctx, private, reg, hl.Routes); err != nil { |
| 89 | slog.Error("the private port is closed", "error", err) |
| 90 | stop() |
| 91 | } |
| 92 | }() |
| 93 | slog.Info("working", "workers", workers, "connections", pool.Config().MaxConns, "private", private) |
| 94 | w.Run(ctx, workers, time.Second, timeout, grace) |
| 95 | slog.Info("shutting down") |
| 96 | return nil |
| 97 | } |