Process lifecycle management

This commit is contained in:
2024-10-02 11:18:36 +08:00
parent f66f144f2e
commit ae12eb2a15
9 changed files with 221 additions and 13 deletions

View File

@@ -8,8 +8,13 @@ import (
)
func getApplications(c *fiber.Ctx) error {
applications := lo.FlatMap(navi.R.Regions, func(item *navi.Region, idx int) []warden.Application {
return item.Applications
applications := lo.FlatMap(navi.R.Regions, func(item *navi.Region, idx int) []warden.ApplicationInfo {
return lo.Map(item.Applications, func(item warden.Application, index int) warden.ApplicationInfo {
return warden.ApplicationInfo{
Application: item,
Status: warden.GetFromPool(item.ID).Status,
}
})
})
return c.JSON(applications)
@@ -24,3 +29,45 @@ func getApplicationLogs(c *fiber.Ctx) error {
return c.SendString(instance.Logs())
}
}
func letApplicationStart(c *fiber.Ctx) error {
if instance, ok := lo.Find(warden.InstancePool, func(item *warden.AppInstance) bool {
return item.Manifest.ID == c.Params("id")
}); !ok {
return fiber.NewError(fiber.StatusNotFound)
} else {
if err := instance.Wake(); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
}
func letApplicationStop(c *fiber.Ctx) error {
if instance, ok := lo.Find(warden.InstancePool, func(item *warden.AppInstance) bool {
return item.Manifest.ID == c.Params("id")
}); !ok {
return fiber.NewError(fiber.StatusNotFound)
} else {
if err := instance.Stop(); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
}
func letApplicationRestart(c *fiber.Ctx) error {
if instance, ok := lo.Find(warden.InstancePool, func(item *warden.AppInstance) bool {
return item.Manifest.ID == c.Params("id")
}); !ok {
return fiber.NewError(fiber.StatusNotFound)
} else {
if err := instance.Stop(); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
if err := instance.Start(); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
}

View File

@@ -49,6 +49,9 @@ func InitSideload() *fiber.App {
cgi.Get("/regions/cfg/:id", getRegionConfig)
cgi.Get("/applications", getApplications)
cgi.Get("/applications/:id/logs", getApplicationLogs)
cgi.Post("/applications/:id/start", letApplicationStart)
cgi.Post("/applications/:id/stop", letApplicationStop)
cgi.Post("/applications/:id/restart", letApplicationRestart)
cgi.Post("/reload", doReload)
}