/// <summary> /// Initializes a new instance of the <see cref="CosmosDbDistributedLock"/> class. /// Note that the lock will not be acquired until <see cref="AcquireLock"/> is called. /// </summary> /// <param name="containerFactory">The Cosmos Container factory</param> /// <param name="lockId">The id of the lock. The document created in the database will use this ID will be prefixed with <see cref="IdPrefix"/>.</param> /// <param name="logger">A logger instance</param> /// <param name="lockDocumentTimeToLive">The time to live for the lock document. If the process crashes, the lock will be released after this amount of time</param> public CosmosDbDistributedLock(Func <IScoped <Container> > containerFactory, string lockId, ILogger <CosmosDbDistributedLock> logger, TimeSpan lockDocumentTimeToLive) { EnsureArg.IsNotNull(containerFactory, nameof(containerFactory)); EnsureArg.IsNotNullOrWhiteSpace(lockId, nameof(lockId)); EnsureArg.IsNotNull(logger, nameof(logger)); _lockId = lockId; _logger = logger; _lockDocumentTimeToLive = lockDocumentTimeToLive; _lockDocument = new LockDocument { Id = IdPrefix + lockId, TimeToLiveInSeconds = (int)lockDocumentTimeToLive.TotalSeconds }; _containerFactory = containerFactory; }
/// <summary> /// Initializes a new instance of the <see cref="CosmosDbDistributedLock"/> class. /// Note that the lock will not be acquired until <see cref="AcquireLock"/> is called. /// </summary> /// <param name="documentClientFactory">The document client factory</param> /// <param name="collectionUri">The URI of the collection to use</param> /// <param name="lockId">The id of the lock. The document created in the database will use this ID will be prefixed with <see cref="IdPrefix"/>.</param> /// <param name="logger">A logger instance</param> /// <param name="lockDocumentTimeToLive">The time to live for the lock document. If the process crashes, the lock will be released after this amount of time</param> public CosmosDbDistributedLock(Func <IScoped <IDocumentClient> > documentClientFactory, Uri collectionUri, string lockId, ILogger <CosmosDbDistributedLock> logger, TimeSpan lockDocumentTimeToLive) { EnsureArg.IsNotNull(collectionUri, nameof(collectionUri)); EnsureArg.IsNotNull(documentClientFactory, nameof(documentClientFactory)); EnsureArg.IsNotNullOrWhiteSpace(lockId, nameof(lockId)); EnsureArg.IsNotNull(logger, nameof(logger)); _collectionUri = collectionUri; _lockId = lockId; _logger = logger; _lockDocumentTimeToLive = lockDocumentTimeToLive; _lockDocument = new LockDocument { Id = IdPrefix + lockId, TimeToLiveInSeconds = (int)lockDocumentTimeToLive.TotalSeconds }; _documentClientFactory = documentClientFactory; }
public override async Task <bool> AcquireInitializationLockAsync(TimeSpan lockTime) { string lockId = this.GetStoreLockName(); LockDocument containerDocument = new LockDocument() { Id = lockId, TimeToLive = (int)lockTime.TotalSeconds }; ItemResponse <LockDocument> document = await this.container.TryCreateItemAsync <LockDocument>( this.requestOptionsFactory.GetPartitionKey(lockId), containerDocument).ConfigureAwait(false); if (document != null) { this.lockETag = document.ETag; return(true); } return(false); }