Ejemplo n.º 1
0
        public void Start()
        {
            try
            {
                _applicationId = Log();

                WorkerCommon workerData = new WorkerCommon(_applicationId);

                Task checkerTask = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        WorkerChecker statusChecker = new WorkerChecker(workerData, _cancelTokenSource.Token);
                        statusChecker.Start();
                    }
                    catch (Exception ex)
                    {
                        ErrorManager.LogError(ex);
                    }
                }, _cancelTokenSource.Token);
                _workers.Add(checkerTask);

                Task schedulerTask = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        WorkerScheduler scheduler = new WorkerScheduler(workerData, _cancelTokenSource.Token);
                        scheduler.Start();
                    }
                    catch (Exception ex)
                    {
                        ErrorManager.LogError(ex);
                    }
                }, _cancelTokenSource.Token);
                _workers.Add(schedulerTask);

                Task starterTask = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        WorkerStarter starter = new WorkerStarter(workerData, _cancelTokenSource.Token);
                        starter.Start();
                    }
                    catch (Exception ex)
                    {
                        ErrorManager.LogError(ex);
                    }
                }, _cancelTokenSource.Token);
                _workers.Add(starterTask);
            }
            catch (Exception ex)
            {
                ErrorManager.LogError(ex);
            }
        }
Ejemplo n.º 2
0
 public WorkerStarter(WorkerCommon workerData, CancellationToken cancelToken)
 {
     _workerData  = workerData;
     _cancelToken = cancelToken;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Создает новую таску
        /// </summary>
        /// <param name="queue"></param>
        private void StartTask(OMQueue queue)
        {
            if (!WorkerCommon.ProcessTypeCache.ContainsKey(queue.ProcessTypeId))
            {
                LogError(queue, "Не найден тип процесса с ИД: " + queue.ProcessTypeId);
                return;
            }

            OMProcessType processType = WorkerCommon.ProcessTypeCache[queue.ProcessTypeId];

            ILongProcess longProcess;

            try
            {
                longProcess = WorkerCommon.GetLongProcessObject(queue.ProcessTypeId);
            }
            catch (Exception ex)
            {
                LogError(queue, ex.Message);
                return;
            }

            if (longProcess == null)
            {
                LogError(queue, "Не удалось создать объект типа: " + processType.ClassName);
                return;
            }

            CancellationTokenSource cancelTokenSource = new CancellationTokenSource();

            queue.StartDate     = DateTime.Now;
            queue.LastCheckDate = DateTime.Now;
            queue.Status_Code   = Status.Running;
            queue.Save();

            Task task = new Task(() =>
            {
                try
                {
                    _workerData.СurrentTasks[queue.Id].CurrentThread = Thread.CurrentThread;

                    // Так как данный процесс запускается в отдельном потоке в него необходимо передать данные об авторизованном пользователе,
                    // иначе работа будет выполняться под учетной записью пула приложений
                    if (queue.UserId > 0 && SRDCache.Users.ContainsKey((int)queue.UserId.Value))
                    {
                        SRDSession.SetThreadCurrentPrincipal(queue.UserId.Value);
                    }

                    queue.CleanPropertyChangedList();

                    longProcess.StartProcess(processType, queue, cancelTokenSource.Token);

                    processType.LastStartDate = queue.StartDate;
                    processType.Save();

                    queue.EndDate       = DateTime.Now;
                    queue.LastCheckDate = DateTime.Now;
                    queue.Status_Code   = Status.Completed;
                    queue.Save();

                    _workerData.СurrentTasks.Remove(queue.Id);
                }
                catch (Exception ex)
                {
                    _workerData.СurrentTasks.Remove(queue.Id);

                    int errorId = ErrorManager.LogError(ex);

                    queue.EndDate     = DateTime.Now;
                    queue.Status_Code = Status.Faulted;
                    queue.ErrorId     = errorId;

                    var message = ex.Message;
                    if (message.Length >= 4000)
                    {
                        message = message.Substring(0, 4000);
                    }

                    queue.Message = message;
                    queue.Save();

                    try
                    {
                        longProcess.LogError(queue.ObjectId, ex, errorId);
                    }
                    catch (Exception) { }
                }
            }, cancelTokenSource.Token);

            TaskData taskData = new TaskData
            {
                Task = task,
                CancellationTokenSource = cancelTokenSource,
                LongProcess             = longProcess,
                ProcessTypeId           = processType.Id
            };

            _workerData.СurrentTasks.Add(queue.Id, taskData);

            taskData.Task.Start();
        }