Skip to content

Commit

Permalink
Merge pull request #136 from kthcloud/dev
Browse files Browse the repository at this point in the history
add migrator worker, improve logging, fix restarting bugs
  • Loading branch information
saffronjam authored Aug 11, 2023
2 parents c11387b + dbafc2e commit dfd747d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions models/sys/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ type Deployment struct {
ID string `bson:"id"`
Name string `bson:"name"`
OwnerID string `bson:"ownerId"`

CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
RepairedAt time.Time `bson:"repairedAt"`

CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
RepairedAt time.Time `bson:"repairedAt"`
RestartedAt time.Time `bson:"restartedAt"`

Private bool `bson:"private"`
Envs []Env `bson:"envs"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go-deploy/pkg/sys"
"go-deploy/pkg/workers/confirm"
"go-deploy/pkg/workers/job_execute"
"go-deploy/pkg/workers/migrator"
"go-deploy/pkg/workers/repair"
"go-deploy/pkg/workers/status_update"
"go-deploy/routers"
Expand Down Expand Up @@ -38,6 +39,9 @@ func Start(options *StartOptions) *http.Server {
conf.SetupEnvironment()

models.Setup()

migrator.Migrate()

err := job.ResetRunning()
if err != nil {
log.Fatalln("failed to reset running job. details: ", err)
Expand Down
12 changes: 12 additions & 0 deletions pkg/workers/migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package migrator

import "log"

// This file is edited when an update in the database schema has occurred.
// Thus, every migration needed will be done programmatically.
// Once a migration is done, clear the file.

// Migrate will run as early as possible in the program, and it will never be called again.
func Migrate() {
log.Println("nothing to migrate")
}
18 changes: 18 additions & 0 deletions pkg/workers/repair/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ func deploymentRepairer(ctx *sys.Context) {

time.Sleep(time.Duration(conf.Env.Deployment.RepairInterval) * time.Second)

restarting, err := deploymentModel.GetByActivity(deploymentModel.ActivityRestarting)
if err != nil {
log.Println("error fetching restarting deployments. details: ", err)
continue
}

for _, deployment := range restarting {
// remove activity if it has been restarting for more than 5 minutes
now := time.Now()
if now.Sub(deployment.RestartedAt) > 5*time.Minute {
log.Printf("removing restarting activity from deployment %s\n", deployment.Name)
err = deploymentModel.RemoveActivity(deployment.ID, deploymentModel.ActivityRestarting)
if err != nil {
log.Printf("failed to remove restarting activity from deployment %s. details: %s\n", deployment.Name, err.Error())
}
}
}

withNoActivities, err := deploymentModel.GetWithNoActivities()
if err != nil {
log.Println("error fetching deployments with no activities. details: ", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func CreateBuild(id string, params *deploymentModel.BuildParams) error {
time.Sleep(1 * time.Second)
}

log.Println("build finished with gitlab")
log.Println("build finished with gitlab for deployment", id)

return nil
}
Expand Down

0 comments on commit dfd747d

Please sign in to comment.