protected virtual void ExecuteWrapper(object stateInfo) { try { // if already running then do nothing if (RunEvent.WaitOne(0)) { LogWarning("another thread already running."); return; } if (PauseEvent.WaitOne(0)) { LogWarning("service is paused."); return; } var result = Result <string> .Success(); var batch = new RSMDB.BatchHistory { RunStart = DateTime.Now, SystemId = this.ExternalSystem.Id, }; try { RunEvent.Set(); Interlocked.Increment(ref _iterations); LogMessage("executing."); State = TaskState.Running; result = Execute(stateInfo); } finally { State = TaskState.Idle; RunEvent.Reset(); using (var controller = new BatchHistories()) { var text = result != null ? result.Entity : string.Empty; var msg = (result.Succeeded) ? LogMessageDetail(result.ToString(), "completed. {0}", text) : LogErrorDetail(result.ToString(), "execution failed. {0}", text); batch.RunEnd = DateTime.Now; batch.Message = msg; batch.Outcome = (int)result.Outcome; controller.Add(batch); } } } catch (Exception e) { LogException(e, "execution failed."); } }
private void Revert( CancellationTokenSource tokenSrc, Action <string> UpdateStatus) { while (FilesProcessed.Count > 0) { PauseEvent.WaitOne(); UpdateStatus($"Deleting file: {FilesProcessed[0]}"); File.Delete(FilesProcessed[0]); FilesProcessed.RemoveAt(0); } while (FoldersCreated.Count > 0) { PauseEvent.WaitOne(); UpdateStatus($"Deleting folder: {FoldersCreated[0]}"); Directory.Delete(FoldersCreated[0], true); FoldersCreated.RemoveAt(0); } }
private void Work( CancellationTokenSource tokenSrc, Action <string> UpdateStatus) { while (!tokenSrc.IsCancellationRequested) { PauseEvent.WaitOne(); KeyValuePair <string, List <FileInfo> >?work = GetWork(); if (work == null) { //// task exiting. No more files to process. return; } string targetFolder = work.Value.Key; List <FileInfo> srcFiles = work.Value.Value; if (!Directory.Exists(targetFolder)) { DirectoryInfo inf = new DirectoryInfo(targetFolder); while (!inf.Parent.Exists) { inf = inf.Parent; } UpdateStatus?.Invoke($"Creating directory: {targetFolder}"); Directory.CreateDirectory(targetFolder); if (!FoldersCreated.Contains(inf.FullName)) { lock (_sync) { if (!FoldersCreated.Any(x => inf.FullName.Contains(x))) { FoldersCreated.Add(inf.FullName); } } } } foreach (FileInfo srcFile in srcFiles) { PauseEvent.WaitOne(); if (tokenSrc.IsCancellationRequested) { break; } UpdateStatus?.Invoke($"Processing: {srcFile.FullName}"); string dstFile = FileOrganizerUtility.GetTargetFileName( srcFile.FullName, targetFolder); if (File.Exists(dstFile)) { dstFile = FileOrganizerUtility.AddGuidToFileNameConflict(dstFile); } if (srcFile.CopyTo(dstFile).Exists != true) { throw new IOException( $"Failed to copy file from '{srcFile.FullName}' to '{dstFile}'"); } FilesProcessed.Add(dstFile); UpdateStatus?.Invoke($"Copied source: {srcFile.FullName} to destination: {dstFile}"); } } }
protected override void Read() { using (var reader = new StreamReader(_fileName)) { // Similar to EventMessages.ReadOuterXml(), but send the messages to the handler as we // read them. var adaptor = new XmlReadAdaptor(new XmlTextReader(reader), Xml.Namespace); if (adaptor.IsReadingElement(Xml.EventMessagesElement)) { var messages = new EventMessages(); while (adaptor.ReadElement(Xml.EventMessageElement)) { // Check the reader wait handles - should we stop or pause? if (StopEvent.WaitOne(0, false)) { return; } if (PauseEvent.WaitOne(0, false)) { // Paused - wait for stop or continue. var pauseHandles = new[] { StopEvent, ContinueEvent }; switch (WaitHandle.WaitAny(pauseHandles)) { case 0: return; // Stop. case 1: break; // Continue. } } var message = new EventMessage(); message.ReadXml(adaptor.XmlReader); adaptor.ReadEndElement(); // Send to the message handler, if we have enough. messages.Add(message); if (messages.Count == Constants.MessageComponents.EventFile.MessageReaderBatchSize) { HandleEventMessages(messages); messages = new EventMessages(); } } // Send any remaining messages to the message handler. if (messages.Count > 0) { HandleEventMessages(messages); } } } OnExistingMessagesRead(System.EventArgs.Empty); }