Example #1
0
func listenToOpLog(subHubs []*subscriptionHub) {
	//Address of where the database for mongo is located
	//in this case it is on the same machine
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true)
	ops, errs := gtm.Tail(session, &gtm.Options{
		Filter: filterLog,
	})

	for {
		select {
		case err := <-errs:
			fmt.Println(err)
		case op := <-ops:
			name, nameOk := op.Data["name"].(string)
			value, valueOk := op.Data["value"].(float64)

			if nameOk && valueOk {
				for _, subHub := range subHubs {
					subHub.events <- &event{
						Name:  name,
						Value: value,
					}
				}
			} else {
				fmt.Println("name: ", nameOk, " value: ", valueOk)
				fmt.Println(op.Data)
			}
		}
	}
}
Example #2
0
func main() {
	db, err := mgo.Dial(os.Getenv("MONGO_URL"))
	if err != nil {
		log.Error("Failed to connect to MongoDB", "error", err)
		os.Exit(1)
		return
	}
	defer db.Close()

	ops, errs := gtm.Tail(db, &gtm.Options{
		OpLogCollectionName: &OplogCollection,
		Filter: func(op *gtm.Op) bool {
			return op.Namespace == "dg.lists"
		},
	})

	for {
		select {
		case err = <-errs:
			log.Error("Error from oplog tail", "error", err)
		case <-ops:
			log.Info("Change event")
			Reload()
		}
	}
}