public void Expire_ExpiresSetAndHash() { var expiration = new ConsoleExpirationTransaction(_transaction.Object); expiration.Expire(_consoleId, TimeSpan.FromHours(1)); _transaction.Verify(x => x.ExpireSet(_consoleId.GetSetKey(), It.IsAny <TimeSpan>())); _transaction.Verify(x => x.ExpireHash(_consoleId.GetHashKey(), It.IsAny <TimeSpan>())); // backward compatibility: _transaction.Verify(x => x.ExpireSet(_consoleId.GetOldConsoleKey(), It.IsAny <TimeSpan>())); _transaction.Verify(x => x.ExpireHash(_consoleId.GetOldConsoleKey(), It.IsAny <TimeSpan>())); }
public IEnumerable <ConsoleLine> GetLines(ConsoleId consoleId, int start, int end) { if (consoleId == null) { throw new ArgumentNullException(nameof(consoleId)); } var useOldKeys = false; var items = _connection.GetRangeFromSet(consoleId.GetSetKey(), start, end); if (items == null || items.Count == 0) { // Read operations should be backwards compatible and use // old keys, if new one don't contain any data. items = _connection.GetRangeFromSet(consoleId.GetOldConsoleKey(), start, end); useOldKeys = true; } foreach (var item in items) { var line = JobHelper.FromJson <ConsoleLine>(item); if (line.IsReference) { if (useOldKeys) { try { line.Message = _connection.GetValueFromHash(consoleId.GetOldConsoleKey(), line.Message); } catch { // This may happen, when using Hangfire.Redis storage and having // background job, whose console session was stored using old key // format. } } else { line.Message = _connection.GetValueFromHash(consoleId.GetHashKey(), line.Message); } line.IsReference = false; } yield return(line); } }
public void Persist(ConsoleId consoleId) { if (consoleId == null) { throw new ArgumentNullException(nameof(consoleId)); } _transaction.PersistSet(consoleId.GetSetKey()); _transaction.PersistHash(consoleId.GetHashKey()); // After upgrading to Hangfire.Console version with new keys, // there may be existing background jobs with console attached // to the previous keys. We should persist them also. _transaction.PersistSet(consoleId.GetOldConsoleKey()); _transaction.PersistHash(consoleId.GetOldConsoleKey()); }
public void Expire_ExpiresOldSetAndHashKeysEither_ForBackwardsCompatibility() { var storage = new ConsoleStorage(_connection.Object); storage.Expire(_consoleId, TimeSpan.FromHours(1)); _connection.Verify(x => x.CreateWriteTransaction(), Times.Once); _transaction.Verify(x => x.ExpireSet(_consoleId.GetOldConsoleKey(), It.IsAny <TimeSpan>())); _transaction.Verify(x => x.ExpireHash(_consoleId.GetOldConsoleKey(), It.IsAny <TimeSpan>())); _transaction.Verify(x => x.Commit(), Times.Once); }
public int GetLineCount(ConsoleId consoleId) { if (consoleId == null) { throw new ArgumentNullException(nameof(consoleId)); } var result = (int)_connection.GetSetCount(consoleId.GetSetKey()); if (result == 0) { // Read operations should be backwards compatible and use // old keys, if new one don't contain any data. return((int)_connection.GetSetCount(consoleId.GetOldConsoleKey())); } return(result); }