Exemplo n.º 1
0
        public static void RunScheduledBatchSending(MessagesModuleLogic Logic, BatchSendingTaskSettings Settings = null)
        {
            if (!ScheduledBatchSending_H.GetSafeThreadPermission())
            {
                return;
            }
            try {
                Settings = Settings ?? DefaultsBatchSendingTaskSettings;
                using (var manager = BatchSendingManager.NewInstance(Settings, Logic)) {
                    var allPendingMails = manager.GetPendingEmails();
                    var allPendingSms   = manager.GetPendingSms();

                    var batches = allPendingMails
                                  .Select(x => x.tblMessageBatch)
                                  .Distinct()
                                  .Concat(allPendingSms
                                          .Select(x => x.tblMessageBatch)
                                          .Distinct())
                                  .ToArray();

                    //EMAIL
                    var emailProviders = manager.GetActiveEmailProviders().ToList().GetEnumerator();
                    var Emailer        = new EmailSender(manager);
                    SendCycle(manager, allPendingMails, emailProviders, Emailer);
                    //SMS
                    var smsProviders = manager.GetActiveSmsProviders().ToList().GetEnumerator();
                    var Smser        = new SmsSender(manager);
                    SendCycle(manager, allPendingSms, smsProviders, Smser);

                    foreach (var batch in batches)
                    {
                        //check if batch finished
                        if (!batch.tblMessages.Where(x => (x.tblPendingMessagesQueue != null) && (!x.tblPendingMessagesQueue.Deleted)).Any())
                        {
                            batch.FinishedOn = DateTime.Now;
                        }
                        //TODO THIS CHECK IS NOT RIGHT DECISION
                        //add errors to batch if there are some
                        if (batch.tblMessages.Where(x => x.ErrorLog != null).Any())
                        {
                            (batch as IErrorLoged).AddError("Some errors have arisen at sending stage at " + DateTime.Now.ToShortDateString());
                        }
                        manager.Logic.SaveLazy(batch);
                    }
                    Logic.SaveChanges();
                    //Logic.Dispose();
                }
            }
            finally {
                ScheduledBatchSending_H.ReportTaskEnded();
            }
        }