private void ExecuteServiceTask(ServiceTask task)
        {
            TaskRunnerLog runnerLog = new TaskRunnerLog()
            {
                DateStarted = DateTime.Now,
                TaskName    = task.Name,
                TaskResult  = "RUNNING"
            };

            try
            {
                IRepository <TaskRunnerLog> taskRunnerLogRepository = new EfRepository <TaskRunnerLog>(new DatabaseContext());
                taskRunnerLogRepository.GetMany(l => l.TaskResult == "RUNNING" && l.TaskName == task.Name).ToList().ForEach(l => {
                    l.DateEnded  = DateTime.Now;
                    l.TaskResult = "STOPPED";
                });
                taskRunnerLogRepository.Save();
                taskRunnerLogRepository.Insert(runnerLog);
                taskRunnerLogRepository.Save();
                IServiceTask serviceTask = (IServiceTask)Activator.CreateInstance(task.ExecuteAssembly, task.ExecuteModule).Unwrap();
                serviceTask.Run(task.Settings);
                runnerLog.TaskResult = "COMPLETE";
                runnerLog.DateEnded  = DateTime.Now;
                taskRunnerLogRepository.Save();
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
                runnerLog.TaskResult = "ERROR";
                runnerLog.DateEnded  = DateTime.Now;
            }
        }
        public int ProcessQueue()
        {
            Dictionary <string, List <Proposal> > dictProposalsForUsers = new Dictionary <string, List <Proposal> >();

            TaskRunnerLog lastRun        = _taskRunnerLogRepository.GetMany(l => l.TaskResult.ToUpper() == "COMPLETE").OrderByDescending(l => l.DateStarted).FirstOrDefault();
            DateTime      dReferenceDate = lastRun != null ? lastRun.DateStarted : DateTime.Now.AddMonths(-7);

            List <Proposal> lstProposals = this._repository.GetMany(x => x.StartDate > dReferenceDate && x.EndDate > DateTime.Now).ToList();

            List <ApplicationUser> lstUsersSubscribedToAll = _dc.Users.Where(x => x.SubscriptionType == "ALL").ToList();

            foreach (ApplicationUser xApplicationUser in lstUsersSubscribedToAll)
            {
                dictProposalsForUsers.Add(xApplicationUser.Id, lstProposals);
            }

            List <ApplicationUser> lstUserFromProposals = new List <ApplicationUser>();

            foreach (Proposal xProposal in lstProposals)
            {
                foreach (ApplicationUser xUser in xProposal.Institution.SubscribedUsers.Where(u => u.SubscriptionType == "SELECTED"))
                {
                    if (!dictProposalsForUsers.ContainsKey(xUser.Id))
                    {
                        dictProposalsForUsers.Add(xUser.Id, new List <Proposal>());
                        lstUserFromProposals.Add(xUser);
                    }

                    dictProposalsForUsers[xUser.Id].Add(xProposal);
                }
            }

            List <ApplicationUser> lstAllUsers = new List <ApplicationUser>();

            lstAllUsers.AddRange(lstUsersSubscribedToAll);
            lstAllUsers.AddRange(lstUserFromProposals);

            //add to notification table
            foreach (KeyValuePair <string, List <Proposal> > entry in dictProposalsForUsers.Where(p => p.Value.Count() > 0))
            {
                AddToNotification(lstAllUsers.Find(x => x.Id == entry.Key), entry.Value);
            }
            return(this.SuccessfulNotifications);
        }