first commit.

way too late.
This commit is contained in:
didier amyot committed 2015-01-31 10:19:31 -08:00
commit dbbfaf99b6
15 files changed
+757

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
package nsqsubscriber
import "github.com/bitly/go-nsq"
type NSQMessage struct {
*nsq.Message
}
func (m *NSQMessage) Body() []byte {
return m.Message.Body
}
func (m *NSQMessage) Timestamp() int64 {
return m.Message.Timestamp
}
+40
View File
@@ -0,0 +1,40 @@
package nsqsubscriber
import (
"encoding/json"
"code.google.com/p/go-uuid/uuid"
"github.com/bitly/go-nsq"
"github.com/didiercrunch/doorman/shared"
"gopkg.in/mgo.v2/bson"
)
var UUID = uuid.New()
type NSQSubscriber struct {
NSQLookupURL string
}
func toNSQHandlerFunc(update shared.UpdateHandlerFunc) nsq.HandlerFunc {
return func(message *nsq.Message) error {
wu := &shared.DoormanUpdater{}
if err := json.Unmarshal(message.Body, wu); err != nil {
return err
}
wu.Timestamp = message.Timestamp
return update(wu)
}
}
func (sub *NSQSubscriber) Subscribe(doormanId bson.ObjectId, update shared.UpdateHandlerFunc) error {
config := nsq.NewConfig()
q, err := nsq.NewConsumer(doormanId.Hex(), UUID, config)
if err != nil {
return err
}
q.AddHandler(nsq.HandlerFunc(toNSQHandlerFunc(update)))
if err := q.ConnectToNSQLookupd(sub.NSQLookupURL); err != nil {
return err
}
return nil
}
+19
View File
@@ -0,0 +1,19 @@
package nsqsubscriber
import (
"testing"
"github.com/bitly/go-nsq"
)
func TestNSQMessage(t *testing.T) {
m := &nsq.Message{Body: []byte("hello world"), Timestamp: 10}
nsqM := &NSQMessage{m}
if string(nsqM.Body()) != "hello world" {
t.Error()
}
if nsqM.Timestamp() != 10 {
t.Error()
}
}