Skip to content

Commit 0d71d28

Browse files
committed
Merge branch 'master' of https://github.com/h2non/imaginary
2 parents c2825a6 + 06a6c80 commit 0d71d28

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ COPY --from=builder /etc/ssl/certs /etc/ssl/certs
7878
RUN DEBIAN_FRONTEND=noninteractive \
7979
apt-get update && \
8080
apt-get install --no-install-recommends -y \
81-
libglib2.0-0 libjpeg62-turbo libpng16-16 libopenexr23 \
81+
procps libglib2.0-0 libjpeg62-turbo libpng16-16 libopenexr23 \
8282
libwebp6 libwebpmux3 libwebpdemux2 libtiff5 libgif7 libexif12 libxml2 libpoppler-glib8 \
8383
libmagickwand-6.q16-6 libpango1.0-0 libmatio4 libopenslide0 \
8484
libgsf-1-114 fftw3 liborc-0.4-0 librsvg2-2 libcfitsio7 libimagequant0 libheif1 && \

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ You can enable it simply passing a flag to the binary:
205205
$ imaginary -concurrency 20
206206
```
207207

208+
### Graceful shutdown
209+
210+
When you use a cluster, it is necessary to control how the deployment is executed, and it is very useful to finish the containers in a controlled.
211+
212+
You can use the next command:
213+
214+
```
215+
$ ps auxw | grep 'bin/imaginary' | awk 'NR>1{print buf}{buf = $2}' | xargs kill -TERM > /dev/null 2>&1
216+
```
217+
208218
### Scalability
209219

210220
If you're looking for a large scale solution for massive image processing, you should scale `imaginary` horizontally, distributing the HTTP load across a pool of imaginary servers.

imaginary.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ func main() {
219219
LoadSources(opts)
220220

221221
// Start the server
222-
err := Server(opts)
223-
if err != nil {
224-
exitWithError("cannot start the server: %s", err)
225-
}
222+
Server(opts)
226223
}
227224

228225
func getPort(port int) int {

server.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"net/http"
56
"net/url"
7+
"log"
68
"os"
9+
"os/signal"
10+
"syscall"
711
"path"
812
"strconv"
913
"strings"
@@ -56,7 +60,7 @@ func (e Endpoints) IsValid(r *http.Request) bool {
5660
return true
5761
}
5862

59-
func Server(o ServerOptions) error {
63+
func Server(o ServerOptions) {
6064
addr := o.Address + ":" + strconv.Itoa(o.Port)
6165
handler := NewLog(NewServerMux(o), os.Stdout, o.LogLevel)
6266

@@ -68,7 +72,27 @@ func Server(o ServerOptions) error {
6872
WriteTimeout: time.Duration(o.HTTPWriteTimeout) * time.Second,
6973
}
7074

71-
return listenAndServe(server, o)
75+
done := make(chan os.Signal, 1)
76+
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
77+
78+
go func() {
79+
if err := listenAndServe(server, o); err != nil && err != http.ErrServerClosed {
80+
log.Fatalf("listen: %s\n", err)
81+
}
82+
}()
83+
84+
<-done
85+
log.Print("Graceful shutdown")
86+
87+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
88+
defer func() {
89+
// extra handling here
90+
cancel()
91+
}()
92+
93+
if err := server.Shutdown(ctx); err != nil {
94+
log.Fatalf("Server Shutdown Failed:%+v", err)
95+
}
7296
}
7397

7498
func listenAndServe(s *http.Server, o ServerOptions) error {

0 commit comments

Comments
 (0)