Monday 25 May 2015

Avoiding Script Timeouts - Part 3

Avoiding Script Timeouts Part 3


If you've done absolutely all you can to speed up your scripts but they still time out now and again it may not matter.  Depending upon what your script does, if it completes 90% or so of the required actions on a regular basis that may be enough but there's a couple of things you can do to make the best of this situation even if you can't solve the speed issues.

Use a sort order

If you're working to change Keyword positions or optimise for performance it's a good idea to use a sort order to ensure the most important elements are processed first in the script.  In this way if the script does time out you should have hit the bulk of these most important elements already.  For example, if you're optimising for performance you'll likely want to sort by Cost descending so that the elements with the biggest spend get optimised first.  You can do this simply in your selector using the OrderBy method:

var campSelector = AdWordsApp.campaigns()
  .OrderBy("Cost DESC")
  .get();

You can order by any of the same columns you can use in the Condition methods so for example, CTR (note, of course, that in scripts this is written as "Ctr"), AveragePosition, Impressions and so on.

Once you're ordering by some metric, if you want to cover your whole set of elements even if the script times out, you can consider changing this sort order every now and again.  So, for example, you could change the sort order to ascending just for Monday and Thursday, as follows:

var d = new Date();
var theDay = d.getDay();
// day "0" is Sunday
var sortOrder = (theDay == 1 || theDay == 4)? "ASC" : "DESC";
var campSelector = AdWordsApp.campaigns()
  .OrderBy("Cost " + sortOrder)
  .get();

In this way, twice a week your script will run in the "reverse" direction so you should pick up any elements that are frequently missed when it times out before reaching them.  You can, of course, change how often you do this just by editing the day choices.

Check the time

Time outs can be most annoying when the last thing the script does is write results to a spreadsheet, send an email or perform some other final action.  If the script times out this final action won't happen so you could end up with incomplete data or lacking notifications.  Fortunately, there's a solution to this too.

The AdWordsApp object has a useful method called "getExecutionInfo()" that has a method itself called "getRemainingTime()".  Using these methods you can check how long there is to run (in seconds) before the script times out so if you check this figure frequently you can anticipate a time out and force the final action to happen early.  For example:

if(AdWordsApp.getExecutionInfo().getRemainingTime < 45) {
  // code to exit looping and jump to final action
}

So if there's less than 45 seconds remaining, you can jump out of any loops and force the final action.  How you do this will depend upon the coding but it's usually fairly simple.

Whether a log of an incomplete run is useful is of course dependent upon what you're doing, but even a partial report is often more useful than nothing at all (and you could adjust the reporting to show that it's partial).

I hope you find these tips useful!

No comments: