Example #1
0
 public void Writer()
 {
     try
     {
         if (!core.DatabaseServerIsAvailable)
         {
             Log("No sql server is available, closing DB log writer");
             return;
         }
         if (core.DB == null)
         {
             Log("core.DB is null", true);
         }
         DebugLog("The SQL writer started");
         while (core._Status != core.Status.ShuttingDown)
         {
             string message = "";
             try
             {
                 Thread.Sleep(2000);
                 if (DJ.Count > 0)
                 {
                     List <Item> db = new List <Item>();
                     lock (DJ)
                     {
                         db.AddRange(DJ);
                         DJ.Clear();
                     }
                     lock (core.DB.DatabaseLock)
                     {
                         core.DB.Connect();
                         while (!core.DB.IsConnected)
                         {
                             if (core.DB.ErrorBuffer != null)
                             {
                                 Log("Unable to connect to SQL server: " + core.DB.ErrorBuffer + " retrying in 20 seconds");
                             }
                             Thread.Sleep(20000);
                             core.DB.Connect();
                         }
                         foreach (Item item in db)
                         {
                             Database.Row row = new Database.Row();
                             message = item.message;
                             row.Values.Add(new Database.Row.Value(0));
                             row.Values.Add(new Database.Row.Value(item.channel.Name, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.username, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.time));
                             row.Values.Add(new Database.Row.Value(item.act));
                             row.Values.Add(new Database.Row.Value(item.message, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.type));
                             row.Values.Add(new Database.Row.Value(item.host, Database.DataType.Varchar));
                             if (!core.DB.InsertRow("logs", row))
                             {
                                 Log("Failed to insert row, recycling: " + message);
                                 lock (DJ)
                                 {
                                     DJ.Add(item);
                                 }
                             }
                         }
                         core.DB.Commit();
                         core.DB.Disconnect();
                     }
                 }
             }
             catch (ThreadAbortException)
             {
                 if (core.DatabaseServerIsAvailable)
                 {
                     core.DB.Commit();
                     core.DB.Disconnect();
                 }
                 Log("SQL Writer is shut down with " + DJ.Count.ToString() + " unfinished lines");
                 return;
             }
             catch (Exception fail)
             {
                 handleException(fail);
                 Log("SQL Writer error: " + message, true);
             }
         }
     }
     catch (Exception fail)
     {
         handleException(fail);
         Log("SQL Writer is down!!", true);
     }
 }
Example #2
0
 public void Writer()
 {
     try
     {
         if (!Core.DatabaseServerIsAvailable)
         {
             Log("No sql server is available, closing DB log writer");
             return;
         }
         if (Core.DB == null)
         {
             Log("core.DB is null", true);
         }
         DebugLog("The SQL writer started");
         while (Core.IsRunning)
         {
             string message = "";
             try
             {
                 Thread.Sleep(2000);
                 if (DJ.Count > 0)
                 {
                     List<Item> db = new List<Item>();
                     lock (DJ)
                     {
                         db.AddRange(DJ);
                         DJ.Clear();
                     }
                     lock (Core.DB.DatabaseLock)
                     {
                         Core.DB.Connect();
                         while (!Core.DB.IsConnected)
                         {
                             if (Core.DB.ErrorBuffer != null)
                             {
                                 Log("Unable to connect to SQL server: " + Core.DB.ErrorBuffer + " retrying in 20 seconds");
                             }
                             Thread.Sleep(20000);
                             Core.DB.Connect();
                         }
                         foreach (Item item in db)
                         {
                             Database.Row row = new Database.Row();
                             message = item.message;
                             row.Values.Add(new Database.Row.Value(0));
                             row.Values.Add(new Database.Row.Value(item.channel.Name, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.username, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.time));
                             row.Values.Add(new Database.Row.Value(item.act));
                             row.Values.Add(new Database.Row.Value(item.message, Database.DataType.Varchar));
                             row.Values.Add(new Database.Row.Value(item.type));
                             row.Values.Add(new Database.Row.Value(item.host, Database.DataType.Varchar));
                             if (!Core.DB.InsertRow("logs", row))
                             {
                                 Log("Failed to insert row: " + message);
                             }
                         }
                         Core.DB.Commit();
                         Core.DB.Disconnect();
                     }
                 }
             }
             catch (ThreadAbortException)
             {
                 if (Core.DatabaseServerIsAvailable)
                 {
                     Core.DB.Commit();
                     Core.DB.Disconnect();
                 }
                 Log("SQL Writer is shut down with " + DJ.Count.ToString() + " unfinished lines");
                 return;
             }
             catch (Exception fail)
             {
                 HandleException(fail);
                 Log("SQL Writer error: " + message, true);
             }
         }
     }
     catch (Exception fail)
     {
         HandleException(fail);
         Core.ThreadManager.UnregisterThread(Thread.CurrentThread);
         Log("SQL Writer is down!!", true);
     }
 }