public static void pollManagerForAvailableJobs() { string filename = ""; Stream fileStream = findAvailableJob(ref filename); if (fileStream == null) { return; } Console.WriteLine("Acquired pending job at " + DateTime.Now.ToString()); Console.WriteLine("Job " + Path.GetFileName(filename)); //update perforce syncPerforce(); //we have a job. pendingJobXML pendingJob = null; try { XmlSerializer s = new XmlSerializer(typeof(pendingJobXML), new Type[] { }); pendingJob = (pendingJobXML)s.Deserialize(fileStream); } catch (Exception e) { //this job file is invalid. Delete it so that no one else tries to grab it fileStream.Position = 0; fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.Close(); try { File.Delete(filename); }catch (Exception et) { }; } bool processOK = false; if (pendingJob != null) { processOK = processJob(fileStream, filename, pendingJob); } Console.WriteLine("Entering polling mode"); Console.WriteLine("================================================"); }
public static bool processJob(Stream fileStream, string filename, pendingJobXML pendingJob) { DateTime timeNow = DateTime.Now; //Do our AO calcs////////////////////////////////////////////////////////////////////////// Console.WriteLine(".Starting AO Gen"); LightingClientMain client = new LightingClientMain(); LightingClientMain.eReturnCodes ret = client.generateLighting(pendingJob.issuingSourceFile, networkAOInterface.ResultDir + pendingJob.issuingSystemName, pendingJob.issuingSystemName, pendingJob.quality, pendingJob.totalNumberOfSections, pendingJob.targetSectionIndex); if (ret != LightingClientMain.eReturnCodes.cRC_OK) { //invalidate our job, incase someone else tries to grab it before we delete it fileStream.Position = 0; fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.Close(); //if the error was a missing / corrupted TDL, delete our job so no one else grabs it if (ret == LightingClientMain.eReturnCodes.cRC_TDL_LOAD_ERROR || ret == LightingClientMain.eReturnCodes.cRC_TDL_NOT_FOUND) { try { File.Delete(filename); } catch (Exception e) { } } ; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("....AO Gen FAILED"); Console.ForegroundColor = ConsoleColor.White; return(false); } else { Console.WriteLine("....AO Gen Complete"); //This is tricky.. Hopefully no one will be scanning for availability as soon as we do this... //invalidate our job, incase someone else tries to grab it before we delete it fileStream.Position = 0; fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.WriteByte(128); fileStream.Close(); try { File.Delete(filename); }catch (Exception ed) { }; TimeSpan ts = DateTime.Now - timeNow; float totalTime = (float)ts.TotalMinutes; Console.WriteLine("Work Finished; Total Time = " + totalTime + " minutes"); return(true); } return(true); }
void bw_DoWork(object sender, DoWorkEventArgs e) { string mHostName = Dns.GetHostName(); AmbientOcclusion.eAOQuality quality = (AmbientOcclusion.eAOQuality)e.Argument; //Copy our data & jobs on a worker thread //write our temporary data Directory.CreateDirectory(networkAOInterface.SourceDir + "\\" + mHostName); string sourceInputFile = networkAOInterface.SourceDir + "\\" + mHostName + "\\data.ZIP"; writeTempData(sourceInputFile); if (mWorkerThread.CancellationPending) { e.Cancel = true; return; } //Issue our job files to the network XmlSerializer s = new XmlSerializer(typeof(pendingJobXML), new Type[] { }); for (int i = 0; i < cNumSections; i++) { pendingJobXML pending = new pendingJobXML(); pending.workingJobGUID = mCurrJobGUID; pending.issuingSystemName = mHostName; pending.quality = quality; pending.targetSectionIndex = i; pending.totalNumberOfSections = cNumSections; pending.issuingSourceFile = sourceInputFile; pending.lastModified = DateTime.Now; string jobFilename = networkAOInterface.JobsDir + mHostName + "_" + i + "_" + mCurrJobGUID + ".aojob"; Stream st = File.Open(jobFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None); s.Serialize(st, pending); st.Close(); } string targetDir = networkAOInterface.ResultDir + Dns.GetHostName(); List <string> filesToCheck = new List <string>(); for (int i = 0; i < mNumSectionsToComplete; i++) { filesToCheck.Add(targetDir + "\\" + Dns.GetHostName() + ".AO" + i); } int notProcessedTimer = 10;//in seconds (due to the sleep @ the bottom of this thread while (filesToCheck.Count != 0) { //did we cancel? if (mWorkerThread.CancellationPending) { e.Cancel = true; return; } //if NONE of our work has been picked up then either the clients are all down, or they are all busy //ask the user if they want to keep waiting. if (!areJobsBeingProcessed()) { if (notProcessedTimer <= 0) { DialogResult res = MessageBox.Show( "The issued jobs have not been acquired by the working clients yet. \n " + "This may mean that the clients are down, or otherwise busy with other work. \n" + "Would you like to continue to wait? \n\n " + "Click YES to wait in line for the network. \n\n Click NO to cancel. ", "Think about it..", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (res == DialogResult.Yes) { notProcessedTimer = 10;//in seconds (due to the sleep @ the bottom of this thread } else if (res == DialogResult.No) { e.Cancel = true; return; } } else { notProcessedTimer--; } } else { notProcessedTimer = 60; //reset the timer } for (int i = 0; i < filesToCheck.Count; i++) { if (File.Exists(filesToCheck[i])) { mWorkerThread.ReportProgress(mNumSectionsToComplete - 1 - filesToCheck[i].Length, filesToCheck[i]); filesToCheck.RemoveAt(i); i--; } } Thread.Sleep(1000); } e.Result = mNumSectionsToComplete;// This gets passed to RunWorkerCopmleted }