Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
container/process: refactor container api
Browse files Browse the repository at this point in the history
- Wait semantics are more managable
- Containers are started by RunContained returning a running container
- Can send shutdown signal to container explicitly via function call
with timeout/delay before hard kill
- Can wait for a container to be done and return results
  • Loading branch information
justenwalker committed Aug 12, 2019
1 parent cc517a5 commit 7701215
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 316 deletions.
20 changes: 20 additions & 0 deletions cmd/standalone/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DefaultLogMaxFiles = 5
const DefaultMetricsEndpoint = "/metrics"

const (
EnvDamonContainerName = "DAMON_CONTAINER_NAME"
EnvDamonLogMaxSizeMB = "DAMON_LOG_MAX_SIZE"
EnvDamonLogMaxFiles = "DAMON_LOG_MAX_FILES"
EnvDamonLogDir = "DAMON_LOG_DIR"
Expand Down Expand Up @@ -105,6 +106,15 @@ func envToBool(env string, def bool) bool {
return def
}

func envStr(def string, envs ...string) string {
for _, e := range envs {
if env := os.Getenv(e); env != "" {
return env
}
}
return def
}

func envToInt(def int64, envs ...string) (int64, error) {
for _, e := range envs {
if env := os.Getenv(e); env != "" {
Expand Down Expand Up @@ -135,8 +145,18 @@ func MetricsEndpoint() string {
return DefaultMetricsEndpoint
}

func nomadContainerName() string {
if alloc, name := os.Getenv(EnvNomadAllocID), os.Getenv(EnvNomadTaskName); alloc != "" && name != "" {
return fmt.Sprintf("damon:%s.%s", alloc, name)
}
return ""
}

func LoadContainerConfigFromEnvironment() (container.Config, error) {
var cfg container.Config
if env := os.Getenv(EnvDamonContainerName); env != "" {
cfg.Name = envStr("", EnvDamonContainerName, nomadContainerName())
}
cpu, err := envToInt(0, EnvDamonCPULimit, EnvNomadCPULimit)
if err != nil {
return cfg, err
Expand Down
36 changes: 14 additions & 22 deletions cmd/standalone/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"time"

"github.com/jet/damon/container"
"github.com/jet/damon/log"
Expand Down Expand Up @@ -55,6 +57,7 @@ func main() {
if err != nil {
logger.Error(err, "unable to load container configuration from environment variables")
}
ccfg.Logger = clogger
win32.SetLogger(logger)
resources := win32.GetSystemResources()
labels := make(map[string]string)
Expand All @@ -68,27 +71,16 @@ func main() {
Labels: labels,
}
m.Init()
c := container.Container{
Command: cmd,
Config: ccfg,
Logger: clogger,
OnStats: func(s container.ProcessStats) {
m.OnStats(s)
},
OnViolation: func(v container.LimitViolation) {
m.OnViolation(v)
},
}
if err := c.Start(); err != nil {
c, err := container.RunContained(cmd, &ccfg)
if err != nil {
logger.Error(err, "damon startup error")
os.Exit(1)
}
exitCh := make(chan struct{})
sigCh := make(chan os.Signal)
signal.Notify(sigCh)
go func() {
<-sigCh
close(exitCh)
c.Shutdown(30 * time.Second)
}()
if addr := ListenAddress(); addr != "" {
go func() {
Expand All @@ -103,23 +95,23 @@ func main() {
logger.Error(srv.ListenAndServe(), "error closing http server")
}()
}
pr, err := c.Wait(exitCh)
if err != nil {
pr, _ := c.WaitForResult(context.Background())
end := time.Now()
if pr.Err != nil {
logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
}).Error(err, "process exited with an error")
}

logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
"start": pr.Start,
"end": pr.End,
"run_time": pr.End.Sub(pr.Start),
"exit_status": pr.ExitCode,
"start": c.StartTime,
"end": end,
"run_time": end.Sub(c.StartTime),
"exit_status": pr.ExitStatus,
}).Logln("damon exiting")
os.Exit(pr.ExitCode)
os.Exit(pr.ExitStatus)
}
Loading

0 comments on commit 7701215

Please sign in to comment.