Skip to content

Commit

Permalink
Remove unnecessary structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejholmes committed Jan 4, 2016
1 parent 395d91f commit ec4bb79
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 104 deletions.
105 changes: 48 additions & 57 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,57 @@ type appsService struct {
*Empire
}

func (s *appsService) AppsDestroy(ctx context.Context, db *gorm.DB, app *App) error {
if err := s.Scheduler.Remove(ctx, app.ID); err != nil {
// Destroy destroys removes an app from the scheduler, then destroys it here.
func (s *appsService) Destroy(ctx context.Context, db *gorm.DB, app *App) error {
if err := appsDestroy(db, app); err != nil {
return err
}

return appsDestroy(db, app)
return s.Scheduler.Remove(ctx, app.ID)
}

func (s *appsService) Restart(ctx context.Context, db *gorm.DB, opts RestartOpts) error {
if opts.PID != "" {
return s.Scheduler.Stop(ctx, opts.PID)
}

return s.releases.ReleaseApp(ctx, db, opts.App)
}

func (s *appsService) Scale(ctx context.Context, db *gorm.DB, opts ScaleOpts) (*Process, error) {
app, t, quantity, c := opts.App, opts.Process, opts.Quantity, opts.Constraints

release, err := releasesFind(db, ReleasesQuery{App: app})
if err != nil {
return nil, err
}

if release == nil {
return nil, &ValidationError{Err: fmt.Errorf("no releases for %s", app.Name)}
}

p := release.Process(t)
if p == nil {
return nil, &ValidationError{Err: fmt.Errorf("no %s process type in release", t)}
}

// Update quantity for this process in the formation
p.Quantity = quantity
if c != nil {
p.Constraints = *c
}

if err := processesUpdate(db, p); err != nil {
return nil, err
}

// If there are no changes to the process size, we can do a quick scale
// up, otherwise, we will resubmit the release to the scheduler.
if c == nil {
return p, s.Scheduler.Scale(ctx, release.AppID, string(p.Type), uint(quantity))
} else {
return p, s.releases.Release(ctx, release)
}
}

// appsEnsureRepo will set the repo if it's not set.
Expand Down Expand Up @@ -184,57 +229,3 @@ func appsUpdate(db *gorm.DB, app *App) error {
func appsDestroy(db *gorm.DB, app *App) error {
return db.Delete(app).Error
}

// scaler is a small service for scaling an apps process.
type scaler struct {
*Empire
}

func (s *scaler) Scale(ctx context.Context, db *gorm.DB, opts ScaleOpts) (*Process, error) {
app, t, quantity, c := opts.App, opts.Process, opts.Quantity, opts.Constraints

release, err := releasesFind(db, ReleasesQuery{App: app})
if err != nil {
return nil, err
}

if release == nil {
return nil, &ValidationError{Err: fmt.Errorf("no releases for %s", app.Name)}
}

p := release.Process(t)
if p == nil {
return nil, &ValidationError{Err: fmt.Errorf("no %s process type in release", t)}
}

// Update quantity for this process in the formation
p.Quantity = quantity
if c != nil {
p.Constraints = *c
}

if err := processesUpdate(db, p); err != nil {
return nil, err
}

// If there are no changes to the process size, we can do a quick scale
// up, otherwise, we will resubmit the release to the scheduler.
if c == nil {
return p, s.Scheduler.Scale(ctx, release.AppID, string(p.Type), uint(quantity))
} else {
return p, s.releaser.Release(ctx, release)
}
}

// restarter is a small service for restarting an apps processes.
type restarter struct {
*Empire
}

func (s *restarter) Restart(ctx context.Context, opts RestartOpts) error {
if opts.PID != "" {
return s.Scheduler.Stop(ctx, opts.PID)
}

return s.releaser.ReleaseApp(ctx, opts.App)
}
2 changes: 1 addition & 1 deletion certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (s *certsService) CertsAttach(ctx context.Context, db *gorm.DB, app *App, c
return err
}

if err := s.releaser.ReleaseApp(ctx, app); err != nil {
if err := s.releases.ReleaseApp(ctx, db, app); err != nil {
if err == ErrNoReleases {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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.ReleasesCreate(ctx, db, &Release{
_, err = s.releases.Create(ctx, db, &Release{
App: release.App,
Config: c,
Slug: release.Slug,
Expand Down
4 changes: 2 additions & 2 deletions deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *deployerService) deploy(ctx context.Context, db *gorm.DB, opts Deployme
}

// Create a new slug for the docker image.
slug, err := s.slugs.SlugsCreateByImage(ctx, db, img, opts.Output)
slug, err := s.slugs.Create(ctx, db, img, opts.Output)
if err != nil {
return nil, err
}
Expand All @@ -51,7 +51,7 @@ func (s *deployerService) deploy(ctx context.Context, db *gorm.DB, opts Deployme
// and Slug.
desc := fmt.Sprintf("Deploy %s", img.String())

r, err := s.releases.ReleasesCreate(ctx, db, &Release{
r, err := s.releases.Create(ctx, db, &Release{
App: app,
Config: config,
Slug: slug,
Expand Down
12 changes: 3 additions & 9 deletions empire.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ type Empire struct {
domains *domainsService
tasks *tasksService
releases *releasesService
releaser *releaser
deployer *deployerService
scaler *scaler
restarter *restarter
runner *runnerService
slugs *slugsService
certs *certsService
Expand Down Expand Up @@ -100,11 +97,8 @@ func New(db *DB, options Options) *Empire {
e.domains = &domainsService{Empire: e}
e.slugs = &slugsService{Empire: e}
e.tasks = &tasksService{Empire: e}
e.scaler = &scaler{Empire: e}
e.restarter = &restarter{Empire: e}
e.runner = &runnerService{Empire: e}
e.releases = &releasesService{Empire: e}
e.releaser = &releaser{Empire: e}
e.certs = &certsService{Empire: e}
return e
}
Expand Down Expand Up @@ -175,7 +169,7 @@ func (opts DestroyOpts) Event() Event {
func (e *Empire) Destroy(ctx context.Context, opts DestroyOpts) error {
tx := e.db.Begin()

if err := e.apps.AppsDestroy(ctx, tx, opts.App); err != nil {
if err := e.apps.Destroy(ctx, tx, opts.App); err != nil {
tx.Rollback()
return err
}
Expand Down Expand Up @@ -320,7 +314,7 @@ func (opts RestartOpts) Event() Event {
// Restart restarts processes matching the given prefix for the given Release.
// If the prefix is empty, it will match all processes for the release.
func (e *Empire) Restart(ctx context.Context, opts RestartOpts) error {
if err := e.restarter.Restart(ctx, opts); err != nil {
if err := e.apps.Restart(ctx, e.db, opts); err != nil {
return err
}

Expand Down Expand Up @@ -496,7 +490,7 @@ func (opts ScaleOpts) Event() Event {
func (e *Empire) Scale(ctx context.Context, opts ScaleOpts) (*Process, error) {
tx := e.db.Begin()

p, err := e.scaler.Scale(ctx, tx, opts)
p, err := e.apps.Scale(ctx, tx, opts)
if err != nil {
tx.Rollback()
return p, err
Expand Down
61 changes: 28 additions & 33 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ type releasesService struct {
*Empire
}

// ReleasesCreate creates a new release then submits it to the scheduler.
func (s *releasesService) ReleasesCreate(ctx context.Context, db *gorm.DB, r *Release) (*Release, error) {
// Create creates a new release then submits it to the scheduler.
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 @@ -126,7 +126,7 @@ func (s *releasesService) ReleasesCreate(ctx context.Context, db *gorm.DB, r *Re
}

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

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

desc := fmt.Sprintf("Rollback to v%d", version)
return s.ReleasesCreate(ctx, db, &Release{
return s.Create(ctx, db, &Release{
App: app,
Config: r.Config,
Slug: r.Slug,
Description: desc,
})
}

// Release submits a release to the scheduler.
func (s *releasesService) Release(ctx context.Context, release *Release) error {
a := newServiceApp(release)
return s.Scheduler.Submit(ctx, a)
}

// ReleaseApp will find the last release for an app and release it.
func (s *releasesService) ReleaseApp(ctx context.Context, db *gorm.DB, app *App) error {
release, err := releasesFind(db, ReleasesQuery{App: app})
if err != nil {
if err == gorm.RecordNotFound {
return ErrNoReleases
}

return err
}

if release == nil {
return nil
}

return s.Release(ctx, release)
}

// These associations are always available on a Release.
var releasesPreload = Preload("App", "Config", "Slug", "Processes")

Expand Down Expand Up @@ -246,35 +270,6 @@ func releasesCreate(db *gorm.DB, release *Release) (*Release, error) {
return release, nil
}

type releaser struct {
*Empire
}

// ScheduleRelease creates jobs for every process and instance count and
// schedules them onto the cluster.
func (r *releaser) Release(ctx context.Context, release *Release) error {
a := newServiceApp(release)
return r.Scheduler.Submit(ctx, a)
}

// ReleaseApp will find the last release for an app and release it.
func (r *releaser) ReleaseApp(ctx context.Context, app *App) error {
release, err := releasesFind(r.db, ReleasesQuery{App: app})
if err != nil {
if err == gorm.RecordNotFound {
return ErrNoReleases
}

return err
}

if release == nil {
return nil
}

return r.Release(ctx, release)
}

func newServiceApp(release *Release) *scheduler.App {
var processes []*scheduler.Process

Expand Down
2 changes: 1 addition & 1 deletion slugs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type slugsService struct {
}

// SlugsCreateByImage creates a Slug for the given image.
func (s *slugsService) SlugsCreateByImage(ctx context.Context, db *gorm.DB, img image.Image, out io.Writer) (*Slug, error) {
func (s *slugsService) Create(ctx context.Context, db *gorm.DB, img image.Image, out io.Writer) (*Slug, error) {
return slugsCreateByImage(ctx, db, s.ExtractProcfile, img, out)
}

Expand Down

0 comments on commit ec4bb79

Please sign in to comment.