public void DequeueId(long chatId)
        {
            var senderIndex = QueueList.IndexOf(QueueList.First(item => item.ChatId == chatId));

            QueueList.RemoveAt(senderIndex);
            AlertSubscribers(NotificationEventType.OrderChange, senderIndex);
        }
        public void AddMoreTime(long chatId, int timeMinutes)
        {
            var tempIndex = QueueList.IndexOf(QueueList.FirstOrDefault(x => x.ChatId == chatId));

            if (tempIndex == 0)
            {
                QueueList[tempIndex].TimeMinutes += timeMinutes;
            }
            else
            {
                QueueList[tempIndex].TimeMinutes += timeMinutes;
            }
            var senderIndex = QueueList.IndexOf(QueueList.First(item => item.ChatId == chatId));

            AlertSubscribers(NotificationEventType.TimeChange, senderIndex);
        }
Exemple #3
0
        /// <summary>
        /// The main function of the worker, creates the Task instance and run it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //Task runningTask = e.Argument as Task;
            TaskRunning = true;
            var failed = false;

            while (QueueList.Count != 0)
            {
                var runningTask = QueueList.First();
                RunningTask = runningTask;
                var startTime = DateTime.Now;
                try
                {
                    (sender as BackgroundWorker).ReportProgress(0, new KeyValuePair <Task, string>(runningTask, ""));
                    var taskGenerator =
                        Activator.CreateInstance(Type.GetType(runningTask.GeneratorType)) as GeneratorBase;
                    taskGenerator.Log    += Add_Message;
                    taskGenerator.Result += Add_Result;
                    taskGenerator.Props  += Add_Prop;
                    taskGenerator.StartGenerator();
                    (sender as BackgroundWorker).ReportProgress(1, new KeyValuePair <Task, string>(runningTask, ""));
                }
                catch (Exception ex)
                {
                    var messageBuilder = new StringBuilder();
#if DEBUG
                    // In debug mode shows everything
                    if (ex.InnerException != null)
                    {
                        messageBuilder.Append("INNER ERROR MESSAGE:\r\n-------------------------------\r\n\r\n");
                        messageBuilder.Append(ex.InnerException.Message);
                        messageBuilder.Append("\r\n\r\nINNER STACK TRACE:\r\n-------------------------------\r\n\r\n");
                        messageBuilder.Append(ex.InnerException.StackTrace);
                        messageBuilder.Append("\r\n\r\n");
                    }
                    messageBuilder.Append("ERROR MESSAGE:\r\n-------------------------------\r\n\r\n");
                    messageBuilder.Append(ex.Message);
                    messageBuilder.Append("\r\n\r\nSTACK TRACE:\r\n-------------------------------\r\n\r\n");
                    messageBuilder.Append(ex.StackTrace);
#else
                    // In release just shows the exception message
                    messageBuilder.Append(ex.Message);
#endif
                    (sender as BackgroundWorker).ReportProgress(-1,
                                                                new KeyValuePair <Task, string>(runningTask, messageBuilder.ToString()));
                    failed = true;
                }
                finally
                {
                    _lastResult = failed ? TaskResult.Fail : TaskResult.Success;
                    var endTime        = DateTime.Now;
                    var finishedTaskId = QueueList[0].Id;
                    e.Result = new Run
                    {
                        Date     = DateTime.Now,
                        Duration = (int)(endTime - startTime).TotalSeconds,
                        Result   = _lastResult,
                        TaskId   = finishedTaskId,
                        UserId   = CurrentUser.Id
                    };
                    PrintMessage(String.Format("[{0}] {1} in {2:mm\\mss\\sff}", QueueList[0].Name, _lastResult, (endTime - startTime)));

                    // If scheduled task see if failed
                    // so it need to be reschedule X minutes later Y times
                    if (CurrentlyScheduledTasks.ContainsKey(finishedTaskId))
                    {
                        if (_lastResult == TaskResult.Success)
                        {
                            CurrentlyScheduledTasks.Remove(finishedTaskId);
                            PrintMessage(String.Format("[Scheduler] {0} ran successfully, no retries are scheduled.", QueueList[0].Name));
                        }
                        else
                        {
                            CurrentlyScheduledTasks[finishedTaskId].Retry -= 1;
                            if (CurrentlyScheduledTasks[finishedTaskId].Retry == 0)
                            {
                                CurrentlyScheduledTasks.Remove(finishedTaskId);
                                PrintMessage(String.Format("[Scheduler] {0} task failed, no re-run will be attempted.", QueueList[0].Name), Logger.LogType.Warning);
                            }
                            else
                            {
                                CurrentlyScheduledTasks[finishedTaskId].ScheduleTime =
                                    CurrentlyScheduledTasks[finishedTaskId].ScheduleTime.Add(new TimeSpan(0,
                                                                                                          CurrentlyScheduledTasks[finishedTaskId].Interval, 0));
                                PrintMessage(String.Format("[Scheduler] {0} task failed, the task will re-run automatically in {1} minutes", QueueList[0].Name, CurrentlyScheduledTasks[finishedTaskId].Interval), Logger.LogType.Warning);
                            }
                        }
                    }

                    // Refresh Queue list
                    var newQueueTasks = new ObservableCollection <Task>(QueueList);
                    newQueueTasks.RemoveAt(0);
                    QueueList = newQueueTasks;
                }
            }
            TaskRunning = false;
        }