public void StartConsole() { _cancellationTokenSource = new CancellationTokenSource(); WriteEventLogEntry("Service started.", EventLogEntryType.Information); mail.SendMail("Audio Processor Service started."); // Send mail to notify that service started Task.Run( async() => { CancellationToken token = _cancellationTokenSource.Token; CheckConfig(); //Check prerequsites. (ffmpeg.exe particularly) _speechRecognition = new SpeechRecognition(eventLog, mail, tempDirectoryPath); while (!token.IsCancellationRequested) { try { await ProcessTask(); // An ETL Job to read from DB to update another DB await Task.Delay(TimeSpan.FromMinutes(1), token); } catch (Exception ex1) { Elmah.CreateLogEntry(ex1, eventLog); WriteEventLogEntry(ex1.ToString(), EventLogEntryType.Error); mail.SendMail("Audio Processor Service Error: " + ex1.Message); //Environment.Exit(1); throw new InvalidOperationException("Exception in service", ex1); // this cause the service to stop with status as failed } } WriteEventLogEntry("Service stopped gracefully.", EventLogEntryType.Information); mail.SendMail("Audio Processor Service Stopped..."); //terminate = false; }); }
/// <summary> /// Writes to the event log, writes to ELMAH, and sends an email. /// </summary> /// <param name="errorMessage"></param> /// <param name="ex"></param> private void HandleError(string errorMessage, Exception ex) { WriteEventLogEntry(errorMessage + Environment.NewLine + ex?.ToString(), EventLogEntryType.Error); if (ex != null) { Elmah.CreateLogEntry(ex, eventLog); } mail.SendMail(errorMessage + ex?.Message); }
/// <summary> /// watcher thread method. Here all the processing happens. /// </summary> public async Task ProcessTask() { try { using (var db = new trackdocEntities()) { _speechRecognition.HandleCompleted(db); var audioIdsToConvert = GetJobList(db);//.OrderByDescending(x => x).ToList(); var pendingSpeechRecognitionJobs = _speechRecognition.GetPendingSpeechRecJobs(db); var pendingSpeechRecAudioIds = _speechRecognition.GetPendingSpeechRecJobIds(pendingSpeechRecognitionJobs); var audioIdsToDownload = audioIdsToConvert.Union(pendingSpeechRecAudioIds).Distinct().OrderBy(a => a); var audioMetadatas = GetAudioMetadatas(audioIdsToDownload, db); foreach (var audioId in audioIdsToDownload) { AudioMetadata audioMetadata = null; try { audioMetadata = audioMetadatas.First(a => a.audioId == audioId); await Shared.StreamBlobToFile(connectionString, audioId, audioMetadata.UnconvertedPath, eventLog); if (audioMetadata.OriginalAudioType == "dss" || audioMetadata.OriginalAudioType == "ds2") { ConvertToWav(audioMetadata); } if (pendingSpeechRecAudioIds.Contains(audioId)) { var speechRecJob = pendingSpeechRecognitionJobs.First(s => s.audio_id == audioId); _speechRecognition.InitiateSpeechRecProcessing(speechRecJob, audioMetadata.UnconvertedPath, db); } if (audioIdsToConvert.Contains(audioId)) { ConvertAudio(audioMetadata, db); } } catch (Exception ex) { SetErrorStatus(audioId); Elmah.CreateLogEntry(ex, eventLog); WriteEventLogEntry("Processing of audio " + audioId + " failed: " + ex.ToString(), EventLogEntryType.Error); mail.SendMail("Error processing audio " + audioId + ": " + ex.Message); } finally { DeleteIfExists(audioMetadata?.UnconvertedPath); DeleteIfExists(audioMetadata?.IntermediateWavPath); DeleteIfExists(audioMetadata?.FinalPath); } } } PurgeOldAudios(); } catch (Exception ex) { Elmah.CreateLogEntry(ex, eventLog); WriteEventLogEntry(ex.ToString(), EventLogEntryType.Error); mail.SendMail("Audio Processor Service Error: " + ex.Message); } }