1
0
Fork 0

Use logging macros

This commit is contained in:
Gregory Eremin 2014-07-12 17:42:26 +07:00
parent 51bc5b04e1
commit 313677aa48
5 changed files with 26 additions and 45 deletions

34
main.go
View File

@ -2,31 +2,13 @@ package main
import (
"github.com/stvp/rollbar"
logpkg "log"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
)
type (
Message []byte
Key []byte
)
func NewKey(queue string, index uint) Key {
istr := strconv.FormatUint(uint64(index), 10)
key := strings.Join([]string{queue, istr}, "_")
return Key(key)
}
var (
log *logpkg.Logger
)
func HandleShutdown() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT)
@ -34,21 +16,17 @@ func HandleShutdown() {
go func() {
<-ch
SaveState()
log.Printf("State successfully persisted")
Log("State successfully persisted")
storage.Close()
rollbar.Wait()
log.Println("Storage closed")
log.Printf("Server stopped")
Log("Storage closed")
Log("Server stopped")
os.Exit(1)
}()
}
func main() {
log = logpkg.New(os.Stdout, "", logpkg.Ldate|logpkg.Lmicroseconds)
rollbar.Token = "***REMOVED***" // klit access token
rollbar.Environment = cfg.Env
SetupLogging()
SetupConfig()
SetupStorage()
SetupServer()
@ -57,8 +35,8 @@ func main() {
go KeepStatePersisted()
go PersistMessages()
log.Printf("GOMAXPROCS = %d", runtime.GOMAXPROCS(-1))
log.Printf("Starting HTTP server on port %d", cfg.Port)
Log("GOMAXPROCS = %d", runtime.GOMAXPROCS(-1))
Log("Starting HTTP server on port %d", cfg.Port)
http.ListenAndServe(cfg.PortString(), nil)
}

View File

@ -35,12 +35,9 @@ func Register(q string, msg Message) {
func Process(r *Request) {
for _, queueName := range r.Queues {
q := GetQueue(queueName)
if q.Counter.Distance() > 0 {
if msg, err := q.Fetch(); err != nil {
go r.Callback(nil)
} else {
go r.Callback(&Response{Queue: queueName, Message: msg})
}
msg, ok := q.TryFetch()
if ok {
go r.Callback(&Response{Queue: queueName, Message: msg})
return
}
}

View File

@ -46,7 +46,7 @@ func PublishHandler(w http.ResponseWriter, r *http.Request) {
queueName := r.FormValue("queue")
go Register(queueName, msg)
log.Println("Published message of", len(msg), "bytes to queue", queueName)
Log("Published message of %d bytes to queue %s", len(msg), queueName)
w.Write([]byte("OK"))
}
@ -81,7 +81,7 @@ func SubscriptionHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Queue", res.Queue)
w.Write(res.Message)
log.Println("Recieved message of", len(res.Message), "bytes from queue", res.Queue)
Log("Recieved message of %d bytes from queue %s", len(res.Message), res.Queue)
finished <- true
}

View File

@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"github.com/stvp/rollbar"
"time"
)
@ -28,8 +27,7 @@ func SaveState() {
stateJson, _ := json.Marshal(state)
key := Key(StateMetaKey)
if err := storage.Set(key, stateJson); err != nil {
rollbar.Error("error", err)
log.Printf("Failed to persist state")
Error(err, "Failed to persist state")
return
}
}
@ -40,13 +38,12 @@ func LoadState() {
stateJson, err := storage.Get(key)
if err != nil {
log.Printf("State not found")
Log("State not found")
return
}
if err := json.Unmarshal(stateJson, &state); err != nil {
rollbar.Error("error", err)
log.Printf("Failed to load state")
Log("Failed to load state")
return
}
@ -54,7 +51,7 @@ func LoadState() {
RegisterQueue(queueName, meta["wi"], meta["ri"])
}
log.Printf("State successfully loaded")
Log("State successfully loaded")
}
func KeepStatePersisted() {

View File

@ -2,10 +2,13 @@ package main
import (
"github.com/ezotrank/cabinetgo"
"github.com/stvp/rollbar"
"strconv"
"strings"
)
type (
Message []byte
Key []byte
Payload struct {
Queue *Queue
Message Message
@ -17,10 +20,16 @@ var (
saver = make(chan Payload, 1000)
)
func NewKey(queue string, index uint) Key {
istr := strconv.FormatUint(uint64(index), 10)
key := strings.Join([]string{queue, istr}, "_")
return Key(key)
}
func SetupStorage() {
err := storage.Open(cfg.Storage, cabinet.KCOWRITER|cabinet.KCOCREATE)
if err != nil {
panic(err)
Error(err, "Failed to open database '%s'", cfg.Storage)
}
}