Want to confirm that I'm properly queuing macro launch requests

I have a macro that I call with a Mail rule via an AppleScript; the AppleScript is very simple, it basically passes the message content as a parameter in a Keyboard Maestro macro call, then files the message.

The macro does a license lookup operation for our customers. Up until now, it's been working great. However, we're about to release an update to an app that will (more than likely) result in the mailbox getting multiple simultaneous (or near simultaneous) requests for license lookups.

The problem is that our cart provider has no API for getting our license files, so the server basically replicates what I'd do manually: Loads the web page, waits for a certain box to appear, types in that box, waits for the next page to appear, etc. The whole process takes 10-15 seconds.

But I'm concerned about what happens when two (or three or 10) emails arrive at nearly the same time. Mail will process all the rules in about a second, so then it's up to Keyboard Maestro and the macro to make sure things are queued properly: I can't have multiple copies of the macro running at once, because they're doing GUI stuff in windows onscreen.

So I have a semaphore lock set at the start of the macro, set to a timeout of two minutes—I still want the other requests to run, but they definitely have to run after the prior has completely finished.

My question is: Is that the proper approach to make sure that, regardless of the number of simultaneous (or near simultaneous) emails the server receives, the macro will run each one in sequence, and not start until the prior execution has finished?

-rob.

1 Like

The semaphore will form a FIFO queue. Unless you've done something tricky your Mail Rule will process one email at a time, as they arrive, so your macro will almost always process the emails in arrival order -- Mail reception and Rules processing is async so there's no guarantee of the order the script submits emails to KM, but I assume that the order of email processing doesn't actually matter as long as they all get processed and only one at a time.

You might want to up your Semaphore timeout, or even leave it at the default 99 hours. I'd rely on the currently-processing macro timing out (or erroring) one of its Actions, noting the failure in a log file, then ending execution -- that'll clear the semaphore lock and allow the next in the queue to run.

With a 2 minute timeout on the Semaphore Action you only need 10 emails to arrive within a few seconds for the last couple to be dropped before being processed.

Depending on the macro you may also want to up the "maximum concurrent macros" from the default of 50 -- it isn't just "whole" macros and subs that count towards that total, but "Group" Actions, "If... Then... Else" Actions, and so on.

To save you looking it up :wink:

defaults write com.stairways.keyboardmaestro.engine MaximumSimultaneousMacros -int 100

...or whatever number you think you'll need.

2 Likes

Thanks; great advice as always. I've set the timeout back to the default, and bumped max macros to 200, as there are multiple macros, and many with groups and if/thens.

-rob.

2 Likes

@griffman , nice upgrade! Just saying!

1 Like