Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Gracefully shutdown on sigterm #159

Merged
merged 4 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion weaver/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ docs/
# Others
**/.DS_STORE
**/.git*
**/*.env
**/*.env

vendor/
15 changes: 8 additions & 7 deletions weaver/Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM golang:1.7-alpine
MAINTAINER Arachnys <techteam@arachnys.com>
FROM golang:1.10-alpine
WORKDIR /go/src/github.com/arachnys/athenapdf/weaver

RUN apk add --update git
RUN go get -u github.com/golang/dep/cmd/dep

COPY . /go/src/github.com/arachnys/athenapdf/weaver
WORKDIR /go/src/github.com/arachnys/athenapdf/weaver
COPY Gopkg.lock Gopkg.toml ./
RUN dep ensure --vendor-only -v

COPY . ./

RUN \
go get -v -d \
&& go install -v \
&& CGO_ENABLED=0 go build -ldflags "-s" -a -installsuffix cgo -o weaver .
CGO_ENABLED=0 go build -v -o weaver .

CMD ["/bin/sh"]
181 changes: 181 additions & 0 deletions weaver/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions weaver/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
branch = "master"
name = "github.com/DeanThompson/ginpprof"

[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.14.12"

[[constraint]]
branch = "master"
name = "github.com/getsentry/raven-go"

[[constraint]]
branch = "master"
name = "github.com/gin-gonic/contrib"

[[constraint]]
name = "github.com/gin-gonic/gin"
version = "1.2.0"

[[constraint]]
name = "github.com/satori/go.uuid"
version = "1.2.0"

[[constraint]]
branch = "master"
name = "golang.org/x/net"

[[constraint]]
name = "gopkg.in/alexcesaro/statsd.v2"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
2 changes: 0 additions & 2 deletions weaver/conf/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ set -e
# Block bad hosts
cat conf/hosts >> /etc/hosts

# Start X
rm -f /tmp/.X99-lock
Xvfb :99 -ac -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:99

exec "$@"
5 changes: 1 addition & 4 deletions weaver/converter/cloudconvert/cloudconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ func (c CloudConvert) Convert(s converter.ConversionSource, done <-chan struct{}
Wait: true,
}

u, err := uuid.NewV4()
if err != nil {
return nil, err
}
u := uuid.NewV4()

if c.AWSS3.S3Bucket == "" || c.AWSS3.S3Key == "" {
conv.Download = "inline"
Expand Down
4 changes: 4 additions & 0 deletions weaver/gcmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os/exec"
"syscall"
)

var (
Expand All @@ -19,6 +20,9 @@ var (
// Goroutine.
func Execute(c []string, terminate <-chan struct{}) ([]byte, error) {
cmd := exec.Command(c[0], c[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cout := make(chan []byte, 1)
cerr := make(chan error, 1)

Expand Down
38 changes: 37 additions & 1 deletion weaver/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package main

import (
"context"
"github.com/DeanThompson/ginpprof"
"github.com/arachnys/athenapdf/weaver/converter"
"github.com/getsentry/raven-go"
"github.com/gin-gonic/contrib/sentry"
"github.com/gin-gonic/gin"
"gopkg.in/alexcesaro/statsd.v2"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
)

Expand Down Expand Up @@ -76,6 +82,15 @@ func InitSimpleRoutes(router *gin.Engine, conf Config) {
}
}

func StartX() {
cmd := exec.Command("Xvfb", ":99", "-ac", "-screen", "0", "1024x768x24")
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
err := cmd.Run()
log.Fatal("Xvfb exited:", err)
}

func main() {
router := gin.Default()
// Get config vars from the environment
Expand All @@ -98,5 +113,26 @@ func main() {
}()
}

log.Fatal(router.Run(conf.HTTPAddr))
server := &http.Server{
Addr: conf.HTTPAddr,
Handler: router,
}

go func() {
log.Println(server.ListenAndServe())
}()

go StartX()

sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

<-sigChan
log.Println("Received sigterm, gracefully shutting down")
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Println("Error:", err)
}

}