Skip to content

Commit

Permalink
Merge branch 'manual-stop-scaling' into 'master'
Browse files Browse the repository at this point in the history
Added possibility to manually stop autoscaling by creating a file

See merge request mydocker/mydockergoapp!56

GitOrigin-RevId: 70375c7d3f0f2b01f2ab6274701410656de398bc
  • Loading branch information
pdesgarets authored and p-bizouard committed Apr 5, 2024
1 parent 7ccaffa commit d0dabab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
3 changes: 3 additions & 0 deletions docker-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ this project does NOT adhere to [Semantic Versioning](https://semver.org/spec/v2

## Unreleased

### Added
- Manually stop autoscaling by creating a file

## 2.16.1
### Added
- Pass registry credentials to build image
Expand Down
4 changes: 4 additions & 0 deletions docker-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ provision:
5. Install the plugin: `docker plugin install --disable centralesupelec/mydockervolume`
6. Configure the plugin : `docker plugin set centralesupelec/mydockervolume DRIVER_MODE=FS`
7. Then, launch the Go app : `docker-compose up`

### Manually stop scaleUp / scaleDown

Configure StopScaleUpFilePath / StopScaleDownFilePath. Each time autoscaleUp(Down) is run, it will first check if the corresponding stop file exists. If found, the autoscaling will not proceed.
14 changes: 11 additions & 3 deletions docker-api/docker-api/autoscale_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package main
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"

pb "github.com/centralesupelec/mydocker/docker-api/protobuf"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
"strings"
"sync"
"time"
)

func addScaleDownCron(s *gocron.Scheduler, dockerClient *client.Client, lock *sync.Mutex, scaleUpConfig *ScaleUpConfig) error {
Expand Down Expand Up @@ -61,6 +63,12 @@ type scaleDownService struct {
func (s *scaleDownService) run() {
s.autoscalingLock.Lock()
defer s.autoscalingLock.Unlock()

if _, err := os.Stat(c.StopScaleDownFilePath); err == nil {
s.logger.Info("StopScaleDown file detected. Exiting the scale-down process.")
return
}

jobId := RandomString(6)
s.logger = s.rootLogger.WithFields(log.Fields{"jobId": jobId})
s.logger.Tracef("Start scale down #%s", jobId)
Expand Down
20 changes: 14 additions & 6 deletions docker-api/docker-api/autoscale_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package main
import (
"context"
"fmt"
pb "github.com/centralesupelec/mydocker/docker-api/protobuf"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
"math"
"math/rand"
"os"
"regexp"
"strings"
"sync"
"time"

pb "github.com/centralesupelec/mydocker/docker-api/protobuf"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
)

type scaleUpDockerClient interface {
Expand Down Expand Up @@ -75,6 +77,12 @@ type scaleUpService struct {
func (s *scaleUpService) run() {
s.autoscalingLock.Lock()
defer s.autoscalingLock.Unlock()

if _, err := os.Stat(c.StopScaleUpFilePath); err == nil {
s.logger.Info("StopScaleUp file detected. Exiting the scale-up process.")
return
}

jobId := RandomString(6)
s.logger = s.rootLogger.WithFields(log.Fields{"jobId": jobId})
s.logger.Tracef("Start scale up #%s", jobId)
Expand Down
9 changes: 7 additions & 2 deletions docker-api/docker-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package main

import (
"flag"
"github.com/go-co-op/gocron"
"github.com/spf13/viper"
"io"
"math/rand"
"net"
_ "net/http/pprof"
"strconv"
"time"

"github.com/go-co-op/gocron"
"github.com/spf13/viper"

pb "github.com/centralesupelec/mydocker/docker-api/protobuf"

"github.com/docker/docker/client"
Expand Down Expand Up @@ -57,6 +58,8 @@ type config struct {
ScaleDownRemoveNonEmpty bool
DeployEnvSecrets []string
PrecreateVolume bool
StopScaleUpFilePath string
StopScaleDownFilePath string
}

type registryCredential struct {
Expand Down Expand Up @@ -295,6 +298,8 @@ func main() {
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.SetDefault("MaxRecvMsgSize", defaultMaxRecvMsgSize)
viper.SetDefault("BuildImageRepository", "dev")
viper.SetDefault("StopScaleUpFilePath", "/etc/docker-api/stop-scale-up")
viper.SetDefault("StopScaleDownFilePath", "/etc/docker-api/stop-scale-down")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Panicf("Failed to find config file: %v", err)
Expand Down

0 comments on commit d0dabab

Please sign in to comment.