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"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
//"syscall"
|
2021-02-11 21:31:32 +01:00
|
|
|
"github.com/edgebox-iot/sysctl/internal/diagnostics"
|
|
|
|
|
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-11 19:56:35 +01:00
|
|
|
name := flag.String("name", "edgebox", "name for the service")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-02-11 21:31:32 +01:00
|
|
|
if *version {
|
|
|
|
printVersion()
|
|
|
|
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
|
|
|
|
signal.Notify(sigs)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// infinite loop
|
|
|
|
for {
|
|
|
|
|
|
|
|
log.Printf("Executing instruction %s", *name)
|
|
|
|
|
|
|
|
// wait random number of milliseconds
|
|
|
|
Nsecs := 1000
|
|
|
|
log.Printf("Next instruction executed in %dms", Nsecs)
|
|
|
|
time.Sleep(time.Millisecond * time.Duration(Nsecs))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11 21:31:32 +01:00
|
|
|
"version: %s\ncommit: %s\nbuild time: %s",
|
|
|
|
diagnostics.Version, diagnostics.Commit, diagnostics.BuildDate,
|
|
|
|
)
|
|
|
|
}
|