From 0c2fd3066e775b5a8ef7360055d83416ac3022d5 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Thu, 11 Feb 2021 19:56:35 +0100 Subject: [PATCH] Fixed Dockerfile and compose files for auto re-build, added loop for system service behaviour --- .gitignore | 1 + Dockerfile | 2 +- docker-compose.yml | 2 -- main.go | 61 ++++++++++++++++++++++++++++++++++++---------- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 66fd13c..525ab5b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +main # Test binary, built with `go test -c` *.test diff --git a/Dockerfile b/Dockerfile index 76d3cff..d617d40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN go mod download RUN go get github.com/githubnemo/CompileDaemon -ENTRYPOINT CompileDaemon --build="go build main.go" --command=./runsysctl \ No newline at end of file +ENTRYPOINT CompileDaemon --build="go build main.go" --command=./main \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1ef87a3..e04a675 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,5 +4,3 @@ services: build: ./ volumes: - ./:/app - edgebox-queue: - image: redis-alpine \ No newline at end of file diff --git a/main.go b/main.go index dd87d32..7d1d515 100644 --- a/main.go +++ b/main.go @@ -1,20 +1,55 @@ package main import ( - "fmt" - "github.com/go-redis/redis/v8" + "flag" + "log" + "os" + "os/signal" + "time" + //"syscall" ) func main() { - fmt.Println("Hello World") - - // client := redis.NewClient(&redis.Options{ - // Addr: "edgebox-queue:6379", - // Password: "", // no password set - // DB: 0, // use default DB - // }) - // pong, err := client.Ping().Result() - // fmt.Println(pong, err) - -} \ No newline at end of file + // load command line arguments + + name := flag.String("name", "edgebox", "name for the service") + + flag.Parse() + + 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) + AppCleanup() + 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 +func AppCleanup() { + log.Println("Cleaning up app status before exit") +}