Example #1
0
        public void Dispose()
        {
            if (this.isDisposed)
            {
                return;
            }

            lock (this.disposeLock)
            {
                if (this.isDisposed)
                {
                    return;
                }

                this.onDisposeCancelSource.SafeCancel();
                lock (this.sync)
                {
                    if (this.isDisposed)
                    {
                        return;
                    }

                    this.dbServices.SafeDispose();
                    this.dbServices = null;
                    this.onDisposeCancelSource.SafeDispose();
                    this.isDisposed = true;
                }
            }
        }
Example #2
0
        public static ReserveResult FromValueResult(bool isSuccess, AcquiredLockServices acquiredLockServices)
        {
            if (acquiredLockServices == null)
            {
                throw new ArgumentNullException("acquiredLockServices");
            }

            return(new ReserveResult
            {
                IsSuccess = true,
                ReservedServices = acquiredLockServices,
            });
        }
Example #3
0
        private Task SearchTaskBody(CancellationToken token)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token, this.onDisposeCancelSource.Token))
                {
                    //
                    var cancellationToken = cts.Token;

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            CancellationToken leaseLostToken;
                            lock (this.sync)
                            {
                                // резервируем бд
                                if (this.dbServices?.LockObject.CheckAndUpdateLease(cancellationToken) == true)
                                {
                                    // lease найдена и действует
                                }
                                else
                                {
                                    // lease НЕ найдена или НЕ действует
                                    this.dbServices?.Dispose();
                                    this.dbServices = null;
                                    // при this.dbServices != null - lease найдена и действует
                                    this.dbServices = this.dbClusterService.AcquireLockAndServicesForAnyNodeOrDefault(dbClusterParameters, cancellationToken);
                                }

                                if (this.dbServices != null)
                                {
                                    // lease найдена и действует
                                    leaseLostToken = this.dbServices.LockObject.GetLoseLeaseToken();
                                }
                            }

                            // lease найдена и действует
                            if (leaseLostToken != null)
                            {
                                // ждём потери или отмены
                                WaitHandle.WaitAny(new WaitHandle[] { leaseLostToken.WaitHandle, cancellationToken.WaitHandle });
                                // при потере может стоит производить дополнительные действия?
                                continue;
                            }

                            if (cancellationToken.IsCancellationRequested)
                            {
                                return;
                            }

                            // lease не зарезервировалась
                            Task.Delay(this.settings.SearchDbTimeout, cancellationToken).Wait(cancellationToken);
                        }
                        catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
                        {
                            // завершаем
                            return;
                        }
                        catch (Exception)
                        {
                            // ignore ??
                        }
                    }
                }
            }, TaskCreationOptions.LongRunning));
        }