예제 #1
0
func (m *Machine) ChangeState(status utils.Status) error {
	log.Debugf("  change state of machine (%s, %s)", m.Name, status.String())

	pons := nsqp.New()
	if err := pons.Connect(meta.MC.NSQd[0]); err != nil {
		return err
	}

	bytes, err := json.Marshal(
		carton.Requests{
			CatId:     m.CartonsId,
			Action:    status.String(),
			Category:  carton.STATE,
			CreatedAt: time.Now().Local().Format(time.RFC822),
		})

	if err != nil {
		return err
	}

	log.Debugf("  pub to topic (%s, %s)", TOPIC, bytes)
	if err = pons.Publish(TOPIC, bytes); err != nil {
		return err
	}

	defer pons.Stop()
	return nil
}
예제 #2
0
func notify(boxName string, messages []interface{}) error {
	pons := nsqp.New()

	if err := pons.Connect(meta.MC.NSQd[0]); err != nil {
		return err
	}

	defer pons.Stop()

	for _, msg := range messages {
		log.Debugf("%s:%s", logQueue(boxName), msg)
		if err := pons.PublishJSONAsync(logQueue(boxName), msg, nil); err != nil {
			log.Errorf("Error on publish: %s", err.Error())
		}
	}
	return nil
}