public Task <int> DeleteExpiresAsync(string table, DateTime timeout, int batchCount = 1000, CancellationToken token = default) { var removed = 0; if (table == nameof(PublishedMessages)) { var ids = PublishedMessages.Values.Where(x => x.ExpiresAt < timeout).Select(x => x.DbId).ToList(); foreach (var id in ids) { if (PublishedMessages.TryRemove(id, out _)) { removed++; } } } else { var ids = ReceivedMessages.Values.Where(x => x.ExpiresAt < timeout).Select(x => x.DbId).ToList(); foreach (var id in ids) { if (PublishedMessages.TryRemove(id, out _)) { removed++; } } } return(Task.FromResult(removed)); }
public bool ChangePublishedState(long messageId, string state) { var msg = PublishedMessages.First(x => x.Id == messageId); msg.Retries++; msg.ExpiresAt = null; msg.StatusName = state; return(true); }
public Task ChangePublishStateAsync(MediumMessage message, StatusName state) { var msg = PublishedMessages.FindOne(l => l.Id == message.DbId); msg.StatusName = state; msg.ExpiresAt = message.ExpiresAt; PublishedMessages.Update(msg); return(Task.CompletedTask); }
public async Task <IEnumerable <CapPublishedMessage> > GetPublishedMessagesOfNeedRetry() { return(await PublishedMessages.ToAsyncEnumerable() .Where(x => x.Retries < _capOptions.FailedRetryCount && x.Added < DateTime.Now.AddSeconds(-10) && (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) .Take(200) .ToListAsync()); }
public LiteDBStorage(IOptions <CapOptions> capOptions, IOptions <LiteDBOptions> ldboption) { _capOptions = capOptions; _ldboption = ldboption.Value; _lite = new LiteDatabase(_ldboption.ConnectionString); PublishedMessages = _lite.GetCollection <LiteDBMessage>(nameof(PublishedMessages)); PublishedMessages.EnsureIndex(l => l.Id, true); ReceivedMessages = _lite.GetCollection <LiteDBMessage>(nameof(ReceivedMessages)); ReceivedMessages.EnsureIndex(l => l.Id, true); }
public Task <int> DeleteExpiresAsync(string table, DateTime timeout, int batchCount = 1000, CancellationToken token = default) { var removed = 0; if (table == nameof(PublishedMessages)) { removed = PublishedMessages.DeleteMany(x => x.ExpiresAt < timeout); } else { removed = ReceivedMessages.DeleteMany(x => x.ExpiresAt < timeout); } return(Task.FromResult(removed)); }
public Task <IEnumerable <MediumMessage> > GetPublishedMessagesOfNeedRetry() { var ret = PublishedMessages .Find(x => x.Retries < _capOptions.Value.FailedRetryCount && x.Added < DateTime.Now.AddSeconds(-10) && (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) .Take(200) .Select(x => (MediumMessage)x); foreach (var message in ret) { message.Origin = StringSerializer.DeSerialize(message.Content); } return(Task.FromResult(ret)); }
public MediumMessage StoreMessage(string name, Message content, object dbTransaction = null) { var message = new MediumMessage { DbId = content.GetId(), Origin = content, Content = StringSerializer.Serialize(content), Added = DateTime.Now, ExpiresAt = null, Retries = 0 }; PublishedMessages.Insert(new LiteDBMessage() { Id = message.DbId, Name = name, Content = message.Content, Retries = message.Retries, Added = message.Added, ExpiresAt = message.ExpiresAt, StatusName = StatusName.Scheduled }); return(message); }
public Task <CapPublishedMessage> GetPublishedMessageAsync(long id) { return(PublishedMessages.ToAsyncEnumerable().FirstOrDefaultAsync(x => x.Id == id)); }