jynx/scaler/scaler.go
Nils Stinnesbeck c7da3f2147
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
first test build
2023-07-15 21:51:31 +02:00

78 lines
2.0 KiB
Go

package scaler
import (
"fmt"
"os"
"git.stinnesbeck.com/go/jynx/backend/hetzner"
"git.stinnesbeck.com/go/jynx/ci/woodpecker"
"git.stinnesbeck.com/go/log"
)
func Start(w *woodpecker.Server) error {
log.PrintDebug("Starting autoscaler")
stats, err := w.GetStats()
if err != nil {
return err
}
// get the prefix for jynx managed servers
prefix := os.Getenv("HCLOUD_PREFIX")
// get a list of all servers from hetzner (from jynx)
servers, err := hetzner.ListJynxServers(prefix)
if err != nil {
return err
}
newServers := stats.PendingJobs - len(servers) + stats.RunningJobs
log.PrintDebug(stats.RunningJobs, "running jobs,", stats.PendingJobs, "pending jobs,", stats.IdlingWorkers, "idling workers,", len(servers)-stats.RunningJobs-stats.IdlingWorkers, "servers starting up")
// check if there are servers to be removed
if stats.PendingJobs == 0 && stats.RunningJobs == 0 {
// we need to remove servers
if err := hetzner.CheckServerDeletions(servers); err != nil {
return err
}
}
// check if we need to create new servers
// if stats.PendingJobs < 1 || len(servers)-stats.RunningJobs >= stats.PendingJobs {
if stats.PendingJobs < 1 || newServers < 1 {
// if stats.PendingJobs == 0 || stats.IdlingWorkers+stats.RunningJobs >= stats.PendingJobs {
// skip rest of this run
return nil
}
// there are servers to be created
if newServers < 1 {
return nil
}
var ScaleUpServers []string
// get the suffix for the starting server
suffix, err := hetzner.GetNewSuffix(servers)
for i := 0; i < newServers; i++ {
if err != nil {
return err
}
// assemble server name and add it to the list of servers to scale up
serverName := fmt.Sprintf("%s-worker-%d", prefix, suffix)
ScaleUpServers = append(ScaleUpServers, serverName)
// add 1 to the suffix for the next server
suffix++
}
log.PrintInfo("starting", newServers, "workers:", ScaleUpServers)
// create servers on hetzner cloud
if err := hetzner.CreateServers(ScaleUpServers); err != nil {
return err
}
return nil
}