Ejemplo n.º 1
0
        public string RunTask()
        {
            //run emailTask



            IList <TaskShit> tasks = _helperSvc.GetAllTask();

            if (tasks.Count() == 3)
            {
                //add Executebulkstafftask
                TaskShit BulkUpload = new TaskShit
                {
                    Name            = "BulkUpload",
                    Enabled         = true,
                    status          = false,
                    RunTimerSeconds = 360,
                    LastRun         = CurrentRequestData.Now
                };
                _helperSvc.addTask(BulkUpload);


                //add Executebulkstafftask
                TaskShit PortalImport = new TaskShit
                {
                    Name            = "PortalImport",
                    Enabled         = true,
                    status          = false,
                    RunTimerSeconds = 360,
                    LastRun         = CurrentRequestData.Now
                };
                _helperSvc.addTask(PortalImport);
            }

            if (!tasks.Any())
            {
                //create tasks

                TaskShit birthday = new TaskShit
                {
                    Name            = "Birthday",
                    Enabled         = true,
                    status          = false,
                    RunTimerSeconds = 36000,
                    LastRun         = CurrentRequestData.Now
                };

                TaskShit Email = new TaskShit
                {
                    Name            = "SendEmail",
                    Enabled         = true,
                    status          = false,
                    RunTimerSeconds = 30,
                    LastRun         = CurrentRequestData.Now
                };


                TaskShit Subscriptions = new TaskShit
                {
                    Name            = "Subscriptions",
                    Enabled         = true,
                    status          = false,
                    RunTimerSeconds = 36000,
                    LastRun         = CurrentRequestData.Now
                };

                _helperSvc.addTask(birthday);
                _helperSvc.addTask(Email);
                _helperSvc.addTask(Subscriptions);
            }

            foreach (TaskShit task in tasks)
            {
                double timediff = CurrentRequestData.Now.Subtract(task.LastRun).TotalSeconds;
                //check if task is running.
                if (!task.status && timediff >= task.RunTimerSeconds)
                {
                    switch (task.Name)
                    {
                    case "SendEmail":
                        try
                        {
                            SendEmailMessages(task);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw;
                        }
                        break;


                    case "Birthday":
                        try
                        {
                            DateTime datetime = CurrentRequestData.Now;

                            int hour = datetime.Hour;

                            if (hour > 8)
                            {
                                SendBithdayMessageTask(task);
                            }
                        }
                        catch (Exception e)
                        {
                            task.status = false;
                            _helperSvc.updateTask(task);
                            Console.WriteLine(e);
                            throw;
                        }
                        break;


                    case "Subscriptions":
                        try
                        {
                            SubscriptionTask(task);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw;
                        }
                        break;
                    }
                }
            }

            return("Complete");
        }