2020-11-08 23:43:18 +01:00
package main
2020-11-08 23:48:45 +01:00
2020-11-08 23:43:18 +01:00
import (
2021-02-11 19:56:35 +01:00
"flag"
2021-02-12 18:42:22 +01:00
"fmt"
2021-02-11 19:56:35 +01:00
"log"
"os"
"os/signal"
2021-02-12 19:11:36 +01:00
"syscall"
2021-02-13 15:51:14 +01:00
"time"
2021-02-12 18:42:22 +01:00
2021-02-11 21:31:32 +01:00
"github.com/edgebox-iot/sysctl/internal/diagnostics"
2021-02-13 16:06:13 +01:00
"github.com/edgebox-iot/sysctl/internal/tasks"
2021-02-17 12:10:15 +01:00
"github.com/edgebox-iot/sysctl/internal/utils"
2020-11-08 23:43:18 +01:00
)
2021-02-13 21:49:09 +01:00
const defaultNotReadySleepTime time . Duration = time . Second * 60
const defaultSleepTime time . Duration = time . Second
2020-11-08 23:43:18 +01:00
func main ( ) {
2020-11-08 23:48:45 +01:00
2021-02-11 19:56:35 +01:00
// load command line arguments
2021-02-11 21:31:32 +01:00
version := flag . Bool ( "version" , false , "Get the version info" )
2021-02-13 16:06:13 +01:00
db := flag . Bool ( "database" , false , "Get database connection info" )
name := flag . String ( "name" , "edgebox" , "Name for the service" )
2021-02-11 19:56:35 +01:00
flag . Parse ( )
2021-02-11 21:31:32 +01:00
if * version {
printVersion ( )
os . Exit ( 0 )
2021-02-12 18:42:22 +01:00
}
2021-02-11 21:31:32 +01:00
2021-02-13 16:06:13 +01:00
if * db {
printDbDetails ( )
os . Exit ( 0 )
}
2021-02-11 19:56:35 +01:00
log . Printf ( "Starting Sysctl service for %s" , * name )
// setup signal catching
sigs := make ( chan os . Signal , 1 )
// catch all signals since not explicitly listing
2021-02-12 19:11:36 +01:00
signal . Notify ( sigs , syscall . SIGQUIT )
2021-02-11 19:56:35 +01:00
// Cathing specific signals can be done with:
//signal.Notify(sigs,syscall.SIGQUIT)
// method invoked upon seeing signal
go func ( ) {
s := <- sigs
log . Printf ( "RECEIVED SIGNAL: %s" , s )
2021-02-11 21:31:32 +01:00
appCleanup ( )
2021-02-11 19:56:35 +01:00
os . Exit ( 1 )
} ( )
2021-02-13 15:51:14 +01:00
printVersion ( )
2021-02-13 16:06:13 +01:00
2021-02-13 15:51:14 +01:00
printDbDetails ( )
2021-02-13 16:06:13 +01:00
2021-02-15 19:14:46 +01:00
tick := 0
2021-02-11 19:56:35 +01:00
// infinite loop
for {
2021-02-15 19:14:46 +01:00
tick ++ // Tick is an int, so eventually will "go out of ticks?" Maybe we want to reset the ticks every once in a while, to avoid working with big numbers...
systemIterator ( name , tick )
2021-02-11 19:56:35 +01:00
}
}
// AppCleanup : cleanup app state before exit
2021-02-11 21:31:32 +01:00
func appCleanup ( ) {
2021-02-11 19:56:35 +01:00
log . Println ( "Cleaning up app status before exit" )
}
2021-02-11 21:31:32 +01:00
func printVersion ( ) {
2021-02-11 21:38:43 +01:00
fmt . Printf (
2021-02-13 15:51:14 +01:00
"\nversion: %s\ncommit: %s\nbuild time: %s\n" ,
2021-02-11 21:31:32 +01:00
diagnostics . Version , diagnostics . Commit , diagnostics . BuildDate ,
)
}
2021-02-12 18:42:22 +01:00
2021-02-13 15:51:14 +01:00
func printDbDetails ( ) {
fmt . Printf (
2021-02-17 12:10:15 +01:00
"\n\nDatabase Connection Information:\n %s\n\n" ,
utils . GetMySQLDbConnectionDetails ( ) ,
2021-02-13 15:51:14 +01:00
)
}
2021-02-12 18:42:22 +01:00
// IsSystemReady : Checks hability of the service to execute commands (Only after "edgebox --build" is ran at least once via SSH, or if built for distribution)
func isSystemReady ( ) bool {
2021-02-13 21:52:08 +01:00
_ , err := os . Stat ( "/home/system/components/ws/.ready" )
2021-02-12 18:42:22 +01:00
return ! os . IsNotExist ( err )
}
// IsDatabaseReady : Checks is it can successfully connect to the task queue db
func isDatabaseReady ( ) bool {
return false
}
2021-02-15 19:14:46 +01:00
func systemIterator ( name * string , tick int ) {
2021-02-15 20:37:16 +01:00
2021-02-15 19:14:46 +01:00
log . Printf ( "Tick is %d" , tick )
2021-02-12 18:42:22 +01:00
2021-02-13 17:04:57 +01:00
if isSystemReady ( ) {
2021-02-12 18:42:22 +01:00
// Wait about 60 seconds before trying again.
log . Printf ( "System not ready. Next try will be executed in 60 seconds" )
2021-02-13 21:49:09 +01:00
time . Sleep ( defaultNotReadySleepTime )
2021-02-12 18:42:22 +01:00
} else {
2021-02-15 19:14:46 +01:00
tasks . ExecuteSchedules ( tick )
2021-02-12 18:42:22 +01:00
// Wait about 1 second before resumming operations.
log . Printf ( "Next instruction will be executed 1 second" )
2021-02-13 21:49:09 +01:00
time . Sleep ( defaultSleepTime )
nextTask := tasks . GetNextTask ( )
if nextTask . Task != "" {
log . Printf ( "Executing task %s / Args: %s" , nextTask . Task , nextTask . Args )
tasks . ExecuteTask ( nextTask )
} else {
log . Printf ( "No tasks to execute." )
}
2021-02-13 17:04:57 +01:00
2021-02-12 18:42:22 +01:00
}
2021-02-12 18:51:21 +01:00
2021-02-12 18:42:22 +01:00
}