/// <summary> /// Called as a thread completes its designated operation. We need to remove the thread from /// the thread pool and exit the thread /// </summary> /// <param name="operationThread"></param> protected void OperationComplete(OperationThreadData operationThread) { LogFile ourLog = LogFile.Instance; ourLog.Write("OperationComplete Start", true); try { // Update the operation in the database Operation operation = operationThread.ActiveOperation; operation.EndDate = DateTime.Now; operation.Update(); // Remove this entry from the thread pool _threadPool.Remove(operationThread); // If the operation was successful and it affected the AuditAgent status then ensure that // we now update the status if ((operation.Status == Operation.STATUS.complete_success) && (operation.OperationType != Operation.OPERATION.checkstatus) && (operation.OperationType != Operation.OPERATION.clearagentlog)) { string message; Asset.AGENTSTATUS assetStatus; UpdateCurrentAgentStatus(operation.AssetName, operation.AssetID, out assetStatus, out message); } } catch (Exception) { } ourLog.Write("OperationComplete End", true); return; }
/// <summary> /// Called to perform a 'Deploy Agent' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> protected bool DeployAgent(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { // This is the status of the operation bool success = true; // This is the text which we shall display for the operation string message = "Success"; // Get the operation to be performed Operation operation = operationThread.ActiveOperation; try { // Get the Agent Service Controller passing it the name of the computer AuditAgentServiceController agentServiceController = new AuditAgentServiceController(operation.AssetName); // Use it to get the current status of the asset // If already started, flag this if (assetStatus == Asset.AGENTSTATUS.Running) { message = "The AuditWizard Agent Service is already running"; } else { // If the agent has not been deployed to this computer previously then do so now if (assetStatus == Asset.AGENTSTATUS.notdeployed) { // Install the Agent Service (throws an exception on error) agentServiceController.Install(); assetStatus = Asset.AGENTSTATUS.deployed; AssetDAO lwDataAccess = new AssetDAO(); lwDataAccess.AssetUpdateAssetStatus(operation.AssetID, assetStatus); } // If the status is now deployed then we can start the Agent if (assetStatus == Asset.AGENTSTATUS.deployed) { // Start the agent (throws an exception on error) agentServiceController.Start(); } } } catch (Exception e) { success = false; message = "Error: An Exception occurred while Deploying the AuditAgent, the error text was " + e.Message; } // Update the Operation object with the overall status and completion message operation.Status = (success) ? Operation.STATUS.complete_success : Operation.STATUS.complete_error; operation.ErrorText = message; // Return now return(success); }
/// <summary> /// Called as the operation thread exits - this is in the context of the main thread though so no /// threading issues /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void operationThread_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { LogFile ourLog = LogFile.Instance; ourLog.Write("operationThread_WorkerCompleted Start", true); OperationThreadData operationThreadData = e.Result as OperationThreadData; OperationComplete(operationThreadData); ourLog.Write("operationThread_WorkerCompleted Start", true); }
/// <summary> /// Called to perform a 'Remove Agent' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> protected bool RemoveAgent(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { // This is the status of the operation bool success = true; // This is the text which we shall display for the operation string message = "Success"; // Get the operation to be performed Operation operation = operationThread.ActiveOperation; // If the AuditAgent is not deployed then just log this - it isn't an error as such but there is // no point removing the agent if it is not deployed if (assetStatus == Asset.AGENTSTATUS.notdeployed) { message = "The AuditAgent was not deployed"; } else { try { // Get the Agent Service Controller passing it the name of the computer AuditAgentServiceController agentServiceController = new AuditAgentServiceController(operation.AssetName); // If the AuditAgent is RUNNING then we need to stop it before we can remove it if (assetStatus == Asset.AGENTSTATUS.Running) { success = StopAgent(operationThread, assetStatus); Thread.Sleep(2000); } // Now remove the AudutAgent if (success) { // Use it to get the current status of the asset success = agentServiceController.Remove(); message = (success) ? "Success" : "An error occurred while removing the AuditAgent Service"; } } catch (Exception e) { success = false; message = "Error: An Exception occurred while removing the AuditAgent Service, the error text was " + e.Message; } } // Update the Operation object with the overall status and completion message operation.Status = (success) ? Operation.STATUS.complete_success : Operation.STATUS.complete_error; operation.ErrorText = message; // Return now return(success); }
/// <summary> /// Process the current outstanding list of operations /// </summary> protected bool ProcessOperations() { // If no more operations simply return false now if (_listOutstandingOperations.Count == 0) { return(false); } // Create a log file LogFile ourLog = LogFile.Instance; // We use a separate thread for each upto a maximum of 10 threads // If we have exceeded the thread limit then we need to wait for one to become availab;e if (_threadPool.Count >= MAX_THREADS) { return(false); } // We have a spare entry in the Thread pool - allocate it and pass the operation to the thread to perform OperationThreadData operationThreadData = new OperationThreadData(); operationThreadData.ActiveOperation = _listOutstandingOperations[0]; operationThreadData.ActiveOperation.Status = Operation.STATUS.pending; ourLog.Write("Processing Operation for " + operationThreadData.ActiveOperation.AssetName, true); // remove the operation from the list now that we have allocated it to a thread _listOutstandingOperations.RemoveAt(0); // Add the OperationThreadData to the thread pool _threadPool.Add(operationThreadData); // Mark the operation as 'Pending' in the database OperationsDAO lwDataAccess = new OperationsDAO(); lwDataAccess.OperationUpdate(operationThreadData.ActiveOperation); // Create the actual Windows Thread and start it passing the OperationThreadData object as a parameter BackgroundWorker operationThread = new System.ComponentModel.BackgroundWorker(); operationThread.WorkerReportsProgress = false; operationThread.DoWork += new System.ComponentModel.DoWorkEventHandler(operationThread_DoWork); operationThread.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(operationThread_WorkerCompleted); operationThread.RunWorkerAsync(operationThreadData); //Thread thread = new Thread(new ParameterizedThreadStart(PerformOperation)); //thread //thread.Start(operationThread); return(true); }
/// <summary> /// Called to perform a 'Stop Agent' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> protected bool StopAgent(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { // This is the status of the operation bool success = true; // This is the text which we shall display for the operation string message = "Success"; // Get the operation to be performed Operation operation = operationThread.ActiveOperation; // To STOP the agent it must be deployed, if not then this is an error. if (assetStatus == Asset.AGENTSTATUS.notdeployed) { success = false; message = "The AuditAgent has not been deployed"; } // Already STOPPED is not an error but don't try and start it again else if (assetStatus == Asset.AGENTSTATUS.deployed) { message = "The AuditAgent was already stopped"; } else { try { // Get the Agent Service Controller passing it the name of the computer AuditAgentServiceController agentServiceController = new AuditAgentServiceController(operation.AssetName); // Use it to get the current status of the asset agentServiceController.Stop(); } catch (Exception e) { success = false; message = "Error: An Exception occurred while stopping the AuditAgent, the error text was " + e.Message; } } // Update the Operation object with the overall status and completion message operation.Status = (success) ? Operation.STATUS.complete_success : Operation.STATUS.complete_error; operation.ErrorText = message; // Return now return(success); }
/// <summary> /// Called to perform a 'Reaudit' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> protected bool Reaudit(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { // This is the status of the operation bool success = true; // This is the text which we shall display for the operation string message = "Success"; // Get the operation to be performed Operation operation = operationThread.ActiveOperation; // If the AuditAgent is not active then we fail to do a re-audit as it must be running if (assetStatus != Asset.AGENTSTATUS.Running) { success = false; message = "The AuditAgent is not currently active"; } else { try { // Get the Agent Service Controller passing it the name of the computer AuditAgentServiceController agentServiceController = new AuditAgentServiceController(operation.AssetName); // Use it to request a re-audit of the asset success = (agentServiceController.RequestReaudit() == 0); if (!success) { message = "An error occurred while requesting a re-audit"; } } catch (Exception e) { success = false; message = "Error: An Exception occurred while requesting a re-audit, the error text was " + e.Message; } } // Update the Operation object with the overall status and completion message operation.Status = (success) ? Operation.STATUS.complete_success : Operation.STATUS.complete_error; operation.ErrorText = message; // Return now return(success); }
/// <summary> /// Called to perform a 'Check Status' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> /// <param name="assetStatus"></param> protected bool CheckStatus(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { LogFile ourLog = LogFile.Instance; // This is the text which we shall display for the operation string message = "Success, AgentStatus is " + Asset.TranslateDeploymentStatus(assetStatus); // We have actually already performed the sttaus check so need not do it again! // Get the operation to be performed Operation operation = operationThread.ActiveOperation; operation.Status = Operation.STATUS.complete_success; operation.ErrorText = message; // Return now return(true); }
/// <summary> /// Called to perform a 'Clear Agent Log' Operation on the specified PC /// </summary> /// <param name="operationThread"></param> protected bool ClearAgentLog(OperationThreadData operationThread, Asset.AGENTSTATUS assetStatus) { // This is the status of the operation bool success = true; // This is the text which we shall display for the operation string message = "Success"; // Get the operation to be performed Operation operation = operationThread.ActiveOperation; try { // Get the Agent Service Controller passing it the name of the computer AuditAgentServiceController agentServiceController = new AuditAgentServiceController(operation.AssetName); // Use it to get the current status of the asset success = agentServiceController.ClearLogFile(); if (!success) { message = "An error occurred while clearing the AuditAgent log file"; } } catch (Exception e) { success = false; message = "Error: An Exception occurred while clearing the AuditAgent log file, the error text was " + e.Message; } // Update the Operation object with the overall status and completion message operation.Status = (success) ? Operation.STATUS.complete_success : Operation.STATUS.complete_error; operation.ErrorText = message; // Return now return(success); }
/// <summary> /// This function is called from within the Operations Thread to perform the desired operation /// </summary> /// <param name="operationThread"></param> private void operationThread_DoWork(object sender, DoWorkEventArgs e) { // Recover the OperationThreadData and Operation objects from that passed in to us OperationThreadData operationThreadData = e.Argument as OperationThreadData; Operation operation = operationThreadData.ActiveOperation; // Before we can perform any operation we need to ensure that we have the latest status for // any AuditAgent on the target computer - If we fail to get the status then we can go no further Asset.AGENTSTATUS assetStatus; string message = "Success"; if (UpdateCurrentAgentStatus(operation.AssetName, operation.AssetID, out assetStatus, out message) != 0) { operation.Status = Operation.STATUS.complete_error; operation.ErrorText = message; } else { // Switch to handle the Operation itself switch ((int)operation.OperationType) { case (int)Operation.OPERATION.checkstatus: CheckStatus(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.clearagentlog: ClearAgentLog(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.deployagent: DeployAgent(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.reaudit: Reaudit(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.removeagent: RemoveAgent(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.startagent: StartAgent(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.stopagent: StopAgent(operationThreadData, assetStatus); break; case (int)Operation.OPERATION.updateconfiguration: UpdateAgentConfiguration(operationThreadData, assetStatus); break; default: break; } } // Show that the thread is complete - we pause just a little to give the operation a little time to complete Thread.Sleep(1000); // and exit e.Result = operationThreadData; return; }