init
This commit is contained in:
127
Common/DTS.Common.Import/DatabaseLocks/LockImportGroups.cs
Normal file
127
Common/DTS.Common.Import/DatabaseLocks/LockImportGroups.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DTS.Common.Classes.Locking;
|
||||
using DTS.Common.Import.Interfaces;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Storage;
|
||||
using DTS.Common.Utilities.Logging;
|
||||
using DTS.Slice.Users;
|
||||
|
||||
//FB 36740
|
||||
namespace DTS.Common.Import.DatabaseLocks
|
||||
{
|
||||
public class LockImportGroups : ILockImport
|
||||
{
|
||||
private readonly User _currentUser;
|
||||
private readonly double _strandedLockTimeoutMinutes;
|
||||
|
||||
/// <summary>
|
||||
/// this is a list of all groups locked by the user
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _lockedGroups = new List<LockRecord>();
|
||||
/// <summary>
|
||||
/// this is a list of all groups the user attempted to lock but another user has the lock
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _contendedGroups = new List<LockRecord>();
|
||||
public bool Contended { get => _contendedGroups.Any(); }
|
||||
|
||||
public LockImportGroups(User currentUser, double strandedLockTimeoutMinutes)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_strandedLockTimeoutMinutes = strandedLockTimeoutMinutes;
|
||||
}
|
||||
|
||||
public void FreeLock(ref ImportObject importObject)
|
||||
{
|
||||
if (!_lockedGroups.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var record in _lockedGroups)
|
||||
{
|
||||
LockManager.FreeLock(record.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, record.ItemCategory);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLock(ref ImportObject importObject, ref StringBuilder message)
|
||||
{
|
||||
_lockedGroups.Clear();
|
||||
_contendedGroups.Clear();
|
||||
foreach (var group in importObject.StaticGroups())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!LockManager.LockItem(group.Name, group.Id, LockManager.ItemCategories.Group, _currentUser.UserName,
|
||||
_currentUser.Id, out var existingLock, out var lockError))
|
||||
{
|
||||
if (lockError.ErrorCode == LockError.ITEM_NOT_FOUND)
|
||||
{
|
||||
continue; //lock not needed, doesn't exist in db
|
||||
}
|
||||
var lockOutOfDate = existingLock.LastUpdated.AddMinutes(_strandedLockTimeoutMinutes) < DateTime.Now;
|
||||
var IHaveTheLock = existingLock.LockingUserName == _currentUser.UserName && existingLock.LockingMachineName == Environment.MachineName;
|
||||
if (lockOutOfDate || IHaveTheLock)
|
||||
{
|
||||
//lock is expired (or already belongs to us), either way we can claim it
|
||||
LockManager.FreeLock(existingLock.ItemId, _currentUser.UserName, _currentUser.Id, out lockError, existingLock.ItemCategory);
|
||||
LockManager.LockItem(existingLock.ItemKey, existingLock.ItemId, LockManager.ItemCategories.Group, _currentUser.UserName,
|
||||
_currentUser.Id, out existingLock, out lockError);
|
||||
_lockedGroups.Add(existingLock);
|
||||
}
|
||||
else
|
||||
{
|
||||
//could not lock, we need Admin + confirmation to lock
|
||||
_contendedGroups.Add(existingLock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//successfully locked
|
||||
_lockedGroups.Add(existingLock);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
if (!_contendedGroups.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
message.AppendLine(StringResources.ImportTestSetup_GroupsLocked);
|
||||
foreach (var contended in _contendedGroups)
|
||||
{
|
||||
message.AppendLine($"{contended.ItemKey} by {contended.LockingUserName} on {contended.LockingMachineName}");
|
||||
}
|
||||
}
|
||||
|
||||
public bool StealLock(bool proceed)
|
||||
{
|
||||
if (!_contendedGroups.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_currentUser.IsAdmin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//steal locks if user oks it
|
||||
if (!proceed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var contendedGroup in _contendedGroups)
|
||||
{
|
||||
LockManager.FreeLock(contendedGroup.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, contendedGroup.ItemCategory);
|
||||
LockManager.LockItem(contendedGroup.ItemKey, contendedGroup.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName, _currentUser.Id, out var newLockRecord, out lockError);
|
||||
_lockedGroups.Add(newLockRecord);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Common/DTS.Common.Import/DatabaseLocks/LockImportSensors.cs
Normal file
130
Common/DTS.Common.Import/DatabaseLocks/LockImportSensors.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DTS.Common.Classes.Locking;
|
||||
using DTS.Common.Import.Interfaces;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Storage;
|
||||
using DTS.Common.Utilities.Logging;
|
||||
using DTS.SensorDB;
|
||||
using DTS.Slice.Users;
|
||||
|
||||
//FB 36740
|
||||
namespace DTS.Common.Import.DatabaseLocks
|
||||
{
|
||||
public class LockImportSensors : ILockImport
|
||||
{
|
||||
private readonly User _currentUser;
|
||||
private readonly double _strandedLockTimeoutMinutes;
|
||||
|
||||
/// <summary>
|
||||
/// list of all sensors the user has locked
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _lockedSensors = new List<LockRecord>();
|
||||
/// <summary>
|
||||
/// list of all sensors the user attempted to lock but another user has the lock
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _contendedSensors = new List<LockRecord>();
|
||||
public bool Contended { get => _contendedSensors.Any(); }
|
||||
|
||||
public LockImportSensors(User currentUser, double strandedLockTimeoutMinutes)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_strandedLockTimeoutMinutes = strandedLockTimeoutMinutes;
|
||||
}
|
||||
|
||||
public void FreeLock(ref ImportObject importObject)
|
||||
{
|
||||
if (!_lockedSensors.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var record in _lockedSensors)
|
||||
{
|
||||
LockManager.FreeLock(record.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, record.ItemCategory);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLock(ref ImportObject importObject, ref StringBuilder message)
|
||||
{
|
||||
var sensorLookup = SensorsCollection.SensorsList.GetAllSensors(true).ToDictionary(s => s.SerialNumber);
|
||||
|
||||
_lockedSensors.Clear();
|
||||
_contendedSensors.Clear();
|
||||
|
||||
foreach (var sensorSerialNumber in importObject.Sensors().Select(s => s.SerialNumber))
|
||||
{
|
||||
if (!sensorLookup.ContainsKey(sensorSerialNumber)) { continue; } //no lock needed
|
||||
try
|
||||
{
|
||||
var wasAbleToLock = LockManager.LockItem(sensorSerialNumber, sensorLookup[sensorSerialNumber].DatabaseId, LockManager.ItemCategories.Sensor, _currentUser.UserName,
|
||||
_currentUser.Id, out var existingLock, out var lockError);
|
||||
if (!wasAbleToLock)
|
||||
{
|
||||
var lockOutOfDate = existingLock.LastUpdated.AddMinutes(_strandedLockTimeoutMinutes) < DateTime.Now;
|
||||
var IHaveTheLock = existingLock.LockingUserName == _currentUser.UserName && existingLock.LockingMachineName == Environment.MachineName;
|
||||
|
||||
if (lockOutOfDate || IHaveTheLock)
|
||||
{
|
||||
//lock is expired (or already belongs to us), either way we can claim it
|
||||
LockManager.FreeLock(existingLock.ItemId, _currentUser.UserName, _currentUser.Id, out lockError, existingLock.ItemCategory);
|
||||
LockManager.LockItem(existingLock.ItemKey, existingLock.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName,
|
||||
_currentUser.Id, out existingLock, out lockError);
|
||||
_lockedSensors.Add(existingLock);
|
||||
}
|
||||
else
|
||||
{
|
||||
//lock is not expired, we need admin + confirmation to steal the lock
|
||||
_contendedSensors.Add(existingLock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_lockedSensors.Add(existingLock); //successfully locked
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
if (!_contendedSensors.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
message.AppendLine(StringResources.ImportTestSetup_TestsLocked);
|
||||
foreach (var contended in _contendedSensors)
|
||||
{
|
||||
message.AppendLine($"{contended.ItemKey} by {contended.LockingUserName} on {contended.LockingMachineName}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool StealLock(bool proceed)
|
||||
{
|
||||
if (!_contendedSensors.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_currentUser.IsAdmin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//steal locks if user oks it
|
||||
if (!proceed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var contendedSensor in _contendedSensors)
|
||||
{
|
||||
LockManager.FreeLock(contendedSensor.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, contendedSensor.ItemCategory);
|
||||
LockManager.LockItem(contendedSensor.ItemKey, contendedSensor.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName, _currentUser.Id, out var newLockRecord, out lockError);
|
||||
_lockedSensors.Add(newLockRecord);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
132
Common/DTS.Common.Import/DatabaseLocks/LockImportTestSetups.cs
Normal file
132
Common/DTS.Common.Import/DatabaseLocks/LockImportTestSetups.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DTS.Common.Classes.Locking;
|
||||
using DTS.Common.Import.Interfaces;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Storage;
|
||||
using DTS.Common.Utilities.Logging;
|
||||
using DTS.Slice.Users;
|
||||
|
||||
namespace DTS.Common.Import.DatabaseLocks
|
||||
{
|
||||
//FB 36740
|
||||
public class LockImportTestSetups : ILockImport
|
||||
{
|
||||
private readonly User _currentUser;
|
||||
private readonly double _strandedLockTimeoutMinutes;
|
||||
|
||||
/// <summary>
|
||||
/// this is a list of all tests locked by user
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _lockedTests = new List<LockRecord>();
|
||||
/// <summary>
|
||||
/// this is a list of tests locking was attempted but another user has the lock
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _contendedTests = new List<LockRecord>();
|
||||
public LockImportTestSetups(User currentUser, double strandedLockTimeoutMinutes)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_strandedLockTimeoutMinutes = strandedLockTimeoutMinutes;
|
||||
}
|
||||
|
||||
public bool Contended { get => _contendedTests.Any(); }
|
||||
|
||||
public void FreeLock(ref ImportObject importObject)
|
||||
{
|
||||
if (!_lockedTests.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var record in _lockedTests)
|
||||
{
|
||||
LockManager.FreeLock(record.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, record.ItemCategory);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLock(ref ImportObject importObject, ref StringBuilder message)
|
||||
{
|
||||
_lockedTests.Clear();
|
||||
_contendedTests.Clear();
|
||||
foreach (var test in importObject.TestSetups())
|
||||
{
|
||||
try
|
||||
{
|
||||
var gotLock = LockManager.LockItem(test.Name, test.Id, LockManager.ItemCategories.TestSetup, _currentUser.UserName,
|
||||
_currentUser.Id, out var existingLock, out var lockError);
|
||||
if (!gotLock)
|
||||
{
|
||||
if (lockError.ErrorCode == LockError.ITEM_NOT_FOUND)
|
||||
{
|
||||
continue; //lock not needed, doesn't exist in db
|
||||
}
|
||||
var lockOutOfDate = existingLock.LastUpdated.AddMinutes(_strandedLockTimeoutMinutes) < DateTime.Now;
|
||||
var IHaveTheLock = existingLock.LockingUserName == _currentUser.UserName && existingLock.LockingMachineName == Environment.MachineName;
|
||||
if (lockOutOfDate || IHaveTheLock)
|
||||
{
|
||||
//lock is expired (or already belongs to us), either way we can claim it
|
||||
_ = LockManager.FreeLock(existingLock.ItemId, _currentUser.UserName, _currentUser.Id, out lockError, existingLock.ItemCategory);
|
||||
_ = LockManager.LockItem(existingLock.ItemKey, existingLock.ItemId, LockManager.ItemCategories.TestSetup, _currentUser.UserName,
|
||||
_currentUser.Id, out existingLock, out lockError);
|
||||
_lockedTests.Add(existingLock);
|
||||
}
|
||||
else
|
||||
{
|
||||
//couldn't be locked, we need Admin + confirmation to steal lock
|
||||
_contendedTests.Add(existingLock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//successfully locked
|
||||
_lockedTests.Add(existingLock);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
|
||||
if (!_contendedTests.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
message.AppendLine(StringResources.ImportTestSetup_TestsLocked);
|
||||
foreach (var contended in _contendedTests)
|
||||
{
|
||||
message.AppendLine($"{contended.ItemKey} by {contended.LockingUserName} on {contended.LockingMachineName}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool StealLock(bool proceed)
|
||||
{
|
||||
|
||||
if (!_contendedTests.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_currentUser.IsAdmin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//steal locks if user oks it
|
||||
|
||||
if (!proceed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var contendedTest in _contendedTests)
|
||||
{
|
||||
LockManager.FreeLock(contendedTest.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, contendedTest.ItemCategory);
|
||||
LockManager.LockItem(contendedTest.ItemKey, contendedTest.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName, _currentUser.Id, out var newLockRecord, out lockError);
|
||||
_lockedTests.Add(newLockRecord);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user