Back at the beginning of April I released Evernote Rules, a play on Outlook Rules, where you can define actions that are carried out when a note is created or updated in an Evernote notebook. I have wanted this for almost as long as I have had Evernote and I finally got fed up of waiting for Evernote to implement it and wrote it myself.
As explained previously it works by receiving a webhook from Evernote every time that a note is created or updated and by reading the payload associated with that webhook you can see whether there is anything for you to do. You can read about the Evernote webhooks in the post that I wrote previously.
Rate Limited
Like most APIs Evernote implements rate limiting to prevent flooding of the system and to ensure fair access for all. I found that I was hitting the rate limit and was receiving messages such as the following in my logs:
Rate limit reached. Try again in 367 seconds.
Evernote has a page that describes the rate limits in some detail but neglects to say what the actual limits are so I reached out to Evernote support to find out what mine were set to. Each api key has it’s own limit and mine was set to a fairly measly 60 requests per hour. While I have requested a rate limit raise I have yet to hear back from Evernote support and, of course, they might not even grant it so I set about thinking of way that I could reduce the number of requests I made.
Code Optimisation
There were a couple of places I found where I could optimise my code. The first thing a call to the code does is load your list of notebooks – this could be from the current session or from the API depending on the situation. Because the code is both for managing the rules and actions and processing the webhooks there were some situations were code is being processed but is only required for one type of request. This was one of those situations so I prevented the loading of the notebooks if the request was from a webhook as they aren’t needed. One down.
The other place I could reduce the calls should have been obvious to me from the outset. When Evernote Rules receives the first webhook for a note it checks to see if it matches any of the rules that have been set. At this point there are no API accesses. Next it processes any actions associated such as adding tags or moving it to another notebook. The very process of carrying out an action such as adding a tag triggers another webhook for the same note as now it has been updated by Evernote Rules. Now we set off again to check the rules and process the actions and we get into something of a loop.
Here I could optimise the code to ignore subsequent requests until the first request has been completed. I did this simply by creating an empty file with the guid of the note that is being processed in the system temp directory. Subsequent webhooks check to see if there is already a file with the name of the note guid and if there is simply stop. Once the original request has been processed the file is deleted allowing subsequent request to be processed again.
There is a slight issue with the new real time editor (RTE) in that pretty much every time you type it updates the note and triggers a webhook incessantly and that makes Evernote Rules much more useful for notes that are created automatically, such as those that are emailed in.