Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #26 from fourcube/add-version-and-pidfile-arguments
Browse files Browse the repository at this point in the history
Add `-v` and `-pidFile` arguments
  • Loading branch information
fourcube authored Oct 11, 2018
2 parents 77a61af + be24e04 commit afc09e3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 13 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.1
6 changes: 4 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ CONFIGURATIONS=(
solaris,amd64
)

version=`cat VERSION`

for config in ${CONFIGURATIONS[@]}; do
IFS=","
set $config
Expand All @@ -25,10 +27,10 @@ if [ $os = "windows" ]; then
bin_name="$bin_name.exe"
fi

GOOS="$os" GOARCH="$arch" go build -o "$path/$bin_name"
GOOS="$os" GOARCH="$arch" go build -ldflags "-X main.Version=${version}" -o "$path/$bin_name"
cp -r $GOPATH/src/github.com/fourcube/goiban-data-loader/data "$path/"
cp -r ./static "$path/"
tar czvf "build/goiban-service-$os-$arch.tar.gz" -C "$base_path" goiban-service
tar czvf "build/goiban-service-$version-$os-$arch.tar.gz" -C "$base_path" goiban-service

unset IFS;
done
64 changes: 53 additions & 11 deletions goiban_service.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -42,22 +44,28 @@ var (
metrics *m.KeenMetrics
inmemMetrics = m.NewInmemMetricsRegister()
repo data.BankDataRepository
// Set at link time
Version string = "dev"
// Flags
dataPath string
staticPath string
mysqlURL string
port string
help bool
web bool
dataPath string
staticPath string
mysqlURL string
pidFile string
port string
help bool
web bool
printVersion bool
)

func init() {
flag.StringVar(&dataPath, "dataPath", "", "Base path of the bank data")
flag.StringVar(&staticPath, "staticPath", "", "Base path of the static web content")
flag.StringVar(&mysqlURL, "dbUrl", "", "Database connection string")
flag.StringVar(&pidFile, "pidFile", "", "PID File path")

flag.StringVar(&port, "port", "8080", "HTTP Port or interface to listen on")
flag.BoolVar(&help, "h", false, "Show usage")
flag.BoolVar(&printVersion, "v", false, "Show version")
flag.BoolVar(&web, "w", false, "Serve staticPath folder")
}

Expand All @@ -69,6 +77,15 @@ func main() {
return
}

if printVersion {
fmt.Println(Version)
return
}

if pidFile != "" {
CreatePidfile(pidFile)
}

if web && staticPath == "" {
// Try to serve from the package src directory
path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "fourcube", "goiban-service", "static")
Expand Down Expand Up @@ -126,23 +143,48 @@ func listen() {
listeningInfo := "Listening on %s"
handler := corsHandler.Handler(router)

var server http.Server
var addr string

idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint

log.Printf("Received SIGINT. Waiting for connections to close...")

// We received an interrupt signal, shut down.
if err := server.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
close(idleConnsClosed)
}()

if strings.ContainsAny(port, ":") {
if web {
listeningInfo = fmt.Sprintf(listeningInfo, "%s (serving static content from '/').")
}
log.Printf(listeningInfo, port)
err = http.ListenAndServe(port, handler)
addr = port
} else {
if web {
listeningInfo = fmt.Sprintf(listeningInfo, ":%s (serving static content from '/').")
}
log.Printf(listeningInfo, port)
err = http.ListenAndServe(":"+port, handler)
addr = ":" + port
}

if err != nil {
server.Handler = handler
server.Addr = addr

log.Printf("goiban-service (v%s)", Version)
log.Printf(listeningInfo, port)

if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal("ListenAndServe: ", err)
}

<-idleConnsClosed
}

// Processes requests to the /validate/ url
Expand Down
66 changes: 66 additions & 0 deletions pid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"syscall"
)

// CreatePidfile creates a pidfile.
// If the file already exists, it checks if the process is already running and terminates.
func CreatePidfile(pidFile string) {
if pidFile != "" {
if err := os.MkdirAll(filepath.Dir(pidFile), os.FileMode(0755)); err != nil {
log.Fatalf("Could not create path to pidfile %v", err)
}

if _, err := os.Stat(pidFile); err != nil && !os.IsNotExist(err) {
log.Fatalf("Failed to stat pidfile %v", err)
}

f, err := os.OpenFile(pidFile, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Fatalf("Failed to open pidfile %v", err)
}
defer f.Close()

if pidBytes, err := ioutil.ReadAll(f); err != nil {
log.Fatalf("Failed to read from pidfile %v")
} else {
if len(pidBytes) == 0 {
goto foo
}

pid, err := strconv.Atoi(string(pidBytes))
if err != nil {
log.Fatalf("Invalid pid %v", err)
}

process, err := os.FindProcess(pid)
if err != nil {
log.Fatalf("Failed to find process %v, please delete the pid file %s manually", err, pidFile)
}

if err := process.Signal(syscall.Signal(0)); err == nil {
log.Fatalf("Process %d still running, please stop the process and delete the pid file %s manually", pid, pidFile)
}
}

foo:
if err = f.Truncate(0); err != nil {
log.Fatalf("Failed to truncate pidfile", err)
}
if _, err = f.Seek(0, 0); err != nil {
log.Fatalf("Failed to seek pidfile", err)
}

_, err = fmt.Fprintf(f, "%d", os.Getpid())
if err != nil {
log.Fatalf("Failed to write pidfile %v", err)
}
}
}

0 comments on commit afc09e3

Please sign in to comment.