Skip to content

Commit

Permalink
Removing fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Jan 29, 2024
1 parent f7697fe commit ba3e490
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions Lombiq.NodeJs.Extensions/CustomExecTasks/ExclusiveMutex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ namespace Lombiq.NodeJs.Extensions.CustomExecTasks;

public class ExclusiveMutex(string mutexName, TimeSpan timeout)
{
private readonly string _mutexName = mutexName;
private readonly TimeSpan _timeout = timeout;

public int RetryIntervalMs { get; set; } = 100;
public int WaitTimeMs { get; set; } = 1000;

Expand All @@ -17,9 +14,9 @@ public bool Execute(
{
var count = 1;
var stopwatch = Stopwatch.StartNew();
while (stopwatch.Elapsed <= _timeout)
while (stopwatch.Elapsed <= timeout)
{
using (var mutex = new Mutex(initiallyOwned: false, _mutexName, out var createdNew))
using (var mutex = new Mutex(initiallyOwned: false, mutexName, out var createdNew))
{
// We only try to acquire the mutex in case it was freshly created, because that means that no other
// processes are currently using it, including in a shared way.
Expand All @@ -28,7 +25,7 @@ public bool Execute(
try
{
logWait?.Invoke(
"Acquired exclusive access to {0} after {1}.", [_mutexName, stopwatch.Elapsed]);
"Acquired exclusive access to {0} after {1}.", [mutexName, stopwatch.Elapsed]);
return functionToExecute();
}
finally
Expand All @@ -38,11 +35,11 @@ public bool Execute(
}
}

logWait?.Invoke("#{0} Waiting for exclusive access to {1}.", [count++, _mutexName]);
logWait?.Invoke("#{0} Waiting for exclusive access to {1}.", [count++, mutexName]);
Thread.Sleep(RetryIntervalMs);
}

logError?.Invoke("Failed to acquire exclusive access {0} in {1}.", [_mutexName, _timeout]);
logError?.Invoke("Failed to acquire exclusive access {0} in {1}.", [mutexName, timeout]);
return false;
}
}

0 comments on commit ba3e490

Please sign in to comment.