forked from konveyor/tackle2-hub
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
309 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/konveyor/tackle2-hub/lifecycle" | ||
) | ||
|
||
const ( | ||
StopRoute = "/service/stop" | ||
StartRoute = "/service/start" | ||
) | ||
|
||
type RecoveryHandler struct { | ||
Manager *lifecycle.Manager | ||
} | ||
|
||
func (h RecoveryHandler) AddRoutes(e *gin.Engine) { | ||
routeGroup := e.Group("/") | ||
routeGroup.POST(StopRoute, h.Stop) | ||
routeGroup.POST(StartRoute, h.Start) | ||
} | ||
|
||
func (h RecoveryHandler) Stop(ctx *gin.Context) { | ||
h.Manager.Stop() | ||
ctx.Status(202) | ||
} | ||
|
||
func (h RecoveryHandler) Start(ctx *gin.Context) { | ||
h.Manager.Run() | ||
ctx.Status(202) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jortel/go-utils/logr" | ||
) | ||
|
||
const ( | ||
Unit = time.Second | ||
) | ||
|
||
type Server struct { | ||
Handler http.Handler | ||
Address string | ||
Name string | ||
} | ||
|
||
func (r *Server) Run(ctx context.Context, wg *sync.WaitGroup) { | ||
Log = logr.WithName(r.Name) | ||
srv := &http.Server{ | ||
Addr: r.Address, | ||
Handler: r.Handler, | ||
} | ||
go func() { | ||
Log.Info("api server starting", "address", r.Address) | ||
err := srv.ListenAndServe() | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
Log.Error(err, "api server failed", "address", r.Address) | ||
} | ||
}() | ||
|
||
go func() { | ||
defer wg.Done() | ||
select { | ||
case <-ctx.Done(): | ||
force, cancel := context.WithTimeout(context.Background(), r.timeout()) | ||
defer cancel() | ||
err := srv.Shutdown(force) | ||
if err != nil { | ||
Log.Error(err, "api server shutdown failed", "address", r.Address) | ||
} | ||
return | ||
} | ||
}() | ||
} | ||
|
||
func (r *Server) timeout() (d time.Duration) { | ||
d = Unit * time.Duration(Settings.Shutdown.Timeout) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package lifecycle | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/jortel/go-utils/logr" | ||
"github.com/konveyor/tackle2-hub/database" | ||
"golang.org/x/net/context" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var Log = logr.WithName("lifecycle") | ||
|
||
type Runnable interface { | ||
Run(ctx context.Context, wg *sync.WaitGroup) | ||
} | ||
|
||
func NewManager(db *gorm.DB) (m *Manager) { | ||
m = &Manager{ | ||
db: db, | ||
} | ||
return | ||
} | ||
|
||
type Manager struct { | ||
db *gorm.DB | ||
running bool | ||
cancel context.CancelFunc | ||
wg sync.WaitGroup | ||
runnable []Runnable | ||
} | ||
|
||
func (r *Manager) Register(runnable Runnable) { | ||
r.runnable = append(r.runnable, runnable) | ||
} | ||
|
||
func (r *Manager) Run() { | ||
if r.running { | ||
return | ||
} | ||
r.wg = sync.WaitGroup{} | ||
var ctx context.Context | ||
ctx, r.cancel = context.WithCancel(context.Background()) | ||
for _, runnable := range r.runnable { | ||
r.wg.Add(1) | ||
runnable.Run(ctx, &r.wg) | ||
} | ||
r.running = true | ||
} | ||
|
||
func (r *Manager) Stop() { | ||
if !r.running { | ||
return | ||
} | ||
r.cancel() | ||
r.wg.Wait() | ||
err := database.Close(r.db) | ||
if err != nil { | ||
Log.Error(err, "could not close database after shutting down") | ||
panic(err) | ||
} | ||
r.running = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.