public IActionResult ScheduledTaskEditor(int scheduledTaskId)
        {
            ScheduledTask task = null;

            if (scheduledTaskId < 1 || (task = _scheduledTaskService.Get(scheduledTaskId)) == null)
            {
                return(NotFound());
            }

            var model = _modelMapper.Map <ScheduledTaskModel>(task);

            return(R.Success.With("task", model).Result);
        }
Esempio n. 2
0
        public void Start()
        {
            //only start tasks if we have db installed
            if (!DatabaseManager.IsDatabaseInstalled())
            {
                return;
            }
            var availableTasks = DependencyResolver.ResolveMany <ITask>().ToList();
            var scheduledTasks = _scheduledTaskService.Get(x => true).ToList();

            //first sync available tasks and database tasks
            UpdateTasksInDatabase(availableTasks, scheduledTasks);
            if (!scheduledTasks.Any(x => x.Enabled))
            {
                return;
            }
            var registry = new Registry();

            //add each enabled task to the scheduler
            foreach (var sTask in scheduledTasks.Where(x => x.Enabled))
            {
                var task = availableTasks.FirstOrDefault(x => x.SystemName == sTask.SystemName);
                if (task == null)
                {
                    continue;
                }

                if (sTask.Seconds < 10)
                {
                    sTask.Seconds = 10;
                }
                //schedule the task
                registry.Schedule(() =>
                {
                    ScheduledTaskHelper.RunScheduledTask(sTask, task, _scheduledTaskService, _logger);
                }).ToRunEvery(sTask.Seconds).Seconds();
            }
            //initialize the jobmanager
            JobManager.Initialize(registry);
        }
        public void Start(Type[] availableTasksTypes)
        {
            if (!DatabaseManager.IsDatabaseInstalled())
            {
                return;
            }
            var container = mobSocialEngine.ActiveEngine.IocContainer;
            IList <ScheduledTask> scheduledTasks;

            //because dbcontext resolves per web request and the per request scope hasn't yet started here,
            //we'll have to fake open it, otherwise database context won't be resolved
            using (container.OpenScope(Reuse.WebRequestScopeName))
            {
                //get the scheduled tasks which are enabled
                scheduledTasks = _scheduledTaskService.Get(x => x.Enabled).ToList();
            }

            if (!scheduledTasks.Any())
            {
                return;
            }
            var registry = new Registry();

            //add each to the scheduler
            foreach (var sTask in scheduledTasks)
            {
                var taskType = availableTasksTypes.FirstOrDefault(x => x.FullName == sTask.SystemName);
                if (taskType != null)
                {
                    if (sTask.Seconds < 30)
                    {
                        sTask.Seconds = 30;
                    }
                    //schedule the task
                    registry.Schedule(() =>
                    {
                        using (var task = (ITask)Activator.CreateInstance(taskType))
                        {
                            try
                            {
                                sTask.LastStartDateTime = DateTime.UtcNow;
                                //because dbcontext resolves per web request and it won't be available for background tasks,
                                //we'll have to fake open it, otherwise database context won't be resolved
                                using (container.OpenScope(Reuse.WebRequestScopeName))
                                {
                                    task.Run();
                                }
                                sTask.LastSuccessDateTime = DateTime.UtcNow;
                            }
                            catch (Exception ex)
                            {
                                //check if task should be stopped
                                if (sTask.StopOnError)
                                {
                                    sTask.Enabled         = false;
                                    sTask.LastEndDateTime = DateTime.UtcNow;
                                }
                            }
                            finally
                            {
                                //update the task
                                _scheduledTaskService.Update(sTask);
                            }
                        }
                    }).ToRunEvery(sTask.Seconds).Seconds();
                }
            }

            //initialize the jobmanager
            JobManager.Initialize(registry);
        }