public override void Commit() { _storage.UseStatelessSessionInTransaction(session => { foreach (var command in _commandQueue) { command(session); //does nothing } }); }
public void Execute(CancellationToken cancellationToken) { Logger.DebugFormat("Aggregating records in '{0}' table", _tableName); long removedCount = 0; do { _storage.UseStatelessSessionInTransaction(session => { var counters = session.Query <_Counter>().Take(NumberOfRecordsInSinglePass).ToList(); if (counters.Any()) { 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 = MaxOfDateTimes(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 >= NumberOfRecordsInSinglePass) { cancellationToken.Wait(DelayBetweenPasses); cancellationToken.ThrowIfCancellationRequested(); } } while (removedCount >= NumberOfRecordsInSinglePass); Logger.TraceFormat($"Records from the '{_tableName}' aggregated."); cancellationToken.Wait(_storage.Options.CountersAggregateInterval); }
internal void Initialize() { var started = Stopwatch.StartNew(); do { if (SqlUtil.WrapForTransaction(() => SqlUtil.WrapForDeadlock(_cancellationToken, () => { var done = TryLock(() => { var result = false; _storage.UseStatelessSessionInTransaction(session => { var distributedLock = session.Query <_DistributedLock>() .FirstOrDefault(i => i.Resource == _resource); var utcNow = session.Storage.UtcNow; var expireAtAsLong = utcNow.Add(_timeout).ToEpochDate(); if (distributedLock == null) { distributedLock = new _DistributedLock { CreatedAt = utcNow, Resource = _resource, ExpireAtAsLong = expireAtAsLong }; session.Insert(distributedLock); _lockId = distributedLock.Id; result = true; if (Logger.IsDebugEnabled()) { Logger.Debug($"Inserted row for distributed lock '{_resource}'"); } } else if (distributedLock.ExpireAtAsLong < utcNow.ToEpochDate()) { distributedLock.CreatedAt = utcNow; distributedLock.ExpireAtAsLong = expireAtAsLong; session.Update(distributedLock); if (Logger.IsDebugEnabled()) { Logger.Debug($"Re-used row for distributed lock '{_resource}'"); } _lockId = distributedLock.Id; result = true; } }); return(result); }, () => false); return(done); }, _options))) { if (Logger.IsDebugEnabled()) { Logger.DebugFormat("Granted distributed lock '{0}'", _resource); } return; } if (started.Elapsed > _timeout) { break; } if (Logger.IsDebugEnabled()) { Logger.Debug( $"Will poll for distributed lock '{_resource}' in {_options.DistributedLockPollInterval}."); } _cancellationToken.Wait(_options.DistributedLockPollInterval); } while (true); //dont change this. Hangfire looks for resource name in exception properties throw new DistributedLockTimeoutException(_resource); }