|
| 1 | +package activities |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + |
| 7 | + "github.com/jackc/pgx/v5/pgtype" |
| 8 | + "github.com/jackc/pgx/v5/pgxpool" |
| 9 | + "go.opentelemetry.io/otel/metric" |
| 10 | + |
| 11 | + "github.com/speakeasy-api/gram/server/internal/attr" |
| 12 | + "github.com/speakeasy-api/gram/server/internal/functions" |
| 13 | + funcrepo "github.com/speakeasy-api/gram/server/internal/functions/repo" |
| 14 | + "github.com/speakeasy-api/gram/server/internal/oops" |
| 15 | +) |
| 16 | + |
| 17 | +type ReapFlyAppsResult struct { |
| 18 | + Reaped int |
| 19 | + Errors int |
| 20 | +} |
| 21 | + |
| 22 | +type ReapFlyApps struct { |
| 23 | + logger *slog.Logger |
| 24 | + metrics *metrics |
| 25 | + db *pgxpool.Pool |
| 26 | + deployer functions.Deployer |
| 27 | + keepCount int64 |
| 28 | +} |
| 29 | + |
| 30 | +func NewReapFlyApps( |
| 31 | + logger *slog.Logger, |
| 32 | + meterProvider metric.MeterProvider, |
| 33 | + db *pgxpool.Pool, |
| 34 | + deployer functions.Deployer, |
| 35 | + keepCount int64, |
| 36 | +) *ReapFlyApps { |
| 37 | + return &ReapFlyApps{ |
| 38 | + logger: logger.With(attr.SlogComponent("flyio-reaper")), |
| 39 | + metrics: newMetrics(newMeter(meterProvider), logger), |
| 40 | + db: db, |
| 41 | + deployer: deployer, |
| 42 | + keepCount: keepCount, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (r *ReapFlyApps) Do(ctx context.Context) (*ReapFlyAppsResult, error) { |
| 47 | + logger := r.logger |
| 48 | + |
| 49 | + repo := funcrepo.New(r.db) |
| 50 | + |
| 51 | + // Get all apps that should be reaped (keeping only the most recent N per project) |
| 52 | + appsToReap, err := repo.GetFlyAppsToReap(ctx, pgtype.Int8{Int64: r.keepCount, Valid: true}) |
| 53 | + if err != nil { |
| 54 | + return nil, oops.E(oops.CodeUnexpected, err, "failed to query apps to reap").Log(ctx, logger) |
| 55 | + } |
| 56 | + |
| 57 | + if len(appsToReap) == 0 { |
| 58 | + logger.InfoContext(ctx, "no apps to reap") |
| 59 | + return &ReapFlyAppsResult{ |
| 60 | + Reaped: 0, |
| 61 | + Errors: 0, |
| 62 | + }, nil |
| 63 | + } |
| 64 | + |
| 65 | + result := &ReapFlyAppsResult{ |
| 66 | + Reaped: 0, |
| 67 | + Errors: 0, |
| 68 | + } |
| 69 | + |
| 70 | + for _, app := range appsToReap { |
| 71 | + appLogger := logger.With( |
| 72 | + attr.SlogFlyAppInternalID(app.ID.String()), |
| 73 | + attr.SlogFlyAppName(app.AppName), |
| 74 | + attr.SlogFlyOrgSlug(app.FlyOrgSlug), |
| 75 | + attr.SlogProjectID(app.ProjectID.String()), |
| 76 | + attr.SlogDeploymentID(app.DeploymentID.String()), |
| 77 | + attr.SlogDeploymentFunctionsID(app.FunctionID.String()), |
| 78 | + ) |
| 79 | + |
| 80 | + appLogger.InfoContext(ctx, "reaping fly app") |
| 81 | + |
| 82 | + if err := r.deployer.Reap(ctx, functions.ReapRequest{ |
| 83 | + ProjectID: app.ProjectID, |
| 84 | + DeploymentID: app.DeploymentID, |
| 85 | + FunctionID: app.FunctionID, |
| 86 | + }); err != nil { |
| 87 | + appLogger.ErrorContext(ctx, "failed to reap app", attr.SlogError(err)) |
| 88 | + result.Errors++ |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + result.Reaped++ |
| 93 | + appLogger.InfoContext(ctx, "successfully reaped fly app") |
| 94 | + } |
| 95 | + |
| 96 | + r.metrics.RecordFlyAppReaperReapCount(ctx, int64(result.Reaped), int64(result.Errors)) |
| 97 | + |
| 98 | + return result, nil |
| 99 | +} |
0 commit comments