public void Execute(CancellationToken cancellationToken) { Logger.DebugFormat("Aggregating records in 'Counter' table..."); int removedCount = 0; lock (_lockCounter) { do { using (_storage.Options.AcquireLock(_storage, "LockCounter", TimeSpan.FromMinutes(5), new CancellationToken())) { _storage.UseConnection(connection => { removedCount = connection.Execute( GetAggregationQuery(), new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass }); }); } if (removedCount >= NumberOfRecordsInSinglePass) { cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses); cancellationToken.ThrowIfCancellationRequested(); } } while (removedCount >= NumberOfRecordsInSinglePass); } cancellationToken.WaitHandle.WaitOne(_interval); }
public void Execute(CancellationToken cancellationToken) { foreach (var table in ProcessedTables) { Logger.DebugFormat("Removing outdated records from table '{0}'...", table); int removedCount = 0; do { _storage.UseConnection(connection => { try { Logger.DebugFormat("delete from `{0}` where ExpireAt < @now limit @count;", table); using ( new MySqlDistributedLock( connection, DistributedLockKey, DefaultLockTimeout, cancellationToken).Acquire()) { removedCount = connection.Execute( String.Format( "select null from `{0}` where ExpireAt < @now; " + "delete from `{0}` where ExpireAt < @now limit @count;", table), new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass }); } Logger.DebugFormat("removed records count={0}", removedCount); } catch (MySqlException ex) { Logger.Error(ex.ToString()); } }); if (removedCount > 0) { Logger.Trace(String.Format("Removed {0} outdated record(s) from '{1}' table.", removedCount, table)); cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses); cancellationToken.ThrowIfCancellationRequested(); } } while (removedCount != 0); } cancellationToken.WaitHandle.WaitOne(_checkInterval); }
private int AcquireLock(string resource, TimeSpan timeout) { return(MySqlStorageConnection.AttemptActionReturnObject(() => _storage.UseConnection(connection => connection .Execute( "INSERT INTO DistributedLock (Resource, CreatedAt) " + " SELECT @resource, @now " + " FROM dual " + " WHERE NOT EXISTS ( " + " SELECT * FROM DistributedLock "+ " WHERE Resource = @resource "+ " AND CreatedAt > @expired)", new { resource, now = DateTime.UtcNow, expired = DateTime.UtcNow.Add(timeout.Negate()) })) )); }
public void Execute(CancellationToken cancellationToken) { Logger.DebugFormat($"Aggregating records in '{_storageOptions.TablesPrefix}Counter' table..."); int removedCount = 0; do { _storage.UseConnection(connection => { removedCount = connection.Execute( GetAggregationQuery(), new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass }); }); if (removedCount >= NumberOfRecordsInSinglePass) { cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses); cancellationToken.ThrowIfCancellationRequested(); } } while (removedCount >= NumberOfRecordsInSinglePass); cancellationToken.WaitHandle.WaitOne(_storageOptions.CountersAggregateInterval); }
public override string CreateExpiredJob(Job job, IDictionary <string, string> parameters, DateTime createdAt, TimeSpan expireIn) { if (job == null) { throw new ArgumentNullException("job"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } var invocationData = InvocationData.Serialize(job); Logger.TraceFormat("CreateExpiredJob={0}", JobHelper.ToJson(invocationData)); return(_storage.UseConnection(connection => { var jobId = connection.Query <int>( "insert into HangfireJob (InvocationData, Arguments, CreatedAt, ExpireAt) " + "values (@invocationData, @arguments, @createdAt, @expireAt); " + "select last_insert_id();", new { invocationData = JobHelper.ToJson(invocationData), arguments = invocationData.Arguments, createdAt = createdAt, expireAt = createdAt.Add(expireIn) }).Single().ToString(); if (parameters.Count > 0) { var parameterArray = new object[parameters.Count]; int parameterIndex = 0; foreach (var parameter in parameters) { parameterArray[parameterIndex++] = new { jobId = jobId, name = parameter.Key, value = parameter.Value }; } connection.Execute( "insert into HangfireJobParameter (JobId, Name, Value) values (@jobId, @name, @value)", parameterArray); } return jobId; })); }