package mq

import (
	"encoding/json"

	"mongo.games.com/goserver/core/broker"
	"mongo.games.com/goserver/core/broker/rabbitmq"

	"mongo.games.com/game/dbproxy/svc"
	"mongo.games.com/game/model"
	"mongo.games.com/game/mq"
)

func init() {
	mq.RegisterSubscriber(model.OnlineLogCollName, func(e broker.Event) (err error) {
		msg := e.Message()
		if msg != nil {
			defer func() {
				if err != nil {
					mq.BackUp(e, err)
				}

				e.Ack()

				recover()
			}()

			var log model.OnlineLog
			err = json.Unmarshal(msg.Body, &log)
			if err != nil {
				return
			}

			c := new(svc.OnlineLogSvc)
			if c != nil {
				ret := false
				err = c.InsertSignleOnlineLog(&log, &ret)
			}
			return
		}
		return nil
	}, broker.Queue(model.OnlineLogCollName), broker.DisableAutoAck(), rabbitmq.DurableQueue())
}
