diff --git a/cmd/web-api/main.go b/cmd/web-api/main.go index b642dea..96ce962 100644 --- a/cmd/web-api/main.go +++ b/cmd/web-api/main.go @@ -104,6 +104,7 @@ func main() { TemplateDir string `default:"./templates" envconfig:"TEMPLATE_DIR"` DebugHost string `default:"0.0.0.0:4000" envconfig:"DEBUG_HOST"` ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"` + ScaleToZero time.Duration `envconfig:"SCALE_TO_ZERO"` } Project struct { Name string `default:"" envconfig:"PROJECT_NAME"` @@ -515,6 +516,23 @@ func main() { log.Fatalf("main : Ecs Service Task init : %+v", err) } + // Optional to scale down the service after X seconds. This is useful for a stage env when after 1 hours, review + // should have been completed. The service will automatically scale back to zero for avoid extra costs. The + // deploy stage can always be executed again to bring the service back online. + if cfg.Service.ScaleToZero.Seconds() > 0 { + log.Printf("Scaling service to 0 tasks after %v seconds\n", cfg.Service.ScaleToZero.Seconds()) + + exitTimer := time.NewTimer(cfg.Service.ScaleToZero) + go func() { + <-exitTimer.C + log.Printf("exitTimer expired, scaling awsSession task to 0") + err = devdeploy.EcsServiceSetDesiredCount(log, awsSession, 0) + if err != nil { + log.Fatalf("main : Ecs Service scale down : %+v", err) + } + }() + } + // ========================================================================= // Start API Service diff --git a/cmd/web-app/main.go b/cmd/web-app/main.go index aefd8a5..3db39b9 100644 --- a/cmd/web-app/main.go +++ b/cmd/web-app/main.go @@ -107,6 +107,7 @@ func main() { SessionName string `default:"" envconfig:"SESSION_NAME"` DebugHost string `default:"0.0.0.0:4000" envconfig:"DEBUG_HOST"` ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"` + ScaleToZero time.Duration `envconfig:"SCALE_TO_ZERO"` } Project struct { Name string `default:"" envconfig:"PROJECT_NAME"` @@ -998,6 +999,23 @@ func main() { log.Fatalf("main : Ecs Service Task init : %+v", err) } + // Optional to scale down the service after X seconds. This is useful for a stage env when after 1 hours, review + // should have been completed. The service will automatically scale back to zero for avoid extra costs. The + // deploy stage can always be executed again to bring the service back online. + if cfg.Service.ScaleToZero.Seconds() > 0 { + log.Printf("Scaling service to 0 tasks after %v seconds\n", cfg.Service.ScaleToZero.Seconds()) + + exitTimer := time.NewTimer(cfg.Service.ScaleToZero) + go func() { + <-exitTimer.C + log.Printf("exitTimer expired, scaling awsSession task to 0") + err = devdeploy.EcsServiceSetDesiredCount(log, awsSession, 0) + if err != nil { + log.Fatalf("main : Ecs Service scale down : %+v", err) + } + }() + } + // ========================================================================= // Start APP Service diff --git a/go.mod b/go.mod index 282b4cb..13f7eab 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/tinylib/msgp v1.1.0 // indirect github.com/urfave/cli v1.21.0 github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 - gitlab.com/geeks-accelerator/oss/devops v1.0.19 + gitlab.com/geeks-accelerator/oss/devops v1.0.23 golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 golang.org/x/tools v0.0.0-20190807223507-b346f7fd45de // indirect diff --git a/go.sum b/go.sum index c94c1da..7dca8ed 100644 --- a/go.sum +++ b/go.sum @@ -217,6 +217,10 @@ github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryB github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY= gitlab.com/geeks-accelerator/oss/devops v1.0.19 h1:x/PknYjZFZNfrm9TW4wWXlI73Jd56HYrQt3a1IUbpK8= gitlab.com/geeks-accelerator/oss/devops v1.0.19/go.mod h1:xr+rhNSDXrEh0A6bkBPnfMiRIou3OiPZK0oD5h9GAAM= +gitlab.com/geeks-accelerator/oss/devops v1.0.22 h1:QKTId3GXzSlcXzWYYzLSq/3kniQKRrdygnTYL5MnCe0= +gitlab.com/geeks-accelerator/oss/devops v1.0.22/go.mod h1:xr+rhNSDXrEh0A6bkBPnfMiRIou3OiPZK0oD5h9GAAM= +gitlab.com/geeks-accelerator/oss/devops v1.0.23 h1:ZYWIye57YWJPIV02TKJABbWZ4UXJWQb2PynzsJWbLak= +gitlab.com/geeks-accelerator/oss/devops v1.0.23/go.mod h1:xr+rhNSDXrEh0A6bkBPnfMiRIou3OiPZK0oD5h9GAAM= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=