2019-09-04 19:37:13 -04:00
package main
import (
2021-02-05 20:45:28 -05:00
"log"
2019-12-04 16:44:35 -05:00
"os"
2021-02-05 20:45:28 -05:00
"os/signal"
"syscall"
2021-05-18 22:29:15 -04:00
"time"
2020-10-30 21:00:03 +05:30
2022-12-06 01:41:09 -05:00
"github.com/TwiN/gatus/v5/config"
"github.com/TwiN/gatus/v5/controller"
"github.com/TwiN/gatus/v5/storage/store"
"github.com/TwiN/gatus/v5/watchdog"
2019-09-04 19:37:13 -04:00
)
func main () {
2021-05-18 22:29:15 -04:00
cfg , err := loadConfiguration ()
if err != nil {
panic ( err )
}
2021-10-28 19:35:46 -04:00
initializeStorage ( cfg )
2021-05-18 22:29:15 -04:00
start ( cfg )
2021-02-05 20:45:28 -05:00
// Wait for termination signal
2021-05-18 22:29:15 -04:00
signalChannel := make ( chan os . Signal , 1 )
2021-02-05 20:45:28 -05:00
done := make ( chan bool , 1 )
2021-05-18 22:29:15 -04:00
signal . Notify ( signalChannel , os . Interrupt , syscall . SIGTERM )
2021-02-05 20:45:28 -05:00
go func () {
2021-05-18 22:29:15 -04:00
<- signalChannel
2021-03-03 22:31:55 -05:00
log . Println ( "Received termination signal, attempting to gracefully shut down" )
2021-05-18 22:29:15 -04:00
stop ()
save ()
2021-02-05 20:45:28 -05:00
done <- true
}()
<- done
log . Println ( "Shutting down" )
2019-09-06 21:59:50 -04:00
}
2021-07-08 23:39:12 -04:00
func start ( cfg * config . Config ) {
2022-07-28 20:07:53 -04:00
go controller . Handle ( cfg )
2021-07-08 23:39:12 -04:00
watchdog . Monitor ( cfg )
go listenToConfigurationFileChanges ( cfg )
}
2021-05-18 22:29:15 -04:00
func stop () {
watchdog . Shutdown ()
controller . Shutdown ()
}
func save () {
2021-10-28 19:35:46 -04:00
if err := store . Get (). Save (); err != nil {
2021-05-18 22:29:15 -04:00
log . Println ( "Failed to save storage provider:" , err . Error ())
}
}
2023-01-08 17:53:37 -05:00
func loadConfiguration () ( * config . Config , error ) {
configPath := os . Getenv ( "GATUS_CONFIG_PATH" )
// Backwards compatibility
if len ( configPath ) == 0 {
2023-01-10 19:04:19 -05:00
if configPath = os . Getenv ( "GATUS_CONFIG_FILE" ); len ( configPath ) > 0 {
log . Println ( "WARNING: GATUS_CONFIG_FILE is deprecated. Please use GATUS_CONFIG_PATH instead." )
}
2019-12-04 16:44:35 -05:00
}
2023-01-08 17:53:37 -05:00
return config . LoadConfiguration ( configPath )
2021-05-18 22:29:15 -04:00
}
2021-10-28 19:35:46 -04:00
// initializeStorage initializes the storage provider
//
// Q: "TwiN, why are you putting this here? Wouldn't it make more sense to have this in the config?!"
// A: Yes. Yes it would make more sense to have it in the config package. But I don't want to import
2022-12-06 01:41:09 -05:00
// the massive SQL dependencies just because I want to import the config, so here we are.
2021-10-28 19:35:46 -04:00
func initializeStorage ( cfg * config . Config ) {
err := store . Initialize ( cfg . Storage )
if err != nil {
panic ( err )
}
// Remove all EndpointStatus that represent endpoints which no longer exist in the configuration
var keys [] string
for _ , endpoint := range cfg . Endpoints {
keys = append ( keys , endpoint . Key ())
}
numberOfEndpointStatusesDeleted := store . Get (). DeleteAllEndpointStatusesNotInKeys ( keys )
if numberOfEndpointStatusesDeleted > 0 {
2023-01-08 17:53:37 -05:00
log . Printf ( "[main][initializeStorage] Deleted %d endpoint statuses because their matching endpoints no longer existed" , numberOfEndpointStatusesDeleted )
2021-10-28 19:35:46 -04:00
}
}
2021-05-18 22:29:15 -04:00
func listenToConfigurationFileChanges ( cfg * config . Config ) {
for {
time . Sleep ( 30 * time . Second )
2023-01-08 17:53:37 -05:00
if cfg . HasLoadedConfigurationBeenModified () {
2021-05-18 22:29:15 -04:00
log . Println ( "[main][listenToConfigurationFileChanges] Configuration file has been modified" )
2021-12-03 18:20:09 -05:00
stop ()
time . Sleep ( time . Second ) // Wait a bit to make sure everything is done.
2021-05-18 22:29:15 -04:00
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 )
}
}
2021-12-03 18:20:09 -05:00
initializeStorage ( updatedConfig )
2021-05-18 22:29:15 -04:00
start ( updatedConfig )
return
}
2019-12-04 16:44:35 -05:00
}
}