protected QUITCODE loginToPerforce() { try { logJob("Perfoce login"); QUITCODE retValue = QUITCODE.NONE; string exeFile = Globals.Config.getString(Globals.Config.cPerforceLoginBatchFile) + " " + Globals.Config.getString(Globals.Config.cPerforceUser) + " " + Globals.Config.getString(Globals.Config.cPerforceClient); Task task = runBatch(exeFile); retValue = task.run(); // If error we must write to the db. if (retValue == QUITCODE.ERROR) { logJob("ERROR: Error login in to Perforce. Unable to renew ticket"); setRunStatus(QUITCODE.ERROR); } return(retValue); } catch (Exception exp) { logJob("ERROR: Exception while login in to Perforce: " + exp.Message); } return(QUITCODE.ERROR); }
override public QUITCODE run() { QUITCODE retValue = QUITCODE.NONE; retValue = base.run(); if (retValue != QUITCODE.NONE) { return(retValue); } // Run batch command Task buildBatchCommand = runBatch(mBatchCommand); retValue = buildBatchCommand.run(); // If error we must write to the db. if (retValue == QUITCODE.ERROR) { setRunStatus(QUITCODE.ERROR); } if (mJobLog != null) { mJobLog.Close(); } return(retValue); }
virtual public QUITCODE run() { QUITCODE bSuccess = QUITCODE.NONE; logTask("Begin task " + mName); try { logTask(" - run " + mBatFilename); myProcess = new Process(); myProcess.StartInfo.FileName = mBatFilename; // Set working directory if executable name not relative if (Path.IsPathRooted(mBatFilename)) { myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(mBatFilename); } foreach (string arg in mArgs) { myProcess.StartInfo.Arguments = myProcess.StartInfo.Arguments + " " + arg; } #if false myProcess.StartInfo.UseShellExecute = true; #else myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardError = true; // Set our event handler to asynchronously read the sort output. myProcess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); #endif bSuccess = startProcess(myProcess); } catch (Exception exp) { logTask("ERROR: " + exp.Message + " " + mBatFilename); bSuccess = QUITCODE.ERROR; } return(bSuccess); }
virtual public QUITCODE run() { QUITCODE retValue = QUITCODE.NONE; logJob("starting job " + mName); // Renew perforce ticket just in case it has expired retValue = loginToPerforce(); if (retValue != QUITCODE.NONE) { return(retValue); } return(retValue); }
protected QUITCODE getQuitCode() { QUITCODE quitCode = QUITCODE.NONE; //-- Check DB for quit. try { string query = "SELECT * FROM " + Globals.Config.getString(Globals.Config.cDBQueueTableName) + " WHERE running > '1'"; SqlDataReader rs = Database.exSQL(query, false); if (rs.Read() == false) { quitCode = QUITCODE.NONE; } else { string quit = (string)rs["running"]; quit.Trim(); int qt = System.Convert.ToInt32(quit); if (qt == 2) { quitCode = QUITCODE.USERQUIT; } else if (qt == 3) { quitCode = QUITCODE.ERROR; } else { quitCode = QUITCODE.NONE; } logJob("QuitCode: " + quitCode.ToString()); } rs.Close(); Database.DBConnection.Close(); } catch (Exception exp) { Console.WriteLine("CheckForQuit: error hitting the DB: " + exp.Message); } return(quitCode); }
protected void setRunStatus(QUITCODE quitCode) { try { logJob("Setting run status"); string query = "SELECT * From " + Globals.Config.getString(Globals.Config.cDBQueueTableName) + " WHERE running = '1'"; SqlDataReader rs = Database.exSQL(query, false); if (rs.HasRows == true) { rs.Read(); int queuenum = (int)rs["queuenum"]; query = "UPDATE " + Globals.Config.getString(Globals.Config.cDBQueueTableName) + " SET running = '" + (int)quitCode + "', status = 'running' WHERE queuenum = '" + queuenum + "'"; rs.Close(); Database.exSQL(query, true); } } catch (Exception exp) { logJob("ERROR: setrunstatus: " + exp.Message); } }
private void runJob() { BuildQueueItem currentJobInfo = new BuildQueueItem(); string results = "No errors."; bool bUserHasQuit = false; bool bSuccess = false; logServer_Async("---------------------------"); logServer_Async("Begin runJob #" + mCurrentJobNum); //-- Get job info // bSuccess = Database.getJobFromQueue(mCurrentJobNum, ref currentJobInfo); if (bSuccess) { //-- Get build type for current config BuildTypeItem builtTypeInfo = new BuildTypeItem(); bSuccess = Database.getBuildTypeFromTypes(currentJobInfo.buildconfig, ref builtTypeInfo); if (bSuccess) { Job job = null; switch (builtTypeInfo.type) { case "RunBatchBuild": job = new JobRunBatchBuild(builtTypeInfo.extra); break; default: break; } if (job != null) { // init job job.init(builtTypeInfo.name); logServer_Async("Launching job..."); logServer_Async(String.Format("[started by: {0}, job type: {1}]", currentJobInfo.builduser, builtTypeInfo.name)); // run the job QUITCODE quitCode = job.run(); logServer_Async("Job returned (exitcode: " + quitCode.ToString() + ")"); bUserHasQuit = (quitCode == QUITCODE.USERQUIT); string userName = currentJobInfo.builduser; string fullBuildNumber = getBuildNumber(/*getBuildVersion(false)*/ currentJobInfo.queuenum.ToString(), currentJobInfo.buildconfig); switch (quitCode) { case QUITCODE.NONE: { //-- prop logs to share logServer_Async("Copying logs..."); string logsSrc = Globals.Config.getString(Globals.Config.cLogRoot); string logsDest = Globals.Config.getString(Globals.Config.cLogDest) + fullBuildNumber; bSuccess = propLogs(logsSrc, logsDest); if (!bSuccess) { string errorMsg = "FAILURE: Unable to copy logs"; logServer_Async(errorMsg); results = errorMsg; break; } string buildCount = null; string changeListNumber = null; string emailSubjectLine = null; //-- this is a hack here. //-- For archive builds retreive the build number number from the file work\tools\databuild\buildcount.txt and // the changelist number from work\tools\databuild\changeListSubmission.txt if ((currentJobInfo.buildconfig == 0) || (currentJobInfo.buildconfig == 18) || (currentJobInfo.buildconfig == 27) || (currentJobInfo.buildconfig == 28)) { string archiveBuildPath = Path.GetDirectoryName(builtTypeInfo.extra); if (File.Exists(archiveBuildPath + "\\buildcount.txt") == true) { StreamReader rdr = File.OpenText(archiveBuildPath + "\\buildcount.txt"); buildCount = rdr.ReadToEnd(); buildCount = buildCount.Trim(); rdr.Close(); } if (File.Exists(archiveBuildPath + "\\changeListSubmission.txt") == true) { StreamReader rdr = File.OpenText(archiveBuildPath + "\\changeListSubmission.txt"); Regex changeList1EX = new Regex(@"^Submitting change (?<changelistnumber>\d*)\.$", RegexOptions.Singleline); String line; while ((line = rdr.ReadLine()) != null) { MatchCollection matches = changeList1EX.Matches(line); foreach (Match m in matches) { if (m.Success == true) { changeListNumber = m.Groups["changelistnumber"].Value; break; } } } rdr.Close(); } } //-- send email logServer_Async("Sending email..."); bSuccess = buildSuccessMail(currentJobInfo.comment, logsDest); if (!bSuccess) { string errorMsg = "FAILURE: Unable to create build success mail"; logServer_Async(errorMsg); results = errorMsg; break; } if (String.IsNullOrEmpty(buildCount)) { emailSubjectLine = mMailPrependString + "New " + builtTypeInfo.name; } else { if (String.IsNullOrEmpty(changeListNumber)) { emailSubjectLine = mMailPrependString + "New " + builtTypeInfo.name + " (#" + buildCount + ")"; } else { emailSubjectLine = mMailPrependString + "New " + builtTypeInfo.name + " (#" + buildCount + ")" + " (#" + changeListNumber + ")"; } } bSuccess = sendMail(Globals.Config.getString(Globals.Config.cBuildAlias), Globals.Config.getString(Globals.Config.cMailTemplateReport), emailSubjectLine, userName); if (!bSuccess) { string errorMsg = "FAILURE: Unable to send build success mail"; logServer_Async(errorMsg); results = errorMsg; } } break; case QUITCODE.USERQUIT: // TODO: send build user a msg that their build got canned. break; case QUITCODE.ERROR: { //-- prop logs to share logServer_Async("Copying logs..."); string logsSrc = Globals.Config.getString(Globals.Config.cLogRoot); string logsDest = Globals.Config.getString(Globals.Config.cLogDest) + fullBuildNumber; bSuccess = propLogs(logsSrc, logsDest); if (!bSuccess) { string errorMsg = "FAILURE: Unable to copy logs"; logServer_Async(errorMsg); results = errorMsg; break; } //-- send email logServer_Async("Sending email..."); // Get errors StreamReader rdr = File.OpenText(job.LogFileName); results = rdr.ReadToEnd(); rdr.Close(); // Only use last 20 lines results = cropToLastNumLines(20, results); bSuccess = buildErrorMail(currentJobInfo.comment, logsDest, results); if (!bSuccess) { string errorMsg = "FAILURE: Unable to create build failure mail"; logServer_Async(errorMsg); results = errorMsg; break; } bSuccess = sendMail(Globals.Config.getString(Globals.Config.cFailedBuildAlias), Globals.Config.getString(Globals.Config.cMailTemplateReport), mMailPrependString + "FAILED: " + builtTypeInfo.name + ", from: " + userName, userName); if (!bSuccess) { string errorMsg = "FAILURE: Unable to send build failure mail"; logServer_Async(errorMsg); results = errorMsg; } } break; } job.End(); } else { string errorMsg = "FAILURE: Build type \"" + builtTypeInfo.type + "\" is not recognized by the build system"; logServer_Async(errorMsg); results = errorMsg; } } else { string errorMsg = "FAILURE: Invalid build configuration id (buildconfig = " + currentJobInfo.buildconfig + ") not in " + Globals.Config.getString(Globals.Config.cDBBuildTypes) + " table"; logServer_Async(errorMsg); results = errorMsg; } } else { string errorMsg = "FAILURE: Invalid job number (queuenum = " + mCurrentJobNum + ") not in " + Globals.Config.getString(Globals.Config.cDBQueueTableName) + " table"; logServer_Async(errorMsg); results = errorMsg; } //-- Enter the history if the user didn't quit. if (!bUserHasQuit) { try// DBHistory contains everything we will insert into the history field { string vVersion = /*job.getBuildVersion(false) + */ "\\"; string newPath = Globals.Config.getString(Globals.Config.cLogDest) + vVersion; if (File.Exists(newPath + Globals.Config.getString(Globals.Config.cHistoryPlaytest)) == true) { StreamReader rdr = File.OpenText(newPath + Globals.Config.getString(Globals.Config.cHistoryPlaytest)); results = rdr.ReadToEnd() + "\r\n"; } if (File.Exists(newPath + Globals.Config.getString(Globals.Config.cHistoryDebug)) == true) { StreamReader rdr = File.OpenText(newPath + Globals.Config.getString(Globals.Config.cHistoryDebug)); results = results + rdr.ReadToEnd() + "\r\n"; } if (File.Exists(newPath + Globals.Config.getString(Globals.Config.cHistoryFinal)) == true) { StreamReader rdr = File.OpenText(newPath + Globals.Config.getString(Globals.Config.cHistoryFinal)); results = results + rdr.ReadToEnd() + "\r\n"; } } catch (Exception exp) { logServer_Async("ERROR: Failed calling OpenText:" + exp.Message); } //-- Adding to history entry logServer_Async("Adding entry to history table..."); string date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); try // InsertHistory can fail for a bunch of reasons, so we catch and insert some default comments { currentJobInfo.buildnum = currentJobInfo.queuenum.ToString(); Database.insertHistory(currentJobInfo, currentJobInfo.comment, results, date); } catch (Exception exp) { logServer_Async("ERROR: Failed to call insertHistory, possibly too long of a string, will insert default comments:" + exp.Message); try { Database.insertHistory(currentJobInfo, "Failed to add comments", null, date); } catch (Exception exp1) { logServer_Async("ERROR: Failed to call InsertHistory a second time, comments will be null:" + exp1.Message); } } } try // Remove job from queue { // Remove job from build queue table Database.removeJobFromQueue(mCurrentJobNum); } catch (Exception exp1) { logServer_Async("ERROR: Failed to call to remove job from the BuildQueue table" + exp1.Message); } logServer_Async("End job"); logServer_Async("---------------------------"); mCurrentJobNum = -1; }
protected QUITCODE startProcess(Process myProcess) { QUITCODE bSuccess = QUITCODE.NONE; try { // Create a timer that waits one second, then invokes every second. if (mEnableQuitTimer) { // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(CheckForQuit); mQuitTimer = new Timer(timerDelegate, this, 0, 1000); } myProcess.Start(); // Start the asynchronous read of the output stream. myProcess.BeginOutputReadLine(); while (myProcess.HasExited == false && mUserHasQuit == false) { Thread.Sleep(1); } if (myProcess.HasExited) { myProcess.WaitForExit(); if (mUseExitCodes) { if (myProcess.ExitCode != 0) { bSuccess = QUITCODE.ERROR; } } } else if (mUserHasQuit == true) { logTask(" - user quit the task " + mName); if (myProcess.HasExited == false) { logTask(" - killing task " + mName); myProcess.Kill(); myProcess.WaitForExit(); //-- Also kill any process that the task specifies. foreach (string processName in mProcessKillList) { Process[] myProcesses; myProcesses = Process.GetProcessesByName(processName); foreach (Process proc in myProcesses) { proc.Kill(); } } } bSuccess = QUITCODE.USERQUIT; } myProcess.Close(); // Kill the timer if it has been enabled if (mQuitTimer != null) { mQuitTimer.Dispose(); mQuitTimer = null; } } catch (Exception exp) { logTask("ERROR: (startProcess) " + exp.Message); bSuccess = QUITCODE.ERROR; } string successString = "SUCCESS"; switch (bSuccess) { case QUITCODE.NONE: successString = "SUCCESS"; break; case QUITCODE.ERROR: successString = "ERROR"; break; case QUITCODE.USERQUIT: successString = "USER QUIT"; break; } logTask(" - task has ended. (" + mName + ")" + " (returned: " + successString + ")"); return(bSuccess); }