Skip to content

Commit

Permalink
fix: revert workers, fix updateUser
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzalevski authored and moul committed Apr 26, 2021
1 parent 44422c0 commit c8d20e6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
4 changes: 4 additions & 0 deletions pkg/sgtm/db.go
Expand Up @@ -3,6 +3,7 @@ package sgtm
import (
"github.com/bwmarrin/snowflake"
"gorm.io/gorm"
"gorm.io/gorm/clause"

"moul.io/sgtm/pkg/sgtmpb"
)
Expand Down Expand Up @@ -30,3 +31,6 @@ func beforeCreate(sfn *snowflake.Node) func(*gorm.DB) {
tx.Statement.SetColumn("ID", sfn.Generate().Int64())
}
}

func (svc *Service) rodb() *gorm.DB { return svc._db }
func (svc *Service) rwdb() *gorm.DB { return svc._db.Omit(clause.Associations) }
4 changes: 2 additions & 2 deletions pkg/sgtm/page_profile.go
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/go-chi/chi"
packr "github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2"
"go.uber.org/zap"
"moul.io/sgtm/pkg/sgtmpb"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func (svc *Service) profilePage(box *packr.Box) func(w http.ResponseWriter, r *h

// tracks
{
_, data.Profile.Stats.Tracks, err = svc.storage.GetPostListByUserID(data.Profile.User.ID, 100)
data.Profile.LastTracks, data.Profile.Stats.Tracks, err = svc.storage.GetPostListByUserID(data.Profile.User.ID, 100)
if err != nil {
data.Error = "Cannot fetch last tracks: " + err.Error()
}
Expand Down
36 changes: 17 additions & 19 deletions pkg/sgtm/page_settings.go
Expand Up @@ -5,10 +5,8 @@ import (
"strings"
"time"

packr "github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2"
"go.uber.org/zap"

"moul.io/sgtm/pkg/sgtmpb"
)

func (svc *Service) settingsPage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -27,7 +25,7 @@ func (svc *Service) settingsPage(box *packr.Box) func(w http.ResponseWriter, r *
return
}
if r.Method == "POST" {
validate := func() *sgtmpb.User {
validate := func() map[string]interface{} {
if err := r.ParseForm(); err != nil {
data.Error = err.Error()
return nil
Expand All @@ -40,26 +38,26 @@ func (svc *Service) settingsPage(box *packr.Box) func(w http.ResponseWriter, r *
soundcloud = strings.TrimPrefix(soundcloud, "https://soundcloud.com/")
soundcloud = strings.TrimPrefix(soundcloud, "@")

user := &sgtmpb.User{
Firstname: strings.TrimSpace(r.Form.Get("firstname")),
Lastname: strings.TrimSpace(r.Form.Get("lastname")),
Homepage: strings.TrimSpace(r.Form.Get("homepage")),
Bio: strings.TrimSpace(r.Form.Get("bio")),
Headline: strings.TrimSpace(r.Form.Get("headline")),
Inspirations: strings.TrimSpace(r.Form.Get("inspirations")),
Gears: strings.TrimSpace(r.Form.Get("gears")),
Goals: strings.TrimSpace(r.Form.Get("goals")),
Genres: strings.TrimSpace(r.Form.Get("genres")),
OtherLinks: strings.TrimSpace(r.Form.Get("other_links")),
TwitterUsername: twitter,
SoundcloudUsername: soundcloud,
fields := map[string]interface{}{
"firstname": strings.TrimSpace(r.Form.Get("firstname")),
"lastname": strings.TrimSpace(r.Form.Get("lastname")),
"homepage": strings.TrimSpace(r.Form.Get("homepage")),
"bio": strings.TrimSpace(r.Form.Get("bio")),
"headline": strings.TrimSpace(r.Form.Get("headline")),
"inspirations": strings.TrimSpace(r.Form.Get("inspirations")),
"gears": strings.TrimSpace(r.Form.Get("gears")),
"goals": strings.TrimSpace(r.Form.Get("goals")),
"genres": strings.TrimSpace(r.Form.Get("genres")),
"other_links": strings.TrimSpace(r.Form.Get("other_links")),
"twitter_username": twitter,
"soundcloud_username": soundcloud,
}

return user
return fields
}
fields := validate()
if fields != nil {
err := svc.storage.UpdateUser(fields, fields)
err := svc.storage.UpdateUser(data.User, fields)
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
Expand Down
36 changes: 33 additions & 3 deletions pkg/sgtm/processing_worker.go
Expand Up @@ -60,12 +60,42 @@ func (svc *Service) processingLoop(i int) error {

// track migrations
{
outdated, err := svc.storage.GetOutDatedPosts(len(svc.processingWorker.trackMigrations))
if err != nil {
var outdated []*sgtmpb.Post
if err := svc.rodb().
Where(sgtmpb.Post{Kind: sgtmpb.Post_TrackKind}).
Where("processing_error IS NULL OR processing_error == ''").
Where("processing_version IS NULL OR processing_version < ?", len(svc.processingWorker.trackMigrations)).
Preload("Author").
Find(&outdated).
Error; err != nil {
return fmt.Errorf("failed to fetch tracks that need to be processed: %w", err)
}

err = svc.storage.UpdateProcessingTracks(outdated, svc.processingWorker.trackMigrations)
err := svc.rwdb().Transaction(func(tx *gorm.DB) error {
for _, entryPtr := range outdated {
entry := entryPtr
version := 1
for _, migration := range svc.processingWorker.trackMigrations {
err := migration(entry, tx)
if err != nil {
entry.ProcessingError = err.Error()
break
}
entry.ProcessingVersion = int64(version)
version++
}
if err := tx.
Model(&entry).
Updates(map[string]interface{}{
"processing_version": entry.ProcessingVersion,
"processing_error": entry.ProcessingError,
}).
Error; err != nil {
return fmt.Errorf("failed to save processing state: %w", err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to run migration: %w", err)
}
Expand Down

0 comments on commit c8d20e6

Please sign in to comment.