示例#1
0
文件: service.go 项目: cyanly/gotrade
func (self *Service) Start() chan bool {
	//SIGINT or SIGTERM is caught
	quitChannel := make(chan os.Signal)
	signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
	shutdownCallerChannel := make(chan bool)
	go func() {
		<-quitChannel
		self.shutdownChannel <- true
		shutdownCallerChannel <- true
	}()

	self.Status = proto.RUNNING

	// Immediately publish heartbeat
	self.lastHBMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339)
	self.lastHBMsg.Status = self.Status
	if data, _ := self.lastHBMsg.Marshal(); data != nil {
		self.messageBus.Publish("service.Heartbeat."+self.Config.ServiceName, data)
	}

	log.Infof("Service [%v] Started", self.Config.ServiceName)
	return shutdownCallerChannel
}
示例#2
0
文件: service.go 项目: cyanly/gotrade
func NewService(c Config) *Service {

	// Hardware Info
	uuid = fmt.Sprint(hostname, ":", pid)
	log.Infof("Service [%v] starting @ %v", c.ServiceName, uuid)

	// Service handle
	svc := &Service{
		Config:          c,
		Status:          proto.STARTING,
		shutdownChannel: make(chan bool),
	}

	// Messaging bus
	messageBus, err := messagebus.Connect(svc.Config.MessageBusURL)
	svc.messageBus = messageBus
	if err != nil {
		log.WithField("MessageBusURL", svc.Config.MessageBusURL).Fatal("error: Cannot connect to message bus")
	}

	//Heartbeating
	currDateTime := time.Now().UTC().Format(time.RFC3339)
	hbMsg := &proto.Heartbeat{
		Name:             svc.Config.ServiceName,
		Id:               uuid,
		Status:           proto.STARTING,
		Machine:          hostname,
		CreationDatetime: currDateTime,
		CurrentDatetime:  currDateTime,
	}
	svc.lastHBMsg = hbMsg
	hbTicker := time.NewTicker(time.Second * time.Duration(svc.Config.HeartbeatFreq))
	go func(shutdownChannel chan bool) {
		publish_address := "service.Heartbeat." + svc.Config.ServiceName

		for range hbTicker.C {
			hbMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339)
			hbMsg.Status = svc.Status

			if data, _ := hbMsg.Marshal(); data != nil {
				messageBus.Publish(publish_address, data)
			}

			select {
			case <-shutdownChannel:
				hbTicker.Stop()

				//Publish Stop heartbeat
				if svc.Status != proto.ERROR {
					svc.Status = proto.STOPPED
				}
				hbMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339)
				hbMsg.Status = svc.Status
				if data, _ := hbMsg.Marshal(); data != nil {
					messageBus.Publish(publish_address, data)
				}

				messageBus.Close()

				log.Info("Server Terminated")
				return
			}
		}
	}(svc.shutdownChannel)

	return svc
}
示例#3
0
func TestLogger_printf(t *testing.T) {
	l.Infof("logged in %s", "Chao")
}
示例#4
0
// Initialise OrderRouter instance and set up topic subscribers
func NewOrderRouter(c Config) *OrderRouter {

	or := &OrderRouter{
		Config: c,

		// internal variables
		stopChan: make(chan bool),
		mclist:   map[string]time.Time{},
		mcChan:   make(chan *service.Heartbeat),
		reqChan:  make(chan OrderRequest),
	}

	// Connect to database storage driver
	if storage, err := database.NewOrderStore(c.DatabaseDriver, c.DatabaseUrl, nil); err != nil {
		log.Fatalf("error: Cannot connect to database driver %v @ %v", c.DatabaseDriver, c.DatabaseUrl)
	} else {
		or.orderStore = storage
	}

	// Keep a list of active MarketConnectors
	if ncSvc, err := messagebus.Connect(c.ServiceMessageBusURL); err != nil {
		log.Fatalf("error: Cannot connect to service message bus @ %v", c.ServiceMessageBusURL)
	} else {
		or.msgbusService = ncSvc
		ncSvc.Subscribe("service.Heartbeat.MC.>", func(m *messagebus.Msg) {
			hbMsg := &service.Heartbeat{}
			if err := hbMsg.Unmarshal(m.Data); err == nil {
				or.mcChan <- hbMsg
			}
		})
	}

	// Order requests processors subscribing
	if msgbus, err := messagebus.Connect(c.MessageBusURL); err != nil {
		log.Fatalf("error: Cannot connect to order message bus @ %v", c.MessageBusURL)
	} else {
		or.msgbus = msgbus

		//CL->OR order NEW request
		msgbus.Subscribe("order.NewOrderRequest", func(m *messagebus.Msg) {
			request := new(proto.NewOrderRequest)
			if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 {
				// validate basic fields
				if request.Order == nil {
					// empty request
					return
				}
				if request.Order.MarketConnector == "" {
					// empty market connect
					return
				}

				orReq := OrderRequest{
					ReplyAddr:   m.Reply,
					RequestType: REQ_NEW,
					Request:     request,
				}
				oO := order.Order{
					Order: request.Order,
				}
				log.Infof("CL->OR NEW %v", oO.String())
				or.reqChan <- orReq
			}
		})

		//CL->OR order CANCEL request
		msgbus.Subscribe("order.CancelOrderRequest", func(m *messagebus.Msg) {
			request := new(proto.CancelOrderRequest)
			if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 {
				// validate basic fields
				if request.OrderId == 0 {
					// request without order id
					return
				}

				orReq := OrderRequest{
					ReplyAddr:   m.Reply,
					RequestType: REQ_CANCEL,
					Request:     request,
					OrderId:     request.OrderId,
				}

				log.Infof("CL->OR CXL OrderKey := %v OrderId := %v", request.OrderKey, request.OrderId)
				or.reqChan <- orReq
			}
		})

		//CL->OR order REPLACE request
		msgbus.Subscribe("order.ReplaceOrderRequest", func(m *messagebus.Msg) {
			request := new(proto.ReplaceOrderRequest)
			if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 {
				// validate basic fields
				if request.Order == nil {
					// empty request
					return
				}
				if request.Order.OrderId == 0 {
					// request without order id
					return
				}
				if request.Order.MarketConnector == "" {
					// empty market connect
					return
				}

				orReq := OrderRequest{
					ReplyAddr:   m.Reply,
					RequestType: REQ_REPLACE,
					Request:     request,
					OrderId:     request.Order.OrderId,
				}

				log.Infof("CL->OR RPL OrderKey := %v OrderId := %v", request.Order.OrderKey, request.Order.OrderId)
				or.reqChan <- orReq

			}
		})
	}

	return or
}
示例#5
0
//Use Message Cracker on Incoming Application Messages
func (e *Executor) FromApp(msg quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) {
	log.Infof("FIX->SIM: FIX44NewOrderSingle \n%v", msg.String())
	return e.Route(msg, sessionID)
}
示例#6
0
func (app FIXClient) FromApp(msg quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) {
	log.Infof("FIX->MC EXEC: \n%v", msg.String())
	return app.Route(msg, sessionID)
}