withLockAsync method
creates a lock file and then calls fn
once fn
returns the lock is released.
If waiting
is passed it will be used to write
a log message to the console.
Throws a DCliException if the NamedLock times out.
Implementation
Future<void> withLockAsync(
Future<void> Function() fn, {
String? waiting,
}) async {
var lockHeld = false;
try {
verbose(() => 'lockcount = $_lockCountForName');
_createLockPath();
if (_lockCountForName > 0 || (await _takeLock(waiting))) {
lockHeld = true;
incLockCount;
await fn();
}
} finally {
if (lockHeld) {
await _releaseLock();
}
// just in case an async exception can be thrown
// I'm uncertain if this is a reality.
lockHeld = false;
}
}