public static void TimerElapsed(object source, ElapsedEventArgs e) { // This method invokes the Local file retransmit function FIRST, THEN invokes the db retransmit method // Don't need to await this, it can happen any time after, I just need it to be executed at some point RecordHandler.RetransmitFailedAsync(); Console.WriteLine($"Retransmit Interval invoked at: {e.SignalTime}; Invoked: ({counter}) times since Service start."); SQLServerStream.counter++; }
public static async void Changed(object sender, RecordChangedEventArgs <ModelProcessedHistory> e) { var changedEntity = e.Entity; Console.WriteLine("DML operation: " + e.ChangeType); Console.WriteLine("Item ID: " + changedEntity.HistoryID); Console.WriteLine("ModelElementID: " + changedEntity.ModelElementID); Console.WriteLine("ModelJobID: " + changedEntity.ModelJobID); Console.WriteLine("OrderID: " + changedEntity.OrderID); //RecordHandler.RetransmitFailedLocalAsync(); // Debug only, delete it from here when done if (e.ChangeType == TableDependency.Enums.ChangeType.Insert) { //DML operation of type insert, try push this entry to logging server try { bool transmitStatus = await RecordHandler.TransmitAsync(e); if (transmitStatus) { Console.WriteLine($"Record ID: {changedEntity.HistoryID} successfully transmitted"); } else { // This section executed if either the transmission failed and record was logged to file OR! // The UnitsCounted field was had a value Zero causing a reconcillitation action to be performed Console.WriteLine($"FAILED: Record {changedEntity.HistoryID} transmition failed! Deffered processing"); } } catch (HttpRequestException ex) { Console.WriteLine("A HTTP connection error was encountered, could be because of Web Server being down/unreachable"); Console.WriteLine($"FAILED: Record {changedEntity.HistoryID} transmition failed! It has been logged to file system for later transmittion"); await RecordHandler.RetransmitFailedDBAsync(); // This statement is only for debug must be DELETED when done debugging! dynamic record = JSONConverter.EntityToJObject(e); // If record is null then return, don't continue processing if (record == null) { return; } // Log current record to File system, it was never trasmitted in the first place due to connection issues RecordHandler.LogFailed(record.ToString()); } } }
public static async Task LoginAsync(string WebServerAddress, string WebServerUsername, string WebServerPassword) { await RecordHandler.LoginAsync(WebServerAddress, WebServerUsername, WebServerPassword); }
public static async Task <bool> RetransmitFailedDBAsync() { // This method performs a manual check (query) on DB to find records that have not been marked as trasmitted and transmit them // Can ignore the last 10 records for safety, this will prevent race condition where we manually retransmit a record that is currently // being processed, i.e it just fired it's insert listener and is still being processed then we end up manually retransmitting before // it is finished processing // Performs query on SQL Server marking the current record as trasmitted // Called as soon as a success message is received from web server after calling the /save api endpoint // If even one record fails to transmit and is instead logged to file, this flag set to false, use it to initiate local files retransmition bool transmitStatusAll = true; using (SqlConnection connection = new SqlConnection(Globals.DBConnectionString)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = $"SELECT * FROM {Globals.DBTableName} WHERE AlreadyExported = @exp"; //command.Parameters.AddWithValue("@tbl", Globals.DBTableName); command.Parameters.AddWithValue("@exp", "0"); // Mess with this figure to see changes on different records connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { dynamic record = JSONConverter.SqlDataReaderToJObject(reader); if (record == null) { // Current record had an incorrect value for numberCounted - skip it // In this case, still mark all records as transmitted since nothing really failed, // just an invalid record which initiated reconciliation, but didn't fail, so 'continue' looping // skipping the reconcilliated record continue; } try { // Transmit one by one to web server /save bool transmitStatus = await TransmitJSONRecordAsync(record.ToString()); if (transmitStatus) { Console.WriteLine($"DBTOFILESERVER: Record ID: {record.dataset.HistoryID} successfully transmitted"); } else { Console.WriteLine($"FAILED:DBTOFILESERVER: Record {record.dataset.HistoryID} transmition failed! It has been logged to file system for later transmittion"); transmitStatusAll = false; } } catch (HttpRequestException ex) { Console.WriteLine("DBTOFILESERVER: A HTTP connection error was encountered, could be because of Web Server being down/unreachable"); Console.WriteLine($"FAILED:DBTOFILESERVER: Record {record.dataset.HistoryID} transmition failed! It has been logged to file system for later transmittion"); transmitStatusAll = false; // Log current record to File system, it was never trasmitted in the first place due to connection issues RecordHandler.LogFailed(record.ToString()); } } // Each record trasmitted independently thus will have own status // If current record transmitStatus is true, then transmitStatus remains true // If current record transmitStatus is false then log record to file system instead } connection.Close(); } return(transmitStatusAll); }