Skip to content

Commit

Permalink
PublishPage: Fix problem with nested transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Apr 11, 2024
1 parent f8dd304 commit 16adccd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
29 changes: 21 additions & 8 deletions internal/db/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/carlmjohnson/be"
"github.com/carlmjohnson/be/testfile"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/github"
Expand Down Expand Up @@ -317,9 +318,12 @@ func TestServicePublish(t *testing.T) {
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning := svc.PublishPage(ctx, p1)
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
err, warning := svc.PublishPage(ctx, txq, p1)
be.NilErr(t, warning)
return err
})
be.NilErr(t, err)
be.NilErr(t, warning)

p, err = svc.Queries.GetPageByFilePath(ctx, path1)
be.NilErr(t, err)
Expand Down Expand Up @@ -354,9 +358,12 @@ func TestServicePublish(t *testing.T) {
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning := svc.PublishPage(ctx, p2)
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
err, warning := svc.PublishPage(ctx, txq, p2)
be.NilErr(t, warning)
return err
})
be.Nonzero(t, err)
be.NilErr(t, warning)
_, err = os.Stat(filepath.Join(tmp, path2))
be.Nonzero(t, err)

Expand All @@ -377,9 +384,12 @@ func TestServicePublish(t *testing.T) {
SourceID: "",
PublicationDate: pgtype.Timestamptz{},
}
err, warning = svc.PublishPage(ctx, p3)
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
err, warning := svc.PublishPage(ctx, txq, p3)
be.NilErr(t, warning)
return err
})
be.NilErr(t, err)
be.NilErr(t, warning)
_, err = os.Stat(filepath.Join(tmp, path2))
be.NilErr(t, err)
}
Expand Down Expand Up @@ -415,9 +425,12 @@ func TestServicePublish(t *testing.T) {
svc.ContentStore = github.ErrorClient{
Error: errors.New("bad client"),
}
err, warning := svc.PublishPage(ctx, p4)
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
err, warning := svc.PublishPage(ctx, txq, p4)
be.NilErr(t, warning)
return err
})
be.Nonzero(t, err)
be.NilErr(t, warning)

p, err := svc.Queries.GetPageByFilePath(ctx, path3)
be.NilErr(t, err)
Expand Down
50 changes: 23 additions & 27 deletions pkg/almanack/service-page.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spotlightpa/almanack/pkg/almlog"
)

func (svc Services) PublishPage(ctx context.Context, page *db.Page) (err, warning error) {
func (svc Services) PublishPage(ctx context.Context, txq *db.Queries, page *db.Page) (err, warning error) {
defer errorx.Trace(&err)

page.SetURLPath()
Expand All @@ -40,30 +40,26 @@ func (svc Services) PublishPage(ctx context.Context, page *db.Page) (err, warnin
// If all this goes well, swap in the db.Page to the pointer
var p2 db.Page
err = flowmatic.Do(
func() error {
return svc.Tx.Begin(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadUncommitted,
}, 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() (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)
Expand Down Expand Up @@ -94,16 +90,16 @@ func (svc Services) RefreshPageFromContentStore(ctx context.Context, page *db.Pa

func (svc Services) PopScheduledPages(ctx context.Context) (err, warning error) {
var warnings []error
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(q *db.Queries) (txerr error) {
err = svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
defer errorx.Trace(&txerr)

pages, txerr := q.PopScheduledPages(ctx)
pages, txerr := txq.PopScheduledPages(ctx)
if txerr != nil {
return
}
var errs []error
for _, page := range pages {
txerr, warning = svc.PublishPage(ctx, &page)
txerr, warning = svc.PublishPage(ctx, txq, &page)
errs = append(errs, txerr)
warnings = append(warnings, warning)
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/api/routes-spotlightpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/carlmjohnson/emailx"
"github.com/carlmjohnson/flowmatic"
"github.com/carlmjohnson/resperr"
"github.com/jackc/pgx/v5"
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/gdocs"
"github.com/spotlightpa/almanack/internal/google"
Expand Down Expand Up @@ -555,10 +556,13 @@ func (app *appEnv) postPage(w http.ResponseWriter, r *http.Request) {
shouldPublish := res.ShouldPublish()
shouldNotify := res.ShouldNotify(&oldPage)
if shouldPublish {
err, warning := app.svc.PublishPage(ctx, &res)
if warning != nil {
app.logErr(r.Context(), warning)
}
err = app.svc.Tx.Begin(ctx, pgx.TxOptions{}, func(txq *db.Queries) (txerr error) {
err, warning := app.svc.PublishPage(ctx, txq, &res)
if warning != nil {
app.logErr(r.Context(), warning)
}
return err
})
if err != nil {
err = fmt.Errorf("postPage publish problem: %w", err)
app.replyErr(w, r, err)
Expand Down

0 comments on commit 16adccd

Please sign in to comment.