From 92d308879ba7d0aebdc4757a9b7592eeead626df Mon Sep 17 00:00:00 2001 From: Howl Date: Sat, 9 Apr 2016 18:14:38 +0200 Subject: [PATCH] Add /meta/{up_since,kill} --- app/start.go | 2 ++ app/v1/init.go | 9 +++++++++ app/v1/meta.go | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/v1/init.go diff --git a/app/start.go b/app/start.go index 6f66fee..37ce668 100644 --- a/app/start.go +++ b/app/start.go @@ -50,6 +50,8 @@ func Start(conf common.Conf, db *sql.DB) *gin.Engine { // T E -- the one who said "wow that's so meta" // A T E M gv1.GET("/meta/restart", Method(v1.MetaRestartGET, db, common.PrivilegeAPIMeta)) + gv1.GET("/meta/kill", Method(v1.MetaKillGET, db, common.PrivilegeAPIMeta)) + gv1.GET("/meta/up_since", Method(v1.MetaUpSinceGET, db, common.PrivilegeAPIMeta)) } } diff --git a/app/v1/init.go b/app/v1/init.go new file mode 100644 index 0000000..fab45d1 --- /dev/null +++ b/app/v1/init.go @@ -0,0 +1,9 @@ +package v1 + +import ( + "time" +) + +func init() { + upSince = time.Now() +} diff --git a/app/v1/meta.go b/app/v1/meta.go index 42d41cc..42d9260 100644 --- a/app/v1/meta.go +++ b/app/v1/meta.go @@ -1,8 +1,10 @@ package v1 import ( + "fmt" "os" "syscall" + "time" "github.com/osuripple/api/common" ) @@ -17,6 +19,40 @@ func MetaRestartGET(md common.MethodData) (r common.Response) { } r.Code = 200 r.Message = "brb" - go proc.Signal(syscall.SIGUSR2) + go func() { + time.Sleep(time.Second) + proc.Signal(syscall.SIGUSR2) + }() return } + +// MetaKillGET kills the API process. NOTE TO EVERYONE: NEVER. EVER. USE IN PROD. +// Mainly created because I couldn't bother to fire up a terminal, do htop and kill the API each time. +func MetaKillGET(md common.MethodData) (r common.Response) { + proc, err := os.FindProcess(syscall.Getpid()) + if err != nil { + r.Code = 500 + r.Message = "couldn't find process. what the fuck?" + return + } + const form = "02/01/2006" + r.Code = 200 + // yes + r.Message = fmt.Sprintf("RIP ripple API %s - %s", upSince.Format(form), time.Now().Format(form)) + go func() { + time.Sleep(time.Second) + proc.Kill() + }() + return +} + +var upSince time.Time + +// MetaUpSinceGET retrieves the moment the API application was started. +// Mainly used to get if the API was restarted. +func MetaUpSinceGET(md common.MethodData) common.Response { + return common.Response{ + Code: 200, + Data: upSince.UnixNano(), + } +}