ripple-api/app/v1/meta.go

59 lines
1.3 KiB
Go
Raw Normal View History

2016-04-07 17:32:48 +00:00
package v1
import (
2016-04-09 16:14:38 +00:00
"fmt"
2016-04-07 17:32:48 +00:00
"os"
"syscall"
2016-04-09 16:14:38 +00:00
"time"
2016-04-07 17:32:48 +00:00
"github.com/osuripple/api/common"
)
// MetaRestartGET restarts the API with Zero Downtime™.
func MetaRestartGET(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
}
r.Code = 200
r.Message = "brb"
2016-04-09 16:14:38 +00:00
go func() {
time.Sleep(time.Second)
proc.Signal(syscall.SIGUSR2)
}()
2016-04-07 17:32:48 +00:00
return
}
2016-04-09 16:14:38 +00:00
// 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(),
}
}