示例#1
0
        private void WithLock(DeletionArgs args, string subKey, Func <long> func)
        {
            while (!args.CancellationToken.IsCancellationRequested)
            {
                try
                {
                    using (FluentNHibernateDistributedLock.Acquire(Storage,
                                                                   string.Format("{0}:{1}", DistributedLockKey, subKey),
                                                                   DefaultLockTimeout,
                                                                   args.CancellationToken))
                    {
                        var removedCount = func();
                        if (removedCount < args.NumberOfRecordsInSinglePass)
                        {
                            break;
                        }
                    }
                }
                catch (DistributedLockTimeoutException)
                {
                    Logger.Debug("Distributed lock acquisition timeout was exceeded");
                }

                args.CancellationToken.Wait(DelayBetweenPasses);
            }
        }
        public static FluentNHibernateDistributedLock Acquire(FluentNHibernateJobStorage storage, string resource,
                                                              TimeSpan timeout,
                                                              CancellationToken?cancellationToken = null)
        {
            var tmp = new FluentNHibernateDistributedLock(storage, resource, timeout, cancellationToken);

            tmp.Initialize();
            return(tmp);
        }
示例#3
0
        public void WithLock(CancellationToken cancellationToken, string subKey, Func <long> func)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var fluentNHibernateDistributedLock = new FluentNHibernateDistributedLock(Storage,
                                                                                          string.Format("{0}:{1}", DistributedLockKey, subKey),
                                                                                          DefaultLockTimeout,
                                                                                          cancellationToken).Acquire();
                if (fluentNHibernateDistributedLock != null)
                {
                    using (fluentNHibernateDistributedLock)
                    {
                        var removedCount = func();
                        if (removedCount <= 0)
                        {
                            break;
                        }
                    }
                }

                cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
            }
        }
 public override IDisposable AcquireDistributedLock(string resource, TimeSpan timeout)
 {
     return(FluentNHibernateDistributedLock.Acquire(Storage, resource, timeout));
 }
        public void Execute(CancellationToken cancellationToken)
        {
            Logger.DebugFormat("Aggregating records in '{0}' table", _tableName);

            long removedCount = int.MaxValue;

            while (!cancellationToken.IsCancellationRequested)
            {
                var fluentNHibernateDistributedLock = new FluentNHibernateDistributedLock(_storage,
                                                                                          nameof(CountersAggregator), new TimeSpan(0, 0, 5), cancellationToken).Acquire();
                if (fluentNHibernateDistributedLock == null)
                {
                    cancellationToken.WaitHandle.WaitOne(new TimeSpan(0, 0, 5));
                }
                else
                {
                    using (fluentNHibernateDistributedLock)
                    {
                        _storage.UseStatelessSession(session =>
                        {
                            using (var transaction = session.BeginTransaction())
                            {
                                var counters = session.Query <_Counter>().Take(NumberOfRecordsInSinglePass).ToList();
                                if (!counters.Any())
                                {
                                    return;
                                }
                                var countersByName = counters.GroupBy(counter => counter.Key)
                                                     .Select(i =>
                                                             new
                                {
                                    i.Key,
                                    value    = i.Sum(counter => counter.Value),
                                    expireAt = i.Max(counter => counter.ExpireAt)
                                })
                                                     .ToList();

                                foreach (var item in countersByName)
                                {
                                    session.UpsertEntity <_AggregatedCounter>(i => i.Key == item.Key,
                                                                              n =>
                                    {
                                        n.ExpireAt = item.expireAt;
                                        n.Value   += item.value;
                                    }, n => { n.Key = item.Key; });
                                }

                                removedCount =
                                    session.DeleteByInt32Id <_Counter>(counters.Select(counter => counter.Id)
                                                                       .ToArray());
                                if (removedCount == 0)
                                {
                                    return;
                                }
                                transaction.Commit();
                            }
                        });
                    }
                }
            }

            Logger.DebugFormat("Done aggregating records in '{1}' table.  Waiting {0}",
                               _storage.Options.CountersAggregateInterval, _tableName);

            cancellationToken.WaitHandle.WaitOne(_storage.Options.CountersAggregateInterval);
        }