I want to write a background program that will monitor the _changes feed of a CouchDB, and possibly update the document. The problem is that the update causes another _change, and I get an endless loop! What's the best way to avoid this?
For example, here is the specific scenario: I have a CouchApp where users modify documents through their browser. I also have a python program that creates a PDF version of a document and then attaches it as an attached file to the document itself. My problem is that doing the PUT Attachment to upload the PDF also triggers a document change. I have to be able to tell whether a change is being caused by the PDF upload, or not. It seems like it should be easy, but I can't think of a simple way to do it. I would rather keep the PDF generator program be "stateless", keeping any required state in the db itself.
Now, this can easily be done if I require that users who change the document set some sort of flag on the document to indicate that it needs to be processed. The trick is how to do it without requiring that.
I have come to the conclusion that a "_changes" listener should never modify the document that it listens to. In my case I decided to attach my PDF file to a separate document, in a separate "database" within couchdb, but using the same "_id" to make it easy to correlate. That way I don't trigger a "_change" on the same documents that I am listening to. I could not get past the need to require every client that changes the document to somehow "flag" it as requiring processing ( by deleting the existing attachment, or otherwise setting some "dirty" flag ). After much pondering, I think that this will be a rule-of-thumb for me : that you must not modify a document upon receiving a "_change" notification for that document. Has anyone else reached the same conclusion?
Use a filter function and and filter out the second change – either by the document structure change or by setting an additional flag to the changed document:
function(doc, req)
{
if(!doc.hasStructuralChange) { //fix this
return true;
}
return false;
}
or
function(doc, req)
{
if(!doc.changed) { //set doc.changed during first update
return true;
}
return false;
}
Edit: You can check for an attachment via if (doc._attachments)