public bool AddIfNotExists(string XMLFilename, EncoderThreadItem value) { if (this.Contains(XMLFilename)) return false; this.Add(XMLFilename,value); return true; }
public void MainLoop() { string sXMLSourceDirectory = @"c:\mediafiles\backup"; bool bFound = false; RetryXMLFileList retrylist = new RetryXMLFileList(); bool bVerbose = false; #if DEBUG log.WriteLog("Sleeping for 10 seconds"); System.Threading.Thread.Sleep(10000); #endif ActiveThreadCount = 0; MaxActiveThreadCount = 1; // begin routine // determine how many encoders can be active at once try { Microsoft.Win32.RegistryKey regKey = log.CarverLabRegistryKey(); MaxActiveThreadCount = (int)regKey.GetValue("MaxUploadThreadCount",1); int itemp; itemp = (int)regKey.GetValue("EncoderResponseTimeoutMinutes",7); if (itemp > short.MaxValue) EncoderResponseTimeoutMinutes = short.MaxValue; else EncoderResponseTimeoutMinutes = (short)itemp; log.bDebugMode = ((string)regKey.GetValue("DebugMode") == "1"); itemp = (int)regKey.GetValue("EncoderRetryFileIntervalSeconds",300); if (itemp > short.MaxValue) EncoderRetryFileIntervalSeconds = short.MaxValue; else EncoderRetryFileIntervalSeconds = (short)itemp; itemp = (int)regKey.GetValue("VerboseLogOutput"); if (itemp > 0) { bVerbose = true; log.WriteLog("*** Verbose output enabled! ***"); } } catch (System.Exception regex) { log.WriteLog("Registry read error: " + regex.Message); } log.WriteLog("Oyster Encoder will spawn at most " + MaxActiveThreadCount + " encoding threads."); // create a thread sync'ed list to hold thread instances ThreadList = new EncoderThreadList(); // get the directory name that video and xml files are dropped // into and store into vdirectory try { bFound = false; OysterClassLibrary.Oyster o = new OysterClassLibrary.Oyster(); foreach (OysterClassLibrary.ForwardingServer fs in o.FORWARDINGSERVERS) { if (fs.Address != "Not Assigned") { foreach (OysterClassLibrary.MediaBufferServer mbs in fs.CurrentMediaBufferServers) { if (mbs.Address != "Not Assigned") { sXMLSourceDirectory = mbs.StorageDirectory; bFound = true; // get OysterRootDirectory from the system OysterRootDirectory = (string) o.GetVideoStorageServerById(fs.CurrentVideoStorageServer.ID).CurrentVideoStorageServerType["OysterRootDirectory"]; // get OysterSourceDirectory from the system OysterSourceDirectory = (string) o.GetVideoStorageServerById(fs.CurrentVideoStorageServer.ID).CurrentVideoStorageServerType["OysterSourceDirectory"]; // get OysterProfileName from the system OysterProfileName = (string) o.GetVideoStorageServerById(fs.CurrentVideoStorageServer.ID).CurrentVideoStorageServerType["OysterProfile"]; break; } } } } if (!bFound) { log.WriteLog("Could not find a source directory in the configuration. There is nothing to do. Aborting."); return; } } catch (System.Exception oex) { log.WriteLog("MainLoop: When attempting to open the OysterClassLibrary and exception occured: " + oex.Message); return; } if (false == System.IO.Directory.Exists(sXMLSourceDirectory)) { try { System.IO.Directory.CreateDirectory(sXMLSourceDirectory); } catch (System.Exception dex) { log.WriteLog("Unable to create the directory " + sXMLSourceDirectory + ": " + dex.Message + ". Aborting."); return; } } log.WriteLog("Reading files from " + sXMLSourceDirectory); m_EventMonitor = new System.Threading.ManualResetEvent(true); CarverLab.Utility.EncodingServiceInfo esi = new CarverLab.Utility.EncodingServiceInfo(); esi.Initialize(@"c:\EncodingServiceInfo.xml"); dtStart = System.DateTime.Now; string FAILEDFILE = ""; // while this thread is active log.WriteLog("Entering work loop..."); while (false == IsDone) { try { #if !DEBUG System.Threading.Thread.Sleep(5000); #endif if (bVerbose) { log.WriteLog("Serializing EncodingServiceInfo"); } esi.Serialize(); // get a list of all files in vdirectory with a .xml extension System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sXMLSourceDirectory); System.IO.FileInfo[] fiFileInfoList = di.GetFiles("*.xml"); System.Collections.SortedList slFileInfoList = new System.Collections.SortedList(new FileInfoDateComparer(FileInfoDateComparer.DateCompareType.CreationTime)); System.IO.FileInfo[] fiFileInfoErrorList = di.GetFiles("*.err"); // check to see that the system is still running... // for each file in vdirectory if (bVerbose) { log.WriteLog("Read XML files from " + sXMLSourceDirectory); } System.Collections.ArrayList alErrs = new System.Collections.ArrayList(); if (esi.m_esi.CurrentEncodings != null) { for (int i = 0; i < esi.m_esi.CurrentEncodings.Length; i++) { CarverLab.Utility.Encoding enc = esi.m_esi.CurrentEncodings[i]; if (enc.XMLPathFile.EndsWith(".err")) { if (!System.IO.File.Exists(enc.XMLPathFile)) { alErrs.Add(enc); } } } } m_EventMonitor.WaitOne(10000,false); foreach (object obj in alErrs) { CarverLab.Utility.Encoding enc = (CarverLab.Utility.Encoding) obj; esi.Remove(enc.UniqueIdentifier); } m_EventMonitor.Set(); if (bVerbose) { log.WriteLog("Scanning files..."); } foreach (System.IO.FileInfo fi in fiFileInfoErrorList) { if (esi.UniqueIdentifierOfXMLFile(fi.FullName) == null) { m_EventMonitor.WaitOne(10000,false); string su = esi.Add(fi.FullName,"","",-1.0); esi.AddError(su,"Error File"); m_EventMonitor.Set(); if (bVerbose) { log.WriteLog("Error file: " + fi.FullName); } } } System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); foreach (System.IO.FileInfo fi in fiFileInfoList) { if (esi.UniqueIdentifierOfXMLFile(fi.FullName) == null) { FAILEDFILE = fi.FullName; CheckAndFixXMLInput(fi.FullName); CarverLab.Utility.CARDVIDEO cv = (CarverLab.Utility.CARDVIDEO)Deserialize(fi.FullName, typeof(CarverLab.Utility.CARDVIDEO)); m_EventMonitor.WaitOne(10000,false); if (cv == null) { esi.Add(fi.FullName,"","",-1.0); } else { esi.Add(fi.FullName,cv.FILENAME,cv.FILETITLE,-1.0); } if (bVerbose) { log.WriteLog("File: " + fi.FullName); } m_EventMonitor.Set(); } if (!retrylist.Contains(fi.FullName)) { slFileInfoList.Add(fi,fi); } else { TimeSpan ts = DateTime.Now - retrylist[fi.FullName].LastChecked; if (ts.TotalSeconds > EncoderRetryFileIntervalSeconds) { retrylist.Remove(fi.FullName); } } } foreach (System.Collections.DictionaryEntry entry in slFileInfoList) { System.IO.FileInfo fi = entry.Value as System.IO.FileInfo; if (bVerbose) { log.WriteLog("Considering " + fi.FullName); } // } // foreach (System.IO.FileInfo fi in fiFileInfoList) // { // sleep a bit to save processor time #if !DEBUG System.Threading.Thread.Sleep(1000); #endif if (ActiveThreadCount >= MaxActiveThreadCount) { break; } string sXMLFilename, sVideoFilename; FAILEDFILE = fi.FullName; sXMLFilename = fi.FullName; // search for filename in thread list // if filename exists in thread list skip to next file if (ThreadList.Contains(sXMLFilename)) { if (bVerbose) { log.WriteLog(fi.FullName + " is already active"); } continue; } // endif // open the xml file if (bVerbose) { log.WriteLog("Reading XML file " + sXMLFilename); } CarverLab.Utility.CARDVIDEO cv = (CarverLab.Utility.CARDVIDEO)Deserialize(sXMLFilename, typeof(CarverLab.Utility.CARDVIDEO)); // read the video file name in the xml file sVideoFilename = cv.FILENAME; // if no matching video file exists continue with the next file if (bVerbose) { log.WriteLog("Checking for matching video file"); } if (false == System.IO.File.Exists(sVideoFilename)) { m_EventMonitor.WaitOne(10000,false); ErrorRenameXML(null,sXMLFilename,"*ERROR* " + sVideoFilename + " does not exist for " + sXMLFilename); m_EventMonitor.Set(); continue; } System.IO.FileStream tfs; try { if (bVerbose) { log.WriteLog("Opening file: " + sVideoFilename); } tfs = System.IO.File.Open(sVideoFilename,System.IO.FileMode.Open); if (null == tfs) { m_EventMonitor.WaitOne(10000,false); ErrorRenameXML(null,sXMLFilename,"*ERROR* " + sVideoFilename + " could not open file."); m_EventMonitor.Set(); continue; } } catch (Exception ex1) { m_EventMonitor.WaitOne(10000,false); ErrorRenameXML(null,sXMLFilename,"*ERROR* " + sVideoFilename + " could not open file: " + ex1.Message); m_EventMonitor.Set(); continue; } if (bVerbose) { log.WriteLog("Checking length of file " + sVideoFilename); } long len = tfs.Length; tfs.Close(); if (len < (256 * 1024)) { m_EventMonitor.WaitOne(10000,false); ErrorRenameXML(null,sXMLFilename,"*ERROR* " + sVideoFilename + " file is too small, will not process."); m_EventMonitor.Set(); continue; } if (bVerbose) { log.WriteLog("Creating encoder for " + sVideoFilename); } EncoderThreadItem eti = new EncoderThreadItem(); // create a new thread that is suspended // assign the EncodeFile routine to the new thread System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ThreadStart(eti.Encode)); // pass the thread list index into the new thread eti.log = this.log; eti.m_bIsCompleted = false; eti.m_EncodeFileThread = thr; eti.m_sXMLFilename = sXMLFilename; eti.OysterProfileName = this.OysterProfileName; eti.OysterRootDirectory = this.OysterRootDirectory; eti.OysterSourceDirectory = this.OysterSourceDirectory; eti.m_EventMonitor = this.m_EventMonitor; eti.m_esi = esi; // place filename into the thread list ThreadList.Add(sXMLFilename,eti); // activate the new thread thr.Start(); //ActiveThreadCount++; log.WriteLog("New encoding thread created. ActiveThreadCount = " + (ActiveThreadCount + 1) + " of " + MaxActiveThreadCount); FAILEDFILE = ""; // endfor } fiFileInfoList = null; di = null; } catch (System.Exception whex) { if (FAILEDFILE.Length > 0) { retrylist[FAILEDFILE] = new RetryXMLFile(); TimeSpan ts = DateTime.Now - retrylist[FAILEDFILE].LastChecked; retrylist[FAILEDFILE].LastChecked = DateTime.Now; if (ts.TotalSeconds > 15) // if (retrylist[FAILEDFILE].CheckedCount++ > 5) { log.WriteLog(FAILEDFILE + " being renamed."); System.IO.FileInfo fi = new System.IO.FileInfo(FAILEDFILE); fi.MoveTo(System.IO.Path.ChangeExtension(fi.FullName,"failed")); } } else { log.WriteLog("*ERROR* EncodeLauncher.MainLoop Exception: " + whex.Message); log.WriteLog(" Pausing for one second before continuing..."); } System.Threading.Thread.Sleep(1000); } // cleanup any completed threads System.Collections.ArrayList al = new System.Collections.ArrayList(); foreach (string ss in ThreadList) { EncoderThreadItem ti = ThreadList[ss]; bool bAddIt, bAbortIt; bAddIt = false; bAbortIt = false; if (ti.IsCompleted) { bAddIt = true; log.WriteLog(ti.m_sXMLFilename + " is scheduled for release."); } else { try { // if no error on the encoder // check for the encoder lockup by comparing the the encoding // time right now with the encoding time save one minute ago. if (false == ti.IsEncoderResponding(EncoderResponseTimeoutMinutes)) { log.WriteLog(ti.m_sXMLFilename + " is scheduled for emergency release."); bAddIt = true; bAbortIt = true; } } catch (System.Exception handlerex) { log.WriteLog("In the Thread Freeze handler: " + handlerex.Message); bAddIt = true; if (null != ti.m_EncodeFileThread) bAbortIt = true; } } if (bAddIt) { if (!bAbortIt) { al.Add(ti.m_sXMLFilename); //ActiveThreadCount--; } //log.WriteLog(ss + ": encoding thread destroyed. ActiveThreadCount = " + ActiveThreadCount // + " of " + MaxActiveThreadCount); } if (bAbortIt) { log.WriteLog(ss + ": encoder not responding, **ABORTING** thread."); log.WriteLog(ss + ": Performing Emergency Stop."); ti.EmergencyStop(); m_EventMonitor.WaitOne(10000,false); ThreadList.Remove(ti.m_sXMLFilename); m_EventMonitor.Set(); log.WriteLog(ss + ": Thread removed from management list."); ActiveThreadCount--; } } foreach (string ss in al) { string sXMLFilename, sVideoFilename; sXMLFilename = ss; // open the xml file CarverLab.Utility.CARDVIDEO cv = (CarverLab.Utility.CARDVIDEO)Deserialize(sXMLFilename, typeof(CarverLab.Utility.CARDVIDEO)); // read the video file name in the xml file sVideoFilename = cv.FILENAME; // delete the XML and video files log.WriteLog("Cleaning up XML and video source files"); try { if (ThreadList[sXMLFilename].DidEncodingFail) { // skip deletion of files log.WriteLog("\t" + sXMLFilename + " is being skipped for deletion."); } else { log.WriteLog("\t" + sXMLFilename); System.IO.File.Delete(sXMLFilename); log.WriteLog("\t" + sVideoFilename); System.IO.File.Delete(sVideoFilename); } m_EventMonitor.WaitOne(10000,false); esi.Remove(ThreadList[ss].m_sUniqueIdentifier); m_EventMonitor.Set(); } catch(System.Exception fdex) { log.WriteLog("Unable to delete file: " + fdex.Message); //esi.UpdateError(ThreadList[ss].m_sUniqueIdentifier,"Failed to stop."); } ThreadList.Remove(ss); } // end while } // end routine esi.DeInitialize(); log.WriteLog("Clean exit."); }
public void Add(string XMLFilename, EncoderThreadItem value) { this.BaseAdd(XMLFilename,value); }