60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package scaler
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/go/jynx/backend/common"
|
|
"git.stinnesbeck.com/go/jynx/backend/hetzner"
|
|
"git.stinnesbeck.com/go/jynx/ci/woodpecker"
|
|
"git.stinnesbeck.com/go/jynx/db"
|
|
"git.stinnesbeck.com/go/log"
|
|
)
|
|
|
|
func Start(w *woodpecker.Server) error {
|
|
log.PrintDebug("Starting autoscaler")
|
|
stats, err := w.GetStats()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// read info from database
|
|
servers, err := db.Read()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var newServers int
|
|
|
|
if stats.PendingJobs == 0 || stats.IdlingWorkers+stats.RunningJobs+uint(len(servers)) >= stats.PendingJobs {
|
|
return nil
|
|
}
|
|
|
|
log.PrintInfo("starting", stats.PendingJobs, "workers")
|
|
|
|
newServers += int(stats.PendingJobs)
|
|
var ScaleUpServers []common.Server
|
|
|
|
for i := 0; i < newServers; i++ {
|
|
name := fmt.Sprintf("jynx-worker-%d", i+1)
|
|
|
|
ScaleUpServers = append(ScaleUpServers, common.Server{
|
|
Name: name,
|
|
Created: time.Now(),
|
|
})
|
|
if err := hetzner.AddServer(ScaleUpServers[i]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// save servers to database
|
|
servers = append(servers, ScaleUpServers...)
|
|
if err := db.Save(servers); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.PrintInfo(stats.RunningJobs, "running jobs,", stats.PendingJobs, "pending jobs,", stats.IdlingWorkers, "idling workers,", len(servers), "servers starting up")
|
|
|
|
return nil
|
|
}
|