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 void AddLine(ConsoleId consoleId, ConsoleLine line) { if (consoleId == null) { throw new ArgumentNullException(nameof(consoleId)); } if (line == null) { throw new ArgumentNullException(nameof(line)); } if (line.IsReference) { throw new ArgumentException("Cannot add reference directly", nameof(line)); } using (var tran = _connection.CreateWriteTransaction()) { // check if encoded message fits into Set's Value field string value; if (line.Message.Length > ValueFieldLimit - 36) { // pretty sure it won't fit // (36 is an upper bound for JSON formatting, TimeOffset and TextColor) value = null; } else { // try to encode and see if it fits value = JobHelper.ToJson(line); if (value.Length > ValueFieldLimit) { value = null; } } if (value == null) { var referenceKey = Guid.NewGuid().ToString("N"); tran.SetRangeInHash(consoleId.GetHashKey(), new[] { new KeyValuePair <string, string>(referenceKey, line.Message) }); line.Message = referenceKey; line.IsReference = true; value = JobHelper.ToJson(line); } tran.AddToSet(consoleId.GetSetKey(), value, line.TimeOffset); tran.Commit(); } }
public void AddLine_ShortLineIsAddedToSet() { var storage = new ConsoleStorage(_connection.Object); var line = new ConsoleLine() { Message = "test" }; storage.AddLine(_consoleId, line); Assert.False(line.IsReference); _connection.Verify(x => x.CreateWriteTransaction(), Times.Once); _transaction.Verify(x => x.AddToSet(_consoleId.GetSetKey(), It.IsAny <string>(), It.IsAny <double>())); _transaction.Verify(x => x.SetRangeInHash(_consoleId.GetHashKey(), It.IsAny <IEnumerable <KVP> >()), Times.Never); _transaction.Verify(x => x.Commit(), Times.Once); }
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 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); }