Flow Designer Waits: When a Second isn't a Second
Flow Designer, if you havenāt heard from me, is a great tool in the ServiceNow platform. It makes process automation, prototyping and no-code development so fast and easy. Sure, it has itās flaws, but who doesnāt?
What if I told you, when you asked Flow Designer to do something SO SIMPLE as āwaitā for 1 second, it (almost) literally LAUGHED AT YOU and did WHATEVER THE HECK IT FELT LIKE?
The Reason
I have a flow which calls an API to send a push notification to our 2FA (Okta). It works great, you get the push, happy days. BUTā¦
We then need to check another endpoint to see what the userās response was, and because itās a real person, not some kind of BUTTON PUSHING ROBOT - that can take a while. In the worst-case of a user IGNORING the push, it expires after a fairly long period of time (5 minutes!)
I wrote a 1 second wait into a loop, so at worst, we send 300 polls to the endpoint. It was a quick-and-dirty option, alright?
BUTā¦ and hereās where the issue appears. Those waits are taking forever. Enter my tests:
The Setup
Iām running the tests in my Personal Developer Instance, on Orlando Patch 1. The flows have been made in Global Scope, and called via API from Background Scripts.
Hereās my basic test flow:
I was planning on doing a lot more testing (scope vs global, during loops or not, etc), but I donāt think there was much merit in it.
I wanted to compare waits during:
startFlow
vsstartFlowQuick
and
executeFlow
vsexecuteFlowQuick
These all should be Asynchronous and in theory, write two work-notes to a job a second apart. Letās see what happenedā¦
The Results
Control - Test Without a Wait
The flow ran, and it looks to take in the 10ās of milliseconds. Thatās visible in the second update where itās flicked from one second to the next. Thatās OK with me!
Flow, Executed by āTestā w/ 1sec Wait:
- 1: 6 seconds
- 2: 8 seconds
- 3: 8 seconds
- 4: 9 seconds
- 5: 4 seconds
- avg = 7 seconds š¦
Flow, async script (startFlow
):
- 1: 7 seconds
- 2: 9 seconds
- 3: 10 seconds
- 4: 9 seconds
- 5: 8 seconds
- avg = 8.6 seconds š¦š¦
Flow, async script (startFlowQuick
):
Hereās something interesting - doing a Quick never reaches past the āWaitā - even though itās called ASync. Also, it runs as the system user? Who uses this āquickā method and WHY!?
Flow, sync script (executeFlow
):
- 1: 5 seconds
- 2: 2 seconds
- 3: 9 seconds
- 4: 7 seconds
- 5: 5 seconds
- avg = 5.6 seconds š¦š¦š¦
Flow, sync script (executeFlowQuick
):
Another interesting note - Running a Synchronous script using the executeFlowQuick
actually threw an error in the Background Script editor:
Flow Designer: Persisting an unterminated plan is not supported for plan with id null and name: Global Flow Wait Test: no thrown error
This also runs as yourself, vs the System user the ASync one runs as, but still doesnāt work.
Flow, 10 second wait
For funsies I even changed the wait to 10 seconds to see if it would get a little more tight.
- 1: 18 seconds
- 2: 15 seconds
- 3: 13 seconds
- 4: 21 seconds
- 5: 12 seconds
- avg = 15.8 seconds š¦
Outcomes
Well, the outcomes are NOT GOOD. I am disapointed this is the case, and will have to re-engineer or just accept that we are going to use a lot more (without a wait) / less (with a wait thatāll take too long) API calls than we thought.
The Fix(?)
Hereās the magic, right?
Look at this:
and this:
and this:
and THIS!:
BUT! DUN DUN DUN
We canāt have nice things.
The magic I was using to get this exact second is the UNMENTIONABLE HORROR that is gs.sleep(1000)
And every time that runs, it locks a whole thread in your instance. And thatās just not something we can do in Production. No matter how badly we want it, it just feels icky.
The TL;DR
- Flow Designer āWaitsā are all crazy asynchronous things that take far too long. Donāt wait for 1 second. Itās more likely 10.
- The āQuickā options in the API donāt really work with waits
- Sync or ASync doesnāt matter at the flow level
- 1 second or 10 seconds doesnāt matter either
- hard-sleeping a thread works, BUT AT WHAT COST!?
- Just deal with it I guess?
Sorry I couldnāt be more help. Itās interesting data though, right?
ā¤ļø
Andrew