Commit b97386e9 authored by Erick Hitter's avatar Erick Hitter

Doesn't need its own cron

parent e1d56df9
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
"log-dest": "os.Stdout", "log-dest": "os.Stdout",
"api-key": "", "api-key": "",
"threshold": 3600, "threshold": 3600,
"schedule": "* */10 * * * *",
"delete-stale": true "delete-stale": true
} }
...@@ -7,14 +7,12 @@ import ( ...@@ -7,14 +7,12 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"syscall" "sync"
"time" "time"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/robfig/cron"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
...@@ -22,7 +20,6 @@ type config struct { ...@@ -22,7 +20,6 @@ type config struct {
LogDest string `json:"log-dest"` LogDest string `json:"log-dest"`
APIKey string `json:"api-key"` APIKey string `json:"api-key"`
Threshold int `json:"threshold"` Threshold int `json:"threshold"`
Schedule string `json:"schedule"`
DeleteStale bool `json:"delete-stale"` DeleteStale bool `json:"delete-stale"`
} }
...@@ -39,9 +36,10 @@ var ( ...@@ -39,9 +36,10 @@ var (
apiKey string apiKey string
threshold int threshold int
schedule string
deleteStale bool deleteStale bool
wg sync.WaitGroup
client *godo.Client client *godo.Client
) )
...@@ -65,34 +63,27 @@ func init() { ...@@ -65,34 +63,27 @@ func init() {
} }
logDest = cfg.LogDest logDest = cfg.LogDest
apiKey = cfg.APIKey apiKey = cfg.APIKey
threshold = cfg.Threshold threshold = cfg.Threshold
schedule = cfg.Schedule
deleteStale = cfg.DeleteStale deleteStale = cfg.DeleteStale
setUpLogger() setUpLogger()
}
func main() {
logger.Printf("Starting GitLab Runner monitoring with config %s", configPath) logger.Printf("Starting GitLab Runner monitoring with config %s", configPath)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
if deleteStale { if deleteStale {
logger.Println("Stale droplets WILL BE DELETED automatically") logger.Println("Stale droplets WILL BE DELETED automatically")
} else { } else {
logger.Println("Stale droplets will be logged, but not deleted") logger.Println("Stale droplets will be logged, but not deleted")
} }
}
func main() {
authenticate() authenticate()
startCron() checkAPI()
caughtSig := <-sig
logger.Printf("Stopping, got signal %s", caughtSig) wg.Wait()
logger.Println("Execution complete!")
} }
func authenticate() { func authenticate() {
...@@ -113,12 +104,6 @@ func (t *tokenSource) Token() (*oauth2.Token, error) { ...@@ -113,12 +104,6 @@ func (t *tokenSource) Token() (*oauth2.Token, error) {
return token, nil return token, nil
} }
func startCron() {
c := cron.New()
c.AddFunc(schedule, checkAPI)
c.Start()
}
func checkAPI() { func checkAPI() {
ctx := context.TODO() ctx := context.TODO()
droplets, err := listDroplets(ctx, client) droplets, err := listDroplets(ctx, client)
...@@ -129,6 +114,7 @@ func checkAPI() { ...@@ -129,6 +114,7 @@ func checkAPI() {
} }
for _, droplet := range droplets { for _, droplet := range droplets {
wg.Add(1)
go checkDropletAge(droplet) go checkDropletAge(droplet)
} }
} }
...@@ -163,6 +149,8 @@ func listDroplets(ctx context.Context, client *godo.Client) ([]godo.Droplet, err ...@@ -163,6 +149,8 @@ func listDroplets(ctx context.Context, client *godo.Client) ([]godo.Droplet, err
} }
func checkDropletAge(droplet godo.Droplet) { func checkDropletAge(droplet godo.Droplet) {
defer wg.Done()
thr := time.Now().Add(time.Duration(-threshold)) thr := time.Now().Add(time.Duration(-threshold))
created, err := time.Parse(time.RFC3339, droplet.Created) created, err := time.Parse(time.RFC3339, droplet.Created)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment