feat: add timeout to stop after ping (set in config)

This commit is contained in:
Medzik 2021-08-30 13:41:06 +00:00
parent cca24ec927
commit 385f98e080
7 changed files with 60 additions and 39 deletions

44
backend/stopAfterPing.go Normal file
View file

@ -0,0 +1,44 @@
package backend
import (
"os"
"time"
"github.com/MedzikUser/go-utils/common"
"gitlab.com/gaming0skar123/go/pingbot/config"
)
var log = common.Log
func StopAfterPing() {
cache(0)
num := ping()
// timeout
go func() {
timeout := config.Toml.Options.Stop_After_Ping_Timeout
if timeout == 0 {
return
}
time.Sleep(timeout * time.Second)
log.Warn("DB Size -> ", num)
log.Warn("Pinged -> ", Status.Success+Status.Error)
log.Warn("Success -> ", Status.Success)
log.Warn("Error -> ", Status.Error)
os.Exit(1)
}()
for {
if int64(num) == Status.Success+Status.Error {
log.Info("DB Size -> ", num)
log.Info("Pinged -> ", Status.Success+Status.Error)
log.Info("Success -> ", Status.Success)
log.Info("Error -> ", Status.Error)
break
}
}
}

View file

@ -1,14 +0,0 @@
package backend
func StopAfterPing() int {
cache(0)
num := ping()
for {
if int64(num) == Status.Success+Status.Error {
break
}
}
return num
}

View file

@ -1,23 +1,24 @@
[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
stop_after_ping=false # Stop after all pings
stop_after_ping_timeout=120 # If not pinget or other options stop program after 120 (default) seconds

View file

@ -46,7 +46,8 @@ type mongoDBConfig struct {
}
type optionsConfig struct {
Stop_After_Ping bool
Stop_After_Ping bool
Stop_After_Ping_Timeout time.Duration
}
var Toml tomlConfig

3
go.sum
View file

@ -134,7 +134,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
@ -187,8 +186,6 @@ github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaR
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.5 h1:9O69jUPDcsT9fEm74W92rZL9FQY7rCdaXVneq+yyzl4=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

10
main.go
View file

@ -1,7 +1,6 @@
package main
import (
"fmt"
"os"
"sync"
"time"
@ -48,14 +47,7 @@ func main() {
}
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)
backend.StopAfterPing()
os.Exit(0)
}