private void primeEditMonitor(DateTime lastActivityTimeUtc) { bool lastEditSet = false; _subreddit.GetEdited().Where(c => c.CreatedUTC > lastActivityTimeUtc.AddMinutes(-30)) .ForEach(edit => { if (!lastEditSet && isValidComment(edit.FullName).Result) { _db4Repository.SetLastProcessedEditId(edit.FullName); lastEditSet = true; } }); }
private async void monitorEdits(int editScanIntervalSeconds) { await Task.Factory.StartNew(async() => { while (true) { try { if (DateTime.UtcNow > _lastEditCheckUtc.AddSeconds(editScanIntervalSeconds)) { // Get the last valid processed edit // This looping / edit checking has to be done // since deleted edits won't return anything var lastEditIds = _db4Repository.GetLastProcessedEditIds(); string lastProcessedEditId = string.Empty; foreach (string lastEditId in lastEditIds) { if (await isValidComment(lastEditId)) { lastProcessedEditId = lastEditId; break; } } // If we got here with no editId, DeltaBot can't continue. if (string.IsNullOrEmpty(lastProcessedEditId)) { var latestEdits = await _subreddit.GetEdited(1).OrderByDescending(c => c.CreatedUTC).ToList(); if (latestEdits != null && latestEdits.Count > 0) { lastProcessedEditId = latestEdits[0].FullName; // Need to have a good starting point ASAP _db4Repository.SetLastProcessedEditId(lastProcessedEditId); _logger.Warn($"WARN: Had to bootstrap from latest edit: {lastProcessedEditId}!!"); } else { _logger.Error(new Exception(), "CRITICAL: No valid last processed edit found. DeltaBot cannot continue monitoring edits..."); break; } } var editsJson = await _subreddit.WebAgent.Get($"/r/{_subreddit.Name}/about/edited.json?only=comments&before={lastProcessedEditId}&limit=100"); var children = editsJson["data"]["children"] as JArray; var edits = new List <Comment>(); if (children != null && children.Count > 0) { foreach (var child in children) { edits.Add(Thing.Parse <Comment>(_subreddit.WebAgent, child)); } } if (edits.Count > 0) { // Make sure comments are sorted oldest to newest so oldest get processed first var sortedEdits = edits.OrderBy(c => c.CreatedUTC).ToList(); foreach (var comment in sortedEdits) { // Record the time when this was processed. // Whenever DeltaBot stops, it's going to read this time // and query / process all things starting from this time _db4Repository.SetLastActivityTimeUtc(); // Send to queue for processing _activityDispatcher.SendToQueue(comment); // Mark as the last processed edit _db4Repository.SetLastProcessedEditId(comment.FullName); } } _lastEditCheckUtc = DateTime.UtcNow; } Thread.Sleep(100); } catch (Exception ex) { _logger.Error(ex, "Error in edit monitor loop - continuing..."); } } }, _cancellationTokenSource.Token); _logger.Info("Started monitoring edits..."); }