rwynn / gtm

gtm (go tail mongo) is a MongoDB event listener
MIT License
147 stars 33 forks source link

How to specify the After option ? #2

Closed jkernech closed 8 years ago

jkernech commented 8 years ago

I have tried to specify the After property of gtm.option but when I try to specify the After property with a MongoTimestamp, I have the following error :

(type bson.MongoTimestamp) as type gtm.TimestampGenerator in field value

Could you provide an example (usage of the field) and explain why you decided not to use the type bson.MongoTimestamp ?

Thanks

rwynn commented 8 years ago

The gtm.TimestampGenerator is typed as a function. So you can use code like this if you just have a timestamp you want to use:

    var ts bson.MongoTimestamp = 0
    ops, errs := gtm.Tail(session, &gtm.Options {
        After: func (session *mgo.Session, options *gtm.Options) bson.MongoTimestamp { 
            return ts
        },
    })

Regarding why this field is a function and not simply a bson.MongoTimestamp: I don't have a great reason. It's a case of an implementation detail leaking into the interface. If you look at the gtm.go source you will see the default function, gtm.LastOpTimestamp, which reads and returns the most recent timestamp from the oplog. If After is provided it directly replaces this default function.

If your program saves the timestamp of the last event processed using gtm in Mongo, then your After function could be used to retrieve that timestamp using the mgo.Session in order to "resume" tailing the oplog where you last left off.

jkernech commented 8 years ago

Hi @rwynn, I have now a better understanding of how it works and more importantly why you chose this design :) Thank you for this detailled answer !