|
| 1 | +package activities |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/jackc/pgx/v5/pgtype" |
| 9 | + "github.com/jackc/pgx/v5/pgxpool" |
| 10 | + "go.opentelemetry.io/otel/metric" |
| 11 | + "go.temporal.io/sdk/temporal" |
| 12 | + |
| 13 | + "github.com/speakeasy-api/gram/server/internal/attr" |
| 14 | + "github.com/speakeasy-api/gram/server/internal/functions" |
| 15 | + funcrepo "github.com/speakeasy-api/gram/server/internal/functions/repo" |
| 16 | + "github.com/speakeasy-api/gram/server/internal/oops" |
| 17 | +) |
| 18 | + |
| 19 | +type ReapFlyAppsRequest struct { |
| 20 | + Scope FunctionsReaperScope |
| 21 | + |
| 22 | + ProjectID uuid.NullUUID |
| 23 | +} |
| 24 | + |
| 25 | +type ReapFlyAppsResult struct { |
| 26 | + Reaped int |
| 27 | + Errors int |
| 28 | +} |
| 29 | + |
| 30 | +type ReapFlyApps struct { |
| 31 | + logger *slog.Logger |
| 32 | + metrics *metrics |
| 33 | + db *pgxpool.Pool |
| 34 | + deployer functions.Deployer |
| 35 | + keepCount int64 |
| 36 | +} |
| 37 | + |
| 38 | +func NewReapFlyApps( |
| 39 | + logger *slog.Logger, |
| 40 | + meterProvider metric.MeterProvider, |
| 41 | + db *pgxpool.Pool, |
| 42 | + deployer functions.Deployer, |
| 43 | + keepCount int64, |
| 44 | +) *ReapFlyApps { |
| 45 | + return &ReapFlyApps{ |
| 46 | + logger: logger.With(attr.SlogComponent("flyio-reaper")), |
| 47 | + metrics: newMetrics(newMeter(meterProvider), logger), |
| 48 | + db: db, |
| 49 | + deployer: deployer, |
| 50 | + keepCount: keepCount, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (r *ReapFlyApps) Do(ctx context.Context, req ReapFlyAppsRequest) (*ReapFlyAppsResult, error) { |
| 55 | + logger := r.logger |
| 56 | + |
| 57 | + switch { |
| 58 | + case req.Scope == FunctionsReaperScopeProject && req.ProjectID.UUID == uuid.Nil: |
| 59 | + return nil, temporal.NewApplicationErrorWithOptions("project ID must be set for project-scoped reaper", "reaper_error", temporal.ApplicationErrorOptions{ |
| 60 | + NonRetryable: true, |
| 61 | + Cause: nil, |
| 62 | + }) |
| 63 | + case req.Scope == FunctionsReaperScopeGlobal && req.ProjectID.UUID != uuid.Nil: |
| 64 | + return nil, temporal.NewApplicationErrorWithOptions("project ID must not be set for global reaper", "reaper_error", temporal.ApplicationErrorOptions{ |
| 65 | + NonRetryable: true, |
| 66 | + Cause: nil, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + repo := funcrepo.New(r.db) |
| 71 | + |
| 72 | + // Get all apps that should be reaped (keeping only the most recent N per project) |
| 73 | + appsToReap, err := repo.GetFlyAppsToReap(ctx, funcrepo.GetFlyAppsToReapParams{ |
| 74 | + KeepCount: pgtype.Int8{Int64: r.keepCount, Valid: true}, |
| 75 | + // Starting with a small batch size for now and we'll increase later on |
| 76 | + // after some observation. |
| 77 | + BatchSize: pgtype.Int8{Int64: 20, Valid: true}, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return nil, oops.E(oops.CodeUnexpected, err, "failed to query apps to reap").Log(ctx, logger) |
| 81 | + } |
| 82 | + |
| 83 | + if len(appsToReap) == 0 { |
| 84 | + logger.InfoContext(ctx, "no apps to reap") |
| 85 | + return &ReapFlyAppsResult{ |
| 86 | + Reaped: 0, |
| 87 | + Errors: 0, |
| 88 | + }, nil |
| 89 | + } |
| 90 | + |
| 91 | + result := &ReapFlyAppsResult{ |
| 92 | + Reaped: 0, |
| 93 | + Errors: 0, |
| 94 | + } |
| 95 | + |
| 96 | + for _, app := range appsToReap { |
| 97 | + appLogger := logger.With( |
| 98 | + attr.SlogFlyAppInternalID(app.ID.String()), |
| 99 | + attr.SlogFlyAppName(app.AppName), |
| 100 | + attr.SlogFlyOrgSlug(app.FlyOrgSlug), |
| 101 | + attr.SlogProjectID(app.ProjectID.String()), |
| 102 | + attr.SlogDeploymentID(app.DeploymentID.String()), |
| 103 | + attr.SlogDeploymentFunctionsID(app.FunctionID.String()), |
| 104 | + ) |
| 105 | + |
| 106 | + appLogger.InfoContext(ctx, "reaping fly app") |
| 107 | + |
| 108 | + if err := r.deployer.Reap(ctx, functions.ReapRequest{ |
| 109 | + ProjectID: app.ProjectID, |
| 110 | + DeploymentID: app.DeploymentID, |
| 111 | + FunctionID: app.FunctionID, |
| 112 | + }); err != nil { |
| 113 | + appLogger.ErrorContext(ctx, "failed to reap app", attr.SlogError(err)) |
| 114 | + result.Errors++ |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + result.Reaped++ |
| 119 | + appLogger.InfoContext(ctx, "successfully reaped fly app") |
| 120 | + } |
| 121 | + |
| 122 | + r.metrics.RecordFlyAppReaperReapCount(ctx, int64(result.Reaped), int64(result.Errors)) |
| 123 | + |
| 124 | + return result, nil |
| 125 | +} |
0 commit comments