diff --git a/backend/ping.go b/backend/ping.go index 35d5b44..69aa7c2 100644 --- a/backend/ping.go +++ b/backend/ping.go @@ -11,7 +11,7 @@ import ( var cacheRetry int -func ping() { +func ping() int { if cacheRetry >= config.Toml.Backend.Cache { cache(0) cacheRetry = 0 @@ -21,6 +21,8 @@ func ping() { for _, url := range cacheURL { go loop(url) } + + return len(cacheURL) } func loop(url string) { diff --git a/backend/stop_after_ping.go b/backend/stop_after_ping.go new file mode 100644 index 0000000..7eed80d --- /dev/null +++ b/backend/stop_after_ping.go @@ -0,0 +1,14 @@ +package backend + +func StopAfterPing() int { + cache(0) + num := ping() + + for { + if int64(num) == Status.Success+Status.Error { + break + } + } + + return num +} diff --git a/config.schema.toml b/config.schema.toml index 9be0997..9f53e75 100644 --- a/config.schema.toml +++ b/config.schema.toml @@ -1,20 +1,23 @@ [http] -enabled=true # Enable Website -port=8080 # Port to listen +enabled=true # Enable Website +port=8080 # Port to listen [backend] -enabled=true # Enable Backend -ping=3 # Ping every three minutes -cache=5 # Get urls from db every five url pings +enabled=true # Enable Backend +ping=3 # Ping every three minutes +cache=5 # Get urls from db every five url pings [autoupdate] -enabled=true # Enable Auto Updater -check=2 # Check every two minutes +enabled=true # Enable Auto Updater +check=2 # Check every two minutes [cluster] -id=1 # Cluster ID -node=1 # Node ID +id=1 # Cluster ID +node=1 # Node ID [mongodb] -database="PingBot" # MongoDB Database Name -collection="URL" # MongoDB Collection Name +database="PingBot" # MongoDB Database Name +collection="URL" # MongoDB Collection Name + +[options] +stop_after_ping=false # Stop after all pings diff --git a/config/toml.go b/config/toml.go index 2d86f5a..29c8c65 100644 --- a/config/toml.go +++ b/config/toml.go @@ -16,6 +16,7 @@ type tomlConfig struct { AutoUpdate autoUpdateConfig Cluster clusterConfig MongoDB mongoDBConfig + Options optionsConfig } type httpConfig struct { @@ -44,6 +45,10 @@ type mongoDBConfig struct { Collection string } +type optionsConfig struct { + Stop_After_Ping bool +} + var Toml tomlConfig func init() { diff --git a/main.go b/main.go index b68812f..51b9f7c 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "sync" "time" @@ -22,6 +23,43 @@ func main() { mongo.Connect() + if config.Toml.AutoUpdate.Enabled { + wg.Add(1) + + client := updater.Client{ + GitHub: config.GH_Repo, + GitHubToken: config.GH_Token, + Version: config.Version, + Binary: "pingbot.out", + CheckEvery: config.Toml.AutoUpdate.Check * time.Minute, + AfterUpdate: func() { + log.Info("Updated!") + + if !config.Toml.Options.Stop_After_Ping { + os.Exit(0) + } + }, + Major: false, + } + + go client.AutoUpdater() + } else { + log.Warn("Auto Update -> Disabled") + } + + if config.Toml.Options.Stop_After_Ping { + dbNum := backend.StopAfterPing() + + fmt.Println() + + log.Info("DB Size -> ", dbNum) + log.Info("Pinged -> ", backend.Status.Success+backend.Status.Error) + log.Info("Success -> ", backend.Status.Success) + log.Info("Error -> ", backend.Status.Error) + + os.Exit(0) + } + if config.Toml.HTTP.Enabled { wg.Add(1) go website.Server() @@ -36,28 +74,6 @@ func main() { log.Warn("Backend -> Disabled") } - if config.Toml.AutoUpdate.Enabled { - wg.Add(1) - - client := updater.Client{ - GitHub: config.GH_Repo, - GitHubToken: config.GH_Token, - Version: config.Version, - Binary: "pingbot.out", - CheckEvery: config.Toml.AutoUpdate.Check * time.Minute, - AfterUpdate: func() { - log.Info("Updated!") - - os.Exit(1) - }, - Major: false, - } - - go client.AutoUpdater() - } else { - log.Warn("Auto Update -> Disabled") - } - config.StartTime = time.Now() wg.Wait()