Skip to content

Commit

Permalink
PublishPage: Use tx lock around page update
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Apr 8, 2024
1 parent 6704904 commit 4283445
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions pkg/almanack/service-page.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,44 @@ func (svc Services) PublishPage(ctx context.Context, q *db.Queries, page *db.Pag
if err != nil {
return
}

// Start two goroutines.
// In one, try the update while holding a lock.
// If the update succeeds, also do the GitHub publish.
// If it publishes, commit the locked update. If not, rollback.
// In the background, do the index and issue a warning if it fails.
// If all this goes well, swap in the db.Page to the pointer
var p2 db.Page
err = flowmatic.Do(
func() error {
internalID, _ := page.Frontmatter["internal-id"].(string)
title := cmp.Or(internalID, page.FilePath)
msg := fmt.Sprintf("Content: publishing %q", title)
return svc.ContentStore.UpdateFile(ctx, msg, page.FilePath, []byte(data))
}, func() error {
return svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
defer errorx.Trace(&txerr)

p2, txerr = txq.UpdatePage(ctx, db.UpdatePageParams{
FilePath: page.FilePath,
URLPath: page.URLPath.String,
SetLastPublished: true,
SetFrontmatter: false,
SetBody: false,
SetScheduleFor: false,
ScheduleFor: db.NullTime,
})
if txerr != nil {
return txerr
}

internalID, _ := page.Frontmatter["internal-id"].(string)
title := cmp.Or(internalID, page.FilePath)
msg := fmt.Sprintf("Content: publishing %q", title)
return svc.ContentStore.UpdateFile(ctx, msg, page.FilePath, []byte(data))
})
},
func() error {
_, warning = svc.Indexer.SaveObject(page.ToIndex(), ctx)
return nil
})
if err != nil {
return
}

p2, err := q.UpdatePage(ctx, db.UpdatePageParams{
FilePath: page.FilePath,
URLPath: page.URLPath.String,
SetLastPublished: true,
SetFrontmatter: false,
SetBody: false,
SetScheduleFor: false,
ScheduleFor: db.NullTime,
})
if err != nil {
return
}
*page = p2
return
}
Expand Down

0 comments on commit 4283445

Please sign in to comment.