/// <summary> /// Creates MongoDB distributed lock /// </summary> /// <param name="resource">Lock resource</param> /// <param name="timeout">Lock timeout</param> /// <param name="database">Lock database</param> /// <param name="storageOptions">Database options</param> /// <exception cref="DistributedLockTimeoutException">Thrown if lock is not acuired within the timeout</exception> /// <exception cref="MongoDistributedLockException">Thrown if other mongo specific issue prevented the lock to be acquired</exception> public MongoDistributedLock(string resource, TimeSpan timeout, HangfireDbContext database, MongoStorageOptions storageOptions) { _resource = resource ?? throw new ArgumentNullException(nameof(resource)); _database = database ?? throw new ArgumentNullException(nameof(database)); _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions)); if (string.IsNullOrEmpty(resource)) { throw new ArgumentException($@"The {nameof(resource)} cannot be empty", nameof(resource)); } if (timeout.TotalSeconds > int.MaxValue) { throw new ArgumentException($"The timeout specified is too large. Please supply a timeout equal to or less than {int.MaxValue} seconds", nameof(timeout)); } _signal = new MongoSignal(_database.Signal); if (!AcquiredLocks.Value.ContainsKey(_resource) || AcquiredLocks.Value[_resource] == 0) { Cleanup(); Acquire(timeout); AcquiredLocks.Value[_resource] = 1; StartHeartBeat(); } else { AcquiredLocks.Value[_resource]++; } }
public MongoJobQueue(HangfireDbContext database, MongoStorageOptions storageOptions) { _database = database ?? throw new ArgumentNullException(nameof(database)); _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions)); _signal = new MongoSignal(database.Signal); }