add git pull worker

This commit is contained in:
Kenta420 2023-10-30 10:45:33 +07:00
parent 70cfd89fc4
commit 15e74d47e3
5 changed files with 71 additions and 37 deletions

@ -1 +1 @@
Subproject commit 5adec80644b88c732a06331bfa002427bf0a84da Subproject commit 47004d38a2c9468fa5cdbadfaf792fb9bd234a1f

26
server/git_pull.sh Executable file
View file

@ -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."
}
}
}
}

View file

@ -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)
}
}

View file

@ -35,6 +35,9 @@ func main() {
serverStopCtx() serverStopCtx()
}() }()
// Spawn a goroutine to run git pull every 10 minutes
go RunGitRecipeWorker()
err := s.Run() err := s.Run()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -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()
}