2019-09-05 01:37:13 +02:00
package main
import (
2021-02-06 02:45:28 +01:00
"log"
2019-12-04 22:44:35 +01:00
"os"
2021-02-06 02:45:28 +01:00
"os/signal"
"syscall"
2021-05-19 04:29:15 +02:00
"time"
2020-10-30 16:30:03 +01:00
"github.com/TwinProduction/gatus/config"
2020-12-30 02:22:17 +01:00
"github.com/TwinProduction/gatus/controller"
2021-02-06 02:45:28 +01:00
"github.com/TwinProduction/gatus/storage"
2020-10-30 16:30:03 +01:00
"github.com/TwinProduction/gatus/watchdog"
2019-09-05 01:37:13 +02:00
)
func main ( ) {
2021-05-19 04:29:15 +02:00
cfg , err := loadConfiguration ( )
if err != nil {
panic ( err )
}
start ( cfg )
2021-02-06 02:45:28 +01:00
// Wait for termination signal
2021-05-19 04:29:15 +02:00
signalChannel := make ( chan os . Signal , 1 )
2021-02-06 02:45:28 +01:00
done := make ( chan bool , 1 )
2021-05-19 04:29:15 +02:00
signal . Notify ( signalChannel , os . Interrupt , syscall . SIGTERM )
2021-02-06 02:45:28 +01:00
go func ( ) {
2021-05-19 04:29:15 +02:00
<- signalChannel
2021-03-04 04:31:55 +01:00
log . Println ( "Received termination signal, attempting to gracefully shut down" )
2021-05-19 04:29:15 +02:00
stop ( )
save ( )
2021-02-06 02:45:28 +01:00
done <- true
} ( )
<- done
log . Println ( "Shutting down" )
2019-09-07 03:59:50 +02:00
}
2021-07-09 05:39:12 +02:00
func start ( cfg * config . Config ) {
2021-09-11 07:51:14 +02:00
go controller . Handle ( cfg . Security , cfg . Web , cfg . UI , cfg . Metrics )
2021-07-09 05:39:12 +02:00
watchdog . Monitor ( cfg )
go listenToConfigurationFileChanges ( cfg )
}
2021-05-19 04:29:15 +02:00
func stop ( ) {
watchdog . Shutdown ( )
controller . Shutdown ( )
}
func save ( ) {
err := storage . Get ( ) . Save ( )
if err != nil {
log . Println ( "Failed to save storage provider:" , err . Error ( ) )
}
}
func loadConfiguration ( ) ( cfg * config . Config , err error ) {
2020-03-08 23:16:39 +01:00
customConfigFile := os . Getenv ( "GATUS_CONFIG_FILE" )
if len ( customConfigFile ) > 0 {
2021-05-19 04:29:15 +02:00
cfg , err = config . Load ( customConfigFile )
2019-12-04 22:44:35 +01:00
} else {
2021-05-19 04:29:15 +02:00
cfg , err = config . LoadDefaultConfiguration ( )
2019-12-04 22:44:35 +01:00
}
2021-05-19 04:29:15 +02:00
return
}
func listenToConfigurationFileChanges ( cfg * config . Config ) {
for {
time . Sleep ( 30 * time . Second )
if cfg . HasLoadedConfigurationFileBeenModified ( ) {
log . Println ( "[main][listenToConfigurationFileChanges] Configuration file has been modified" )
save ( )
updatedConfig , err := loadConfiguration ( )
if err != nil {
if cfg . SkipInvalidConfigUpdate {
log . Println ( "[main][listenToConfigurationFileChanges] Failed to load new configuration:" , err . Error ( ) )
log . Println ( "[main][listenToConfigurationFileChanges] The configuration file was updated, but it is not valid. The old configuration will continue being used." )
// Update the last file modification time to avoid trying to process the same invalid configuration again
cfg . UpdateLastFileModTime ( )
continue
} else {
panic ( err )
}
}
stop ( )
start ( updatedConfig )
return
}
2019-12-04 22:44:35 +01:00
}
}