/// <summary> /// Inserts asynchronous task information to the database /// </summary> /// <returns>ApsParameterValue</returns> public ApsParameterValue InsertData() { // Create list data to json serilize. List <int> listData = new List <int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Sets parameters of ApsParameterValue to insert asynchronous task information. ApsParameterValue parameterValue = new ApsParameterValue( "AsyncProcessingService", "InsertTask", "InsertTask", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); parameterValue.UserId = "A"; parameterValue.ProcessName = "AAA"; parameterValue.Data = JsonConvert.SerializeObject(listData); parameterValue.ExecutionStartDateTime = DateTime.Now; parameterValue.RegistrationDateTime = DateTime.Now; parameterValue.NumberOfRetries = 0; parameterValue.ProgressRate = 0; parameterValue.CompletionDateTime = DateTime.Now; parameterValue.StatusId = (int)(AsyncStatus.Register); parameterValue.CommandId = 0; parameterValue.ReservedArea = "xxxxxx"; ApsLayerB layerB = new ApsLayerB(); ApsReturnValue returnValue = (ApsReturnValue)layerB.DoBusinessLogic( (ApsParameterValue)parameterValue, DbEnum.IsolationLevelEnum.DefaultTransaction); return(parameterValue); }
/// <summary> /// Updates the selected asynchronous task based type of SQL_Update_Method_name /// </summary> /// <param name="selectedAsyncTask">Selected Asynchronous Task</param> /// <param name="updateTask">SQL_Update_Method_name</param> /// <returns></returns> public ApsReturnValue UpdateAsyncTask(ApsReturnValue selectedAsyncTask, AsyncTaskUpdate updateTask, string exceptionInfo = null) { ApsParameterValue asyncParameterValue = new ApsParameterValue( "AsyncProcessingService", updateTask.ToString(), updateTask.ToString(), "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.TaskId = selectedAsyncTask.TaskId; // Update databse based on task switch (updateTask) { case AsyncTaskUpdate.UpdateTaskStart: asyncParameterValue.ExecutionStartDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncStatus.Processing; break; case AsyncTaskUpdate.UpdateTaskRetry: if (exceptionInfo.Length > 512) { exceptionInfo = exceptionInfo.Substring(0, 512); } asyncParameterValue.NumberOfRetries = selectedAsyncTask.NumberOfRetries; asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncStatus.AbnormalEnd; asyncParameterValue.ExceptionInfo = exceptionInfo; break; case AsyncTaskUpdate.UpdateTaskFail: if (exceptionInfo.Length > 512) { exceptionInfo = exceptionInfo.Substring(0, 512); } asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncStatus.Abort; asyncParameterValue.ExceptionInfo = exceptionInfo; break; case AsyncTaskUpdate.UpdateTaskSuccess: asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.ProgressRate = 100; asyncParameterValue.StatusId = (int)AsyncStatus.End; break; } ApsLayerB layerB = new ApsLayerB(); ApsReturnValue asyncReturnValue = (ApsReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); return(asyncReturnValue); }
/// <summary> /// Sets Asynchronous Processing Service exception flag by checking prerequisites /// </summary> public void SetServiceException() { try { // Checks for successful connection to the database. ApsParameterValue asyncParameterValue = new ApsParameterValue( "AsyncProcessingService", "TestConnection", "TestConnection", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); ApsLayerB layerB = new ApsLayerB(); layerB.DoBusinessLogic((BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.NoTransaction); // Asynchronous Processing Service initializes successfully without any errors this._isServiceException = false; } catch (Exception ex) { // Service Failed due to unexpected exception. this._isServiceException = true; LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0000"), ex.Message.ToString())); } }
/// <summary> /// Stop the asynchronous service and Waits to complete all worker thread to complete. /// </summary> public void StopAsyncProcess() { // Breaks the infinite loop of Main thread. this._infiniteLoop = false; // Wait the end of the main thread. this._mainThread.Join(); // Update stop command to all the running asynchronous task ApsParameterValue asyncParameterValue = new ApsParameterValue( "AsyncProcessingService", "StopAllTask", "StopAllTask", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.StatusId = (int)AsyncStatus.Processing; asyncParameterValue.CommandId = (int)AsyncCommand.Stop; ApsLayerB layerB = new ApsLayerB(); ApsReturnValue asyncReturnValue = (ApsReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); if (asyncReturnValue.ErrorFlag) { LogIF.ErrorLog("ASYNC-SERVICE", "ErrorMessageID: " + asyncReturnValue.ErrorMessageID + "ErrorMessage: " + asyncReturnValue.ErrorMessage); } // Wait for all worker thread to be complete LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0004")); while (this._workerThreadCount != 0) { // Wait for the completion of the worker thread. Thread.Sleep(this._numberOfSeconds * 1000); } // Log after completing all worker threads LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0008")); }
/// <summary> /// Maintains the Main thread of the Asynchronous Service /// </summary> public void MainThreadInvoke() { // Asynchronous service is started LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0001")); // Infinte loop processing for selecting register task. while (this._infiniteLoop) { try { // Get asynchronous task from the database. ApsParameterValue asyncParameterValue = new ApsParameterValue( "AsyncProcessingService", "SelectTask", "SelectTask", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.RegistrationDateTime = DateTime.Now - new TimeSpan(_maxNumberOfHours, 0, 0); asyncParameterValue.NumberOfRetries = this._maxNumberOfRetries; asyncParameterValue.CompletionDateTime = DateTime.Now - new TimeSpan(_maxNumberOfHours, 0, 0); ApsLayerB layerB = new ApsLayerB(); ApsReturnValue selectedAsyncTask = (ApsReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); // Existence check of asynchronous task if (string.IsNullOrEmpty(selectedAsyncTask.UserId)) { // Asynchronous task does not exist. // Wait for the asynchronous task to be registered in the database. Thread.Sleep(this._numberOfSeconds * 1000); // Continue to this infinite loop. continue; } // Asynchronous task exists. // Check the number of free worker threads. int freeWorkerThreads = 0; int completionPortThreads = 0; // Gets the available threads. ThreadPool.GetAvailableThreads(out freeWorkerThreads, out completionPortThreads); while (freeWorkerThreads == 0) { // Wait for the completion of the worker thread. Thread.Sleep(this._numberOfSeconds * 1000); // Get available threads. ThreadPool.GetAvailableThreads(out freeWorkerThreads, out completionPortThreads); } // Selected asynchronous task is assigned to a worker thread this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.UpdateTaskStart); LogIF.InfoLog("ASYNC-SERVICE", string.Format( GetMessage.GetMessageDescription("I0002"), selectedAsyncTask.TaskId)); // Assign the task to the worker thread ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkerThreadCallBack), (object)selectedAsyncTask); } catch (Exception ex) { // Service Failed due to unexpected exception. this._isServiceException = true; this._infiniteLoop = false; LogIF.ErrorLog("ASYNC-SERVICE", string.Format( GetMessage.GetMessageDescription("E0000"), ex.Message.ToString())); } } }