private static void InsertProgramsThreadStart()
{
try
{
Log.Debug("BusinessLayer: InsertProgramsThread started");
IGentleProvider prov = ProviderFactory.GetDefaultProvider();
string provider = prov.Name.ToLowerInvariant();
string defaultConnectString = prov.ConnectionString;
DateTime lastImport = DateTime.Now;
InsertProgramsDelegate insertProgams;
switch (provider)
{
case "mysql":
insertProgams = InsertProgramsMySql;
break;
case "sqlserver":
insertProgams = InsertProgramsSqlServer;
break;
default:
Log.Info("BusinessLayer: InsertPrograms unknown provider - {0}", provider);
return;
}
while (true)
{
if (lastImport.AddSeconds(60) < DateTime.Now)
{
// Done importing and 60 seconds since last import
// Remove old programs
TvBusinessLayer layer = new TvBusinessLayer();
layer.RemoveOldPrograms();
// Let's update states
Schedule.SynchProgramStatesForAll();
// and exit
lock (_programInsertsQueue)
{
// Has new work been queued in the meantime?
if (_programInsertsQueue.Count == 0)
{
Log.Debug("BusinessLayer: InsertProgramsThread exiting");
_insertProgramsThread = null;
break;
}
}
}
_pendingProgramInserts.WaitOne(10000); // Check every 10 secs
while (_programInsertsQueue.Count > 0)
{
try
{
ImportParams importParams;
lock (_programInsertsQueue)
{
importParams = _programInsertsQueue.Dequeue();
}
importParams.ConnectString = defaultConnectString;
Thread.CurrentThread.Priority = importParams.Priority;
insertProgams(importParams);
Log.Debug("BusinessLayer: Inserted {0} programs to the database", importParams.ProgramList.Count);
lastImport = DateTime.Now;
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
}
catch (Exception ex)
{
Log.Error("BusinessLayer: InsertMySQL/InsertMSSQL caused an exception:");
Log.Write(ex);
}
}
// Now all queued inserts have been processed, clear Gentle cache
Gentle.Common.CacheManager.ClearQueryResultsByType(typeof (Program));
}
}
catch (Exception ex)
{
Log.Error("BusinessLayer: InsertProgramsThread error - {0}, {1}", ex.Message, ex.StackTrace);
}
}