diff --git a/server/cofffeemachineConfig b/server/cofffeemachineConfig index 5adec80..47004d3 160000 --- a/server/cofffeemachineConfig +++ b/server/cofffeemachineConfig @@ -1 +1 @@ -Subproject commit 5adec80644b88c732a06331bfa002427bf0a84da +Subproject commit 47004d38a2c9468fa5cdbadfaf792fb9bd234a1f diff --git a/server/git_pull.sh b/server/git_pull.sh new file mode 100755 index 0000000..a541675 --- /dev/null +++ b/server/git_pull.sh @@ -0,0 +1,26 @@ +#!/usr/bin/expect + +set timeout 30 + +if { $env(password_env) eq "" } { + send_user "password_env is empty\n" + exit 1 +} +log_user 0 +cd ./cofffeemachineConfig +spawn git pull +expect { + "ikong@192.168.10.159's password:" { + send "$env(password_env)\n" + expect { + "Already up to date." { + log_user 1 + send_user "Already up to date." + } + "remote:" { + log_user 1 + send_user "Coffee recipe updated." + } + } + } +} \ No newline at end of file diff --git a/server/git_recipe_worker.go b/server/git_recipe_worker.go new file mode 100644 index 0000000..afd7886 --- /dev/null +++ b/server/git_recipe_worker.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "fmt" + "os/exec" + "recipe-manager/services/logger" + "time" + + "go.uber.org/zap" +) + +func pull_request() error { + contextCommand, cancelCommand := context.WithTimeout(context.Background(), 30*time.Second) + defer cancelCommand() + pull_command := exec.CommandContext(contextCommand, "./git_pull.sh") + + output, err := pull_command.CombinedOutput() + if err != nil { + return err + } + + if len(output) > 0 { + if string(output) == "Already up to date." || string(output) == "Coffee recipe updated." { + logger.GetInstance().Info("Git pull successful", zap.String("output", string(output))) + } + } + + return nil +} + +func RunGitRecipeWorker() { + //pull every 10 minutes + for { + err := pull_request() + if err != nil { + fmt.Println(err) + } + time.Sleep(10 * time.Minute) + } +} diff --git a/server/main.go b/server/main.go index b486261..1a5db19 100644 --- a/server/main.go +++ b/server/main.go @@ -35,6 +35,9 @@ func main() { serverStopCtx() }() + // Spawn a goroutine to run git pull every 10 minutes + go RunGitRecipeWorker() + err := s.Run() if err != nil { log.Fatal(err) diff --git a/server/main_test.go b/server/main_test.go deleted file mode 100644 index 44d9788..0000000 --- a/server/main_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "context" - "log" - "os" - "os/signal" - "syscall" - "testing" - "time" -) - -func TestServer(t *testing.T) { - s := NewServer() - - serverCtx, serverStopCtx := context.WithCancel(context.Background()) - sig := make(chan os.Signal, 1) - signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) - - // start server 3 minute and stop - go func() { - time.Sleep(180 * time.Second) - serverStopCtx() - }() - - // start server - go func() { - err := s.Run() - if err != nil { - log.Fatal(err) - } - }() - - // stop server - <-serverCtx.Done() -}