Skip to content

Commit

Permalink
feat: add signal handling serviceweaver config (#11)
Browse files Browse the repository at this point in the history
Added signal handling for graceful process termination and set the 
SERVICEWEAVER_CONFIG environment variable if not already set. 
The feature enhances the run method by incorporating signal handling 
for gracefully closing the process upon receiving SIGINT, SIGKILL, 
or SIGTERM signals. Additionally, a comment with a TODO was included 
for potential future improvements. Also, Set MySQL driver in template 
for database connections
  • Loading branch information
renanbastos93 authored Jul 3, 2023
2 parents 66461b3 + 4a82494 commit 5dc55b3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/boneless/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func init() {

func main() {
flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }

flag.Parse()

if len(flag.Args()) == 0 {
fmt.Fprint(os.Stderr, usage)
os.Exit(1)
Expand Down
45 changes: 36 additions & 9 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import (
"io/fs"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"time"
)

const msgRunning = `
__ __ ______ __ ____
/ /_ ____ ____ ___ / / ___ _____ _____ / ____/ / / / _/
/ __ \ / __ \ / __ \ / _ \ / / / _ \ / ___/ / ___/ / / / / / /
/ /_/ // /_/ / / / / // __/ / / / __/ (__ ) (__ ) / /___ / /___ _/ /
/_.___/ \____/ /_/ /_/ \___/ /_/ \___/ /____/ /____/ \____/ /_____//___/
running...
`

func findMainFile() (filePath string) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}

// TODO: improve that to ensure that the file is the package main
_ = filepath.Walk(pwd, func(path string, info fs.FileInfo, _ error) error {
if !info.IsDir() {
if info.Name() == "main.go" {
Expand All @@ -30,6 +42,15 @@ func findMainFile() (filePath string) {

func Start() {
mainFile := findMainFile()

// Set SERVICEWEAVER_CONFIG environment variable if not already set
if _, ok := os.LookupEnv("SERVICEWEAVER_CONFIG"); !ok {
err := os.Setenv("SERVICEWEAVER_CONFIG", "./weaver.toml")
if err != nil {
panic(fmt.Sprintf("failed to set env var SERVICEWEAVER_CONFIG: %s", err))
}
}

cmd := exec.Command("go", "run", mainFile)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Expand All @@ -42,19 +63,25 @@ func Start() {
panic("cmd run failed: " + err.Error())
}
time.Sleep(time.Second)
fmt.Printf(`
__ __ ______ __ ____
/ /_ ____ ____ ___ / / ___ _____ _____ / ____/ / / / _/
/ __ \ / __ \ / __ \ / _ \ / / / _ \ / ___/ / ___/ / / / / / /
/ /_/ // /_/ / / / / // __/ / / / __/ (__ ) (__ ) / /___ / /___ _/ /
/_.___/ \____/ /_/ /_/ \___/ /_/ \___/ /____/ /____/ \____/ /_____//___/
println(msgRunning)

running...
`)
go CloseBySignal(cmd.Process.Pid)

err = cmd.Wait()
if err != nil {
println(err.Error())
}
}

func CloseBySignal(pid int) {
println("PID:", pid)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
select {
default:
switch <-ch {
case syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM:
syscall.Kill(-pid, syscall.SIGKILL)
}
}
}
1 change: 1 addition & 0 deletions internal/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ReadToml(componentName string) (qsConn string) {
panic(err)
}

// TODO: improve that same internal/run.go:46
weaverToml, err := ioutil.ReadFile(pwd + "/weaver.toml")
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

const Version = "v0.1.1"
const Version = "v0.2.0"

func ValidateLatestVersion() {
cmd := exec.Command("go", "list", "-m", "github.com/renanbastos93/boneless@latest")
Expand Down
1 change: 1 addition & 0 deletions templates/files/component.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/ServiceWeaver/weaver"
_ "github.com/go-sql-driver/mysql"
"{{.Module}}/internal/{{.ComponentName}}/store"
)

Expand Down

0 comments on commit 5dc55b3

Please sign in to comment.