Skip to content

Commit dfe2f89

Browse files
committed
Track and display R2 upload duration in build timings
1 parent f3329bb commit dfe2f89

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

cmd/wpcomposer/cmd/deploy.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,35 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13+
// deployR2SyncSeconds captures the last SyncToR2 duration for the current deploy run.
14+
var deployR2SyncSeconds *int
15+
1316
var deployCmd = &cobra.Command{
1417
Use: "deploy [build-id]",
1518
Short: "Promote a build, rollback, or cleanup old builds",
1619
Args: cobra.MaximumNArgs(1),
1720
RunE: runDeploy,
1821
}
1922

23+
func secondsPtrSince(start time.Time) *int {
24+
v := int(time.Since(start).Seconds())
25+
return &v
26+
}
27+
28+
func syncToR2Timed(cmd *cobra.Command, buildDir, buildID, previousBuildID string) error {
29+
started := time.Now()
30+
err := deploy.SyncToR2(cmd.Context(), application.Config.R2, buildDir, buildID, previousBuildID, application.Logger)
31+
deployR2SyncSeconds = secondsPtrSince(started)
32+
if err != nil {
33+
return fmt.Errorf("R2 sync failed: %w", err)
34+
}
35+
recordR2Sync(cmd, buildID)
36+
return nil
37+
}
38+
2039
func runDeploy(cmd *cobra.Command, args []string) error {
40+
deployR2SyncSeconds = nil
41+
2142
repoDir := filepath.Join("storage", "repository")
2243
cleanup, _ := cmd.Flags().GetBool("cleanup")
2344
toR2, _ := cmd.Flags().GetBool("to-r2")
@@ -78,10 +99,9 @@ func runDeploy(cmd *cobra.Command, args []string) error {
7899

79100
// Sync to R2 first, then promote locally
80101
if toR2 || application.Config.R2.Enabled {
81-
if err := deploy.SyncToR2(cmd.Context(), application.Config.R2, buildDir, target, previousBuildID, application.Logger); err != nil {
82-
return fmt.Errorf("R2 sync failed: %w", err)
102+
if err := syncToR2Timed(cmd, buildDir, target, previousBuildID); err != nil {
103+
return err
83104
}
84-
recordR2Sync(cmd, target)
85105
}
86106

87107
if _, err := deploy.Rollback(repoDir, target, application.Logger); err != nil {
@@ -110,10 +130,9 @@ func runDeploy(cmd *cobra.Command, args []string) error {
110130

111131
// Sync to R2 first, then promote locally
112132
if toR2 || application.Config.R2.Enabled {
113-
if err := deploy.SyncToR2(cmd.Context(), application.Config.R2, buildDir, buildID, previousBuildID, application.Logger); err != nil {
114-
return fmt.Errorf("R2 sync failed: %w", err)
133+
if err := syncToR2Timed(cmd, buildDir, buildID, previousBuildID); err != nil {
134+
return err
115135
}
116-
recordR2Sync(cmd, buildID)
117136
}
118137

119138
if err := deploy.Promote(repoDir, buildID, application.Logger); err != nil {

cmd/wpcomposer/cmd/pipeline.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ func runPipeline(cmd *cobra.Command, args []string) error {
108108
d := pipelineStepDurations
109109
_, dbErr := application.DB.ExecContext(ctx, `
110110
UPDATE builds SET status = 'completed', finished_at = ?, duration_seconds = ?,
111-
discover_seconds = ?, update_seconds = ?, build_seconds = ?, deploy_seconds = ?
111+
discover_seconds = ?, update_seconds = ?, build_seconds = ?, deploy_seconds = ?,
112+
r2_upload_seconds = ?
112113
WHERE id = ?`,
113114
now.Format(time.RFC3339),
114115
int(now.Sub(started).Seconds()),
115-
d.Discover, d.Update, d.Build, d.Deploy,
116+
d.Discover, d.Update, d.Build, d.Deploy, d.R2Upload,
116117
buildID,
117118
)
118119
if dbErr != nil {
@@ -129,6 +130,7 @@ type stepDurations struct {
129130
Update *int
130131
Build *int
131132
Deploy *int
133+
R2Upload *int
132134
}
133135

134136
// pipelineStepDurations is set by executePipelineSteps so that recordFailedBuild
@@ -171,10 +173,12 @@ func executePipelineSteps(cmd *cobra.Command, ctx context.Context, skipDiscover,
171173
application.Logger.Info("pipeline: running deploy")
172174
deployCmd.SetContext(ctx)
173175
stepStart = time.Now()
174-
if err := runDeploy(deployCmd, nil); err != nil {
176+
err := runDeploy(deployCmd, nil)
177+
pipelineStepDurations.Deploy = intPtr(int(time.Since(stepStart).Seconds()))
178+
pipelineStepDurations.R2Upload = deployR2SyncSeconds
179+
if err != nil {
175180
return fmt.Errorf("deploy: %w", err)
176181
}
177-
pipelineStepDurations.Deploy = intPtr(int(time.Since(stepStart).Seconds()))
178182

179183
// Clean up old builds after a successful deploy.
180184
repoDir := filepath.Join("storage", "repository")
@@ -202,12 +206,12 @@ func recordFailedBuild(cmd *cobra.Command, started time.Time, pipelineErr error)
202206
_, dbErr := application.DB.ExecContext(cmd.Context(), `
203207
UPDATE builds SET status = 'failed', finished_at = ?, duration_seconds = ?,
204208
error_message = ?, discover_seconds = ?, update_seconds = ?,
205-
build_seconds = ?, deploy_seconds = ?
209+
build_seconds = ?, deploy_seconds = ?, r2_upload_seconds = ?
206210
WHERE id = ?`,
207211
now.Format(time.RFC3339),
208212
int(now.Sub(started).Seconds()),
209213
pipelineErr.Error(),
210-
d.Discover, d.Update, d.Build, d.Deploy,
214+
d.Discover, d.Update, d.Build, d.Deploy, d.R2Upload,
211215
pipelineBuildID,
212216
)
213217
if dbErr != nil {

internal/http/handlers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,12 +959,13 @@ type buildRow struct {
959959
UpdateSeconds *int
960960
BuildSeconds *int
961961
DeploySeconds *int
962+
R2UploadSeconds *int
962963
}
963964

964965
func queryBuilds(ctx context.Context, db *sql.DB) ([]buildRow, error) {
965966
rows, err := db.QueryContext(ctx, `SELECT id, started_at, packages_total, packages_changed,
966967
artifact_count, status, COALESCE(r2_synced_at, ''), COALESCE(error_message, ''),
967-
duration_seconds, discover_seconds, update_seconds, build_seconds, deploy_seconds
968+
duration_seconds, discover_seconds, update_seconds, build_seconds, deploy_seconds, r2_upload_seconds
968969
FROM builds ORDER BY started_at DESC LIMIT 50`)
969970
if err != nil {
970971
return nil, err
@@ -976,7 +977,7 @@ func queryBuilds(ctx context.Context, db *sql.DB) ([]buildRow, error) {
976977
var b buildRow
977978
_ = rows.Scan(&b.ID, &b.StartedAt, &b.PackagesTotal, &b.PackagesChanged,
978979
&b.ArtifactCount, &b.Status, &b.R2SyncedAt, &b.ErrorMessage,
979-
&b.DurationSeconds, &b.DiscoverSeconds, &b.UpdateSeconds, &b.BuildSeconds, &b.DeploySeconds)
980+
&b.DurationSeconds, &b.DiscoverSeconds, &b.UpdateSeconds, &b.BuildSeconds, &b.DeploySeconds, &b.R2UploadSeconds)
980981
builds = append(builds, b)
981982
}
982983
return builds, rows.Err()

internal/http/templates/admin_builds.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ <h1 class="text-xl font-bold">Build History</h1>
4646
</td>
4747
<td class="px-4 py-2 text-xs font-mono whitespace-nowrap">
4848
{{if .DurationSeconds}}<span class="text-gray-700">{{formatDuration .DurationSeconds}}</span>{{end}}
49-
{{if or .DiscoverSeconds .UpdateSeconds .BuildSeconds .DeploySeconds}}
50-
<span class="text-gray-400 block mt-0.5">{{- if .DiscoverSeconds}}D:{{formatDuration .DiscoverSeconds}} {{end}}{{- if .UpdateSeconds}}U:{{formatDuration .UpdateSeconds}} {{end}}{{- if .BuildSeconds}}B:{{formatDuration .BuildSeconds}} {{end}}{{- if .DeploySeconds}}Dp:{{formatDuration .DeploySeconds}}{{end}}</span>
49+
{{if or .DiscoverSeconds .UpdateSeconds .BuildSeconds .DeploySeconds .R2UploadSeconds}}
50+
<span class="text-gray-400 block mt-0.5">{{- if .DiscoverSeconds}}D:{{formatDuration .DiscoverSeconds}} {{end}}{{- if .UpdateSeconds}}U:{{formatDuration .UpdateSeconds}} {{end}}{{- if .BuildSeconds}}B:{{formatDuration .BuildSeconds}} {{end}}{{- if .DeploySeconds}}Dp:{{formatDuration .DeploySeconds}} {{end}}{{- if .R2UploadSeconds}}R2:{{formatDuration .R2UploadSeconds}}{{end}}</span>
5151
{{end}}
5252
</td>
5353
<td class="px-4 py-2">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- +goose Up
2+
ALTER TABLE builds ADD COLUMN r2_upload_seconds INTEGER;
3+
4+
-- +goose Down
5+
ALTER TABLE builds DROP COLUMN r2_upload_seconds;

0 commit comments

Comments
 (0)