Esempio n. 1
0
        public ActionResult Create([Bind(Include = "RecipientId,Name,Address")] DbRecipient recipient)
        {
            if (ModelState.IsValid)
            {
                db.DbRecipients.Add(recipient);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(recipient));
        }
Esempio n. 2
0
        public ActionResult Create(DbMailTask dbMailTask)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(dbMailTask.TaskName))
                {
                    dbMailTask.TaskName = "Unnamed task";
                }

                dbMailTask.CreationTime  = DateTime.Now;
                dbMailTask.LastWriteTime = DateTime.Now;
                db.DbMailTasks.Add(dbMailTask);
                db.SaveChanges();
                return(RedirectToAction("Index", "DbRecipients"));
            }

            return(View(dbMailTask));
        }
Esempio n. 3
0
        static void _updateResult(int taskId,
                                  bool isSucceeded,
                                  string recipientAddress,
                                  string status)
        {
            using (var db = new WebProject1Context())
            {
                DbRecipientResult result = new DbRecipientResult();

                result.CreationTime = DateTime.Now;
                result.IsSucceeded  = isSucceeded;
                result.Recipient    = recipientAddress;
                result.TaskId       = taskId;
                result.Status       = status;

                db.DbRecipientResults.Add(result);
                db.SaveChanges();

                var task       = db.DbMailTasks.Find(taskId);
                var taskStatus = _queryTaskStatus(taskId);

                if (task != null && taskStatus != null)
                {
                    task.Failed     = taskStatus.Failed;
                    task.Succeeded  = taskStatus.Succeeded;
                    task.TotalCount = taskStatus.TotalCount;
                    if (taskStatus.Completed)
                    {
                        task.Status = DbMailTaskStatus.Completed;
                    }

                    db.Entry(task).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
Esempio n. 4
0
        static void _queryTaskThreadProc()
        {
            using (var db = new WebProject1Context())
            {
                // change running tasks (unfinished) to terminated
                var unfinishedTasks = (from s in db.DbMailTasks
                                       select s)
                                      .Where(s => s.Status == DbMailTaskStatus.Running)
                                      .ToArray();

                for (int i = 0; i < unfinishedTasks.Length; i++)
                {
                    var task = unfinishedTasks[i];
                    task.Status          = DbMailTaskStatus.Terminated;
                    db.Entry(task).State = EntityState.Modified;
                    db.SaveChanges();
                }

                int miniSleep = 2;
                while (true)
                {
                    var tasks = (from s in db.DbMailTasks
                                 select s)
                                .Where(s => s.Status == DbMailTaskStatus.Pending)
                                .ToArray();

                    if (tasks.Length == 0)
                    {
                        Thread.Sleep(miniSleep);
                        if (miniSleep * miniSleep < 1024)
                        {
                            miniSleep *= miniSleep;
                        }
                        continue;
                    }

                    miniSleep = 2;

                    for (int i = 0; i < tasks.Length; i++)
                    {
                        _processTask(db, tasks[i]);
                    }
                }
            }
        }
Esempio n. 5
0
        static void _processTask(WebProject1Context db, DbMailTask mailTask)
        {
            var threadPool = new DbSendThreadPool();

            threadPool.UpdateResult = _updateResult;
            _threadPools.Add(mailTask.TaskId, threadPool);

            var recipients = db.DbRecipients.ToArray();

            mailTask.TotalCount = recipients.Length;
            mailTask.Status     = DbMailTaskStatus.Running;

            db.Entry(mailTask).State = EntityState.Modified;
            db.SaveChanges();

            Task.Run(() =>
            {
                threadPool.Start(mailTask, recipients);
            });
        }