public void Rebuild() { if (Rebuilding) { return; } Rebuilt = 0; Task.Run(() => { try { Rebuilding = true; lock (_locker) { _dir.ClearLock("rebuilding"); using (var writer = new IndexWriter(_dir, Analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { writer.DeleteAll(); } } DateTime start = DateTime.Now.Subtract(TimeSpan.FromDays(_recordDays)); while (start < DateTime.Now.AddDays(1)) { string file = _dataDirectory + "/source/" + start.ToString("yyyy-MMM-dd") + "/source.src"; if (File.Exists(file)) { byte[] txt = File.ReadAllBytes(file); foreach (string entry in StringZipper.Unzip(txt).Split(new[] { "<|||>" }, StringSplitOptions.RemoveEmptyEntries)) { AuditSourceRecord record = _jsonSerializationService.DeserializeObject <AuditSourceRecord>(entry); Log(record.Entry, record.Content, false); Rebuilt++; } } start = start.AddDays(1); } } catch (Exception e) { Sitecore.Diagnostics.Log.Error(e.ToString(), this); } finally { Rebuilt = -1; Rebuilding = false; } }); }
private void KickOptimizeTimer() { if (_optimizeTimer == 0) { _optimizeTimer = 100; Task.Run(() => { try { ValidateBackup(); lock (_locker) { StringBuilder sb = new StringBuilder(); using (var writer = new IndexWriter(_dir, Analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { if (_recordDays > 0 && _clearOld) { var query = Parser.Parse($"date:[0 TO {DateTime.Now.AddDays(_recordDays*-1):yyyyMMdd}]"); writer.DeleteDocuments(query); _clearOld = false; } while (_optimizeTimer > 1) { while (_writeQueue.Any()) { Tuple <AuditSourceRecord, Document> doc; if (_writeQueue.TryDequeue(out doc)) { writer.AddDocument(doc.Item2); if (doc.Item1 != null) { WriteSource(doc.Item1, sb); } } } _optimizeTimer--; Thread.Sleep(100); } try { string dir = _dataDirectory + "/source/" + DateTime.Now.ToString("yyyy-MMM-dd"); if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } if (!File.Exists(dir + "/source.src")) { File.Create(dir + "/source.src"); } int count = 1; while (count < 100 && IsFileLocked(new FileInfo(dir + "/source.src"))) { Task.Delay(100).Wait(); count++; } byte[] bytes = File.ReadAllBytes(dir + "/source.src"); File.WriteAllBytes(dir + "/source.src", bytes.Length != 0 ? StringZipper.Zip(StringZipper.Unzip(bytes) + sb) : StringZipper.Zip(sb.ToString())); } catch (Exception e) { Sitecore.Diagnostics.Log.Error("Unable to write the audit logger source", e, this); } writer.Commit(); writer.Optimize(); _optimizeTimer = 0; } } } catch (Exception e) { Sitecore.Diagnostics.Log.Error("Unable to commit to audit logger log", e, this); _optimizeTimer = 0; } }); } else { _optimizeTimer = 100; } }