2017-06-20 15:58:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-27 17:44:59 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-08-09 12:12:04 +02:00
|
|
|
"log"
|
2017-10-17 14:23:48 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-01-17 10:51:19 +02:00
|
|
|
"runtime"
|
2019-04-11 16:07:09 +02:00
|
|
|
"syscall"
|
2019-01-17 10:51:19 +02:00
|
|
|
"time"
|
2017-06-20 15:58:55 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 18:21:17 +02:00
|
|
|
const version = "2.11.0"
|
2018-09-06 15:02:21 +02:00
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
type ctxKey string
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
func initialize() error {
|
2019-08-09 12:12:04 +02:00
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
if err := initLog(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := configure(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initNewrelic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-06 15:26:00 +02:00
|
|
|
initPrometheus()
|
2020-02-27 17:44:59 +02:00
|
|
|
|
|
|
|
if err := initDownloading(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-06 15:26:00 +02:00
|
|
|
initErrorsReporting()
|
2020-02-27 17:44:59 +02:00
|
|
|
|
|
|
|
if err := initVips(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-11 13:54:46 +02:00
|
|
|
|
|
|
|
if err := checkPresets(conf.Presets); err != nil {
|
|
|
|
shutdownVips()
|
2020-02-27 17:44:59 +02:00
|
|
|
return err
|
2019-11-11 13:54:46 +02:00
|
|
|
}
|
2020-02-27 17:44:59 +02:00
|
|
|
|
|
|
|
return nil
|
2019-06-06 15:26:00 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
func run() error {
|
|
|
|
if err := initialize(); err != nil {
|
|
|
|
return err
|
2020-02-04 13:48:33 +02:00
|
|
|
}
|
|
|
|
|
2020-02-04 12:18:09 +02:00
|
|
|
defer shutdownVips()
|
2019-06-06 15:26:00 +02:00
|
|
|
|
2019-01-17 10:51:19 +02:00
|
|
|
go func() {
|
|
|
|
var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
|
|
|
|
|
|
|
|
for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
|
2020-01-10 16:52:40 +02:00
|
|
|
freeMemory()
|
2019-01-17 10:51:19 +02:00
|
|
|
|
|
|
|
if logMemStats {
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
2019-09-16 11:53:45 +02:00
|
|
|
logDebug("MEMORY USAGE: Sys=%d HeapIdle=%d HeapInuse=%d", m.Sys/1024/1024, m.HeapIdle/1024/1024, m.HeapInuse/1024/1024)
|
2019-01-17 10:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
if prometheusEnabled {
|
|
|
|
if err := startPrometheusServer(cancel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := startServer(cancel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-04 12:18:09 +02:00
|
|
|
defer shutdownServer(s)
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-10-17 14:23:48 +02:00
|
|
|
stop := make(chan os.Signal, 1)
|
2019-04-11 16:07:09 +02:00
|
|
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-stop:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "health":
|
|
|
|
os.Exit(healthcheck())
|
|
|
|
case "version":
|
|
|
|
fmt.Println(version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := run(); err != nil {
|
|
|
|
logFatal(err.Error())
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|