Skip to content

Commit

Permalink
Only wrap the release creation in a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mwildehahn committed Jul 12, 2016
1 parent 5ef2290 commit c6441b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (s *configsService) Set(ctx context.Context, db *gorm.DB, opts SetOpts) (*C
}

// Create new release based on new config and old slug
_, err = s.releases.Create(ctx, db, &Release{
_, err = s.releases.CreateAndRelease(ctx, db, &Release{
App: release.App,
Config: c,
Slug: release.Slug,
Expand Down
14 changes: 5 additions & 9 deletions deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type deployerService struct {
*Empire
}

// deploy does the actual deployment
func (s *deployerService) deploy(ctx context.Context, db *gorm.DB, ss scheduler.StatusStream, opts DeployOpts) (*Release, error) {
// createRelease creates a new release that can be deployed
func (s *deployerService) createRelease(ctx context.Context, db *gorm.DB, ss scheduler.StatusStream, opts DeployOpts) (*Release, error) {
app, img := opts.App, opts.Image

// If no app is specified, attempt to find the app that relates to this
Expand Down Expand Up @@ -58,24 +58,21 @@ func (s *deployerService) deploy(ctx context.Context, db *gorm.DB, ss scheduler.
Config: config,
Slug: slug,
Description: desc,
}, ss)

})
return r, err
}

func (s *deployerService) deployInTransaction(ctx context.Context, stream scheduler.StatusStream, opts DeployOpts) (*Release, error) {
tx := s.db.Begin()
r, err := s.deploy(ctx, tx, stream, opts)
r, err := s.createRelease(ctx, tx, stream, opts)
if err != nil {
tx.Rollback()
return r, err
}

if err := tx.Commit().Error; err != nil {
return r, err
}

return r, err
return r, s.releases.Release(ctx, r, stream)
}

// Deploy is a thin wrapper around deploy to that adds the error to the
Expand All @@ -94,6 +91,5 @@ func (s *deployerService) Deploy(ctx context.Context, opts DeployOpts) (*Release
if err := json.NewEncoder(opts.Output).Encode(&msg); err != nil {
return r, err
}

return r, err
}
23 changes: 14 additions & 9 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,18 @@ type releasesService struct {
*Empire
}

// Create creates a new release then submits it to the scheduler.
func (s *releasesService) Create(ctx context.Context, db *gorm.DB, r *Release, ss scheduler.StatusStream) (*Release, error) {
// CreateAndRelease creates a new release then submits it to the scheduler.
func (s *releasesService) CreateAndRelease(ctx context.Context, db *gorm.DB, r *Release, ss scheduler.StatusStream) (*Release, error) {
r, err := s.Create(ctx, db, r)
if err != nil {
return r, err
}
// Schedule the new release onto the cluster.
return r, s.Release(ctx, r, ss)
}

// Create creates a new release.
func (s *releasesService) Create(ctx context.Context, db *gorm.DB, r *Release) (*Release, error) {
// Lock all releases for the given application to ensure that the
// release version is updated automically.
if err := db.Exec(`select 1 from releases where app_id = ? for update`, r.App.ID).Error; err != nil {
Expand All @@ -122,12 +132,7 @@ func (s *releasesService) Create(ctx context.Context, db *gorm.DB, r *Release, s
}

r, err := releasesCreate(db, r)
if err != nil {
return r, err
}

// Schedule the new release onto the cluster.
return r, s.Release(ctx, r, ss)
return r, err
}

// Rolls back to a specific release version.
Expand All @@ -140,7 +145,7 @@ func (s *releasesService) Rollback(ctx context.Context, db *gorm.DB, opts Rollba

desc := fmt.Sprintf("Rollback to v%d", version)
desc = appendMessageToDescription(desc, opts.User, opts.Message)
return s.Create(ctx, db, &Release{
return s.CreateAndRelease(ctx, db, &Release{
App: app,
Config: r.Config,
Slug: r.Slug,
Expand Down
4 changes: 4 additions & 0 deletions scheduler/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package scheduler
import (
"fmt"
"io"
"sync"

"github.com/remind101/pkg/timex"
"golang.org/x/net/context"
)

type FakeScheduler struct {
sync.Mutex
apps map[string]*App
}

Expand All @@ -19,6 +21,8 @@ func NewFakeScheduler() *FakeScheduler {
}

func (m *FakeScheduler) Submit(ctx context.Context, app *App, ss StatusStream) error {
m.Lock()
defer m.Unlock()
m.apps[app.ID] = app
return nil
}
Expand Down

0 comments on commit c6441b7

Please sign in to comment.