예제 #1
0
        /// <summary>
        /// Inserts asynchronous task information to the database
        /// </summary>
        /// <returns></returns>
        public AsyncProcessingServiceParameterValue InsertData()
        {
            // Create array data to serilize.
            byte[] arrayData = { 1, 2, 3, 4, 5 };

            // Sets parameters of AsyncProcessingServiceParameterValue to insert asynchronous task information.
            AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "InsertTask", "InsertTask", "SQL",
                                                                                                                new MyUserInfo("AsyncProcessingService", "AsyncProcessingService"));

            asyncParameterValue.UserId                 = "A";
            asyncParameterValue.ProcessName            = "AAA";
            asyncParameterValue.Data                   = AsyncSvc_sample.LayerB.SerializeToBase64String(arrayData);
            asyncParameterValue.ExecutionStartDateTime = DateTime.Now;
            asyncParameterValue.RegistrationDateTime   = DateTime.Now;
            asyncParameterValue.NumberOfRetries        = 0;
            asyncParameterValue.ProgressRate           = 0;
            asyncParameterValue.CompletionDateTime     = DateTime.Now;
            asyncParameterValue.StatusId               = (int)(AsyncProcessingServiceParameterValue.AsyncStatus.Register);
            asyncParameterValue.CommandId              = 0;
            asyncParameterValue.ReservedArea           = "xxxxxx";

            DbEnum.IsolationLevelEnum         iso = DbEnum.IsolationLevelEnum.DefaultTransaction;
            AsyncProcessingServiceReturnValue asyncReturnValue;

            // Execute do business logic method.
            AsyncProcessingService.LayerB layerB = new AsyncProcessingService.LayerB();
            asyncReturnValue = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic((AsyncProcessingServiceParameterValue)asyncParameterValue, iso);
            return(asyncParameterValue);
        }
        /// <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
            AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "StopAllTask", "StopAllTask", "SQL",
                                                                                    new MyUserInfo("AsyncProcessingService", "AsyncProcessingService"));
            asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Processing;
            asyncParameterValue.CommandId = (int)AsyncProcessingServiceParameterValue.AsyncCommand.Stop;
            LayerB layerB = new LayerB();
            AsyncProcessingServiceReturnValue asyncReturnValue = (AsyncProcessingServiceReturnValue)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>
        ///  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 AsyncProcessingServiceReturnValue UpdateAsyncTask(AsyncProcessingServiceReturnValue selectedAsyncTask, string updateTask, string exceptionInfo = null)
        {
            AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", updateTask, updateTask, "SQL",
                                                                                        new MyUserInfo("AsyncProcessingService", "AsyncProcessingService"));
            asyncParameterValue.TaskId = selectedAsyncTask.TaskId;

            // Update databse based on task
            switch (updateTask)
            {
                case AsyncTaskUpdate.START:
                    asyncParameterValue.ExecutionStartDateTime = DateTime.Now;
                    asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Processing;
                    break;
                case AsyncTaskUpdate.RETRY:
                    if (exceptionInfo.Length > 512)
                    {
                        exceptionInfo = exceptionInfo.Substring(0, 512);
                    }
                    asyncParameterValue.NumberOfRetries = selectedAsyncTask.NumberOfRetries;
                    asyncParameterValue.CompletionDateTime = DateTime.Now;
                    asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.AbnormalEnd;
                    asyncParameterValue.ExceptionInfo = exceptionInfo;
                    break;
                case AsyncTaskUpdate.FAIL:
                    if (exceptionInfo.Length > 512)
                    {
                        exceptionInfo = exceptionInfo.Substring(0, 512);
                    }
                    asyncParameterValue.CompletionDateTime = DateTime.Now;
                    asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Abort;
                    asyncParameterValue.ExceptionInfo = exceptionInfo;
                    break;
                case AsyncTaskUpdate.SUCCESS:
                    asyncParameterValue.CompletionDateTime = DateTime.Now;
                    asyncParameterValue.ProgressRate = 100;
                    asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.End;
                    break;
            }
            LayerB layerB = new LayerB();
            AsyncProcessingServiceReturnValue asyncReturnValue = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic(
                                                                      (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted);
            return asyncReturnValue;
        }
        /// <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.
                    AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("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);
                    LayerB layerB = new LayerB();
                    AsyncProcessingServiceReturnValue selectedAsyncTask = (AsyncProcessingServiceReturnValue)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.START);
                    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._infiniteLoop = false;
                    LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0000"), ex.Message.ToString()));
                }
            }
        }