Example #1
0
        /// <summary>
        /// Unschedule an existing task based on the view model
        /// </summary>
        /// <param name="vm">The view model corresponding to the task we want to unschedule</param>
        public void UnscheduleTask(ScheduledTaskViewModel vm)
        {
            //get the part
            ScheduledTaskPart part = (ScheduledTaskPart)_orchardServices.ContentManager.Get <ScheduledTaskPart>(vm.Id);
            int tId = part.RunningTaskId;

            if (tId > 0)
            {
                var str = _repoTasks.Get(tId);
                if (str != null)
                {
                    _repoTasks.Delete(str);
                }
                else
                {
                    //tId might have changed since the moment we got the information into the view models
                    //e.g. if the task is periodic, it will generate a new Id and update it.
                    //let's check here if there are tasks with the part id in the TaskType
                    //(see the ScheduleTask method for the format we are using)
                    var records = _repoTasks.Table.ToList().Where(rec =>
                                                                  //rec.TaskType.Split(new string[]{"_"}, StringSplitOptions.RemoveEmptyEntries).Last().Equals(part.Id.ToString())
                                                                  rec.TaskType.IndexOf(Constants.TaskTypeBase) == 0
                                                                  ).ToList().Where(rec =>
                                                                                   rec.TaskType.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries).Last().Equals(part.Id.ToString())
                                                                                   ).ToList();
                    foreach (var item in records)
                    {
                        _repoTasks.Delete(item);
                    }
                }
            }

            part.RunningTaskId = 0;
        }
Example #2
0
        public async Task <Response> AddTaskAsync(ScheduledTaskViewModel scheduledTaskViewModel)
        {
            var response = new Response();
            var result   = await databaseContext.Add(new ScheduledTask(scheduledTaskViewModel, false));

            response.Fetch(result);

            return(response);
        }
Example #3
0
        public async Task <ActionResult> StopJob([FromBody] ScheduledTaskViewModel model)
        {
            var result = await _quartzService.InterruptJob(model.Name);

            return(JsonCamelCase(new JSendResponse
            {
                Status = result.Status,
                Message = result.Message
            }));
        }
Example #4
0
        public async Task <ActionResult> RunJob([FromBody] ScheduledTaskViewModel model)
        {
            var result = await _quartzService.RunJob(model.Name, QPContext.CurrentCustomerCode);

            return(JsonCamelCase(new JSendResponse
            {
                Status = result.Status,
                Message = result.Message
            }));
        }
Example #5
0
        public ScheduledTask(ScheduledTaskViewModel scheduledTaskViewModel, bool parseId = true)
        {
            if (parseId)
            {
                _id = ObjectId.Parse(scheduledTaskViewModel.Id);
            }

            Name              = scheduledTaskViewModel.Name;
            Status            = scheduledTaskViewModel.Status;
            ExecutionTime     = scheduledTaskViewModel.ExecutionTime;
            LastExecutionTime = scheduledTaskViewModel.LastExecutionTime;
            Action            = scheduledTaskViewModel.Action;
        }
Example #6
0
        /// <summary>
        /// Schedule a new task based on the information in the view model
        /// </summary>
        /// <param name="vm">The view model we are basing the new task on</param>
        public void ScheduleTask(ScheduledTaskViewModel vm)
        {
            //get the part
            ScheduledTaskPart part = (ScheduledTaskPart)_orchardServices.ContentManager.Get <ScheduledTaskPart>(vm.Id);
            //define tasktype: BASE_SIGNALNAME_ID
            string      taskTypeStr = Constants.TaskTypeBase + "_" + part.SignalName + "_" + part.Id;
            ContentItem ci          = null;

            if (part.ContentItemId > 0)
            {
                ci = _orchardServices.ContentManager.Get(part.ContentItemId);
            }
            _taskManager.CreateTask(taskTypeStr, part.ScheduledStartUTC ?? DateTime.UtcNow, ci);
            part.RunningTaskId = _repoTasks.Get(str => str.TaskType.Equals(taskTypeStr)).Id;
        }
Example #7
0
        public ActionResult IndexScheduleTask(IndexViewModel ivm)
        {
            var formData = ControllerContext.RequestContext.HttpContext.Request.Form;
            List <ScheduledTaskViewModel> vmsForTasks = _scheduledTaskService.GetTaskViewModelsFromForm(formData);

            _scheduledTaskService.UpdateRecords(vmsForTasks);
            //get the vm for the task we are trying to schedule
            ScheduledTaskViewModel vmToSchedule = vmsForTasks.Where(vm => vm.Scheduling).FirstOrDefault();

            if (vmToSchedule != null)
            {
                _scheduledTaskService.ScheduleTask(vmToSchedule);
            }

            return(RedirectToAction("Index"));
        }
Example #8
0
        public async Task <Response> UpdateTaskAsync(ScheduledTaskViewModel updatedScheduledTask)
        {
            var updatedTask = new ScheduledTask(updatedScheduledTask);

            return(await databaseContext.Update(updatedTask, updatedTask._id));
        }
Example #9
0
 public IActionResult NextChild(ScheduledTaskViewModel model)
 {
     return(View(model));
 }
        public void Process(ScheduledTaskContext context)
        {
            string taskTypeStr = context.Task.TaskType;

            if (taskTypeStr.IndexOf(Constants.TaskTypeBase) == 0)
            {
                try {
                    //For some reason, querying the db for the records related to the task returns null
                    //Hence, we placed the part id in the TaskType.
                    int pid = int.Parse(taskTypeStr.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries).Last());
                    ScheduledTaskPart part = (ScheduledTaskPart)_orchardServices.ContentManager.Get <ScheduledTaskPart>(pid);
                    if (part == null)
                    {
                        Logger.Error("Laser.TaskScheduler was unable to identify and process the task of type " + taskTypeStr);
                    }
                    else
                    {
                        //Trigger the event
                        //get the signal name for from the part to track edits that may have been done.
                        if (part.ExecutionType == ExecutionTypes.WorkFlow)
                        {
                            _workflowManager.TriggerEvent(
                                SignalActivity.SignalEventName,
                                context.Task.ContentItem,
                                () => new Dictionary <string, object> {
                                { "Content", context.Task.ContentItem },
                                { SignalActivity.SignalEventName, part.SignalName }
                            }
                                );
                        }
                        else
                        {
                            if (part.ExecutionType == ExecutionTypes.Razor)
                            {
                                if (part.LongTask)
                                {
                                    _sweepGenerator.Terminate();
                                    try {
                                        var result = _razorExecuteService.Execute(part.SignalName, context.Task.ContentItem, new Dictionary <string, object>()
                                        {
                                            { "Content", context.Task.ContentItem },
                                            { SignalActivity.SignalEventName, part.SignalName }
                                        }).Trim();
                                    }
                                    catch (Exception ex) {
                                        Logger.Error(ex, "ScheduledTaskTasksHandler -> Long Task Error on " + taskTypeStr + ex.Message);
                                    }
                                    finally {
                                        if (part.LongTask)
                                        {
                                            _sweepGenerator.Activate();
                                        }
                                    }
                                }
                                else
                                {
                                    var result = _razorExecuteService.Execute(part.SignalName, context.Task.ContentItem, new Dictionary <string, object>()
                                    {
                                        { "Content", context.Task.ContentItem },
                                        { SignalActivity.SignalEventName, part.SignalName }
                                    }).Trim();
                                }
                            }
                        }

                        if (part.Autodestroy)
                        {
                            var sc = new ScheduledTaskViewModel(part);
                            sc.Delete = true;
                            var list = new List <ScheduledTaskViewModel>();
                            list.Add(sc);
                            _scheduledTaskService.UpdateRecords(list);
                        }
                        else
                        {
                            //if the part has periodicity and it was not unscheduled, we may reschedule the task
                            if (part.PeriodicityTime > 0 && part.RunningTaskId > 0)
                            {
                                //define tasktype
                                string      newTaskTypeStr = Constants.TaskTypeBase + "_" + part.SignalName + "_" + part.Id;
                                ContentItem ci             = null;
                                if (part.ContentItemId > 0)
                                {
                                    ci = _orchardServices.ContentManager.Get(part.ContentItemId);
                                }
                                DateTime scheduleTime = _scheduledTaskService.ComputeNextScheduledTime(part);
                                _taskManager.CreateTask(newTaskTypeStr, scheduleTime, ci);
                                part.RunningTaskId = _repoTasks.Get(str => str.TaskType.Equals(newTaskTypeStr)).Id;
                            }
                            else
                            {
                                part.RunningTaskId = 0;
                            }
                        }
                    }
                }
                catch (Exception ex) {
                    Logger.Error(ex, "ScheduledTaskTasksHandler -> Error on " + taskTypeStr + " id= " + context.Task.ContentItem + ex.Message);
                }
            }
        }
Example #11
0
        public async Task <Response <List <ScheduledTaskViewModel> > > UpdateScheduledTask([FromBody] ScheduledTaskViewModel updatedScheduledTask)
        {
            await scheduledTaskService.UpdateTaskAsync(updatedScheduledTask);

            return(await scheduledTaskService.GetAllAsync());
        }
Example #12
0
        public async Task <Response <List <ScheduledTaskViewModel> > > AddScheduledTask([FromBody] ScheduledTaskViewModel scheduledTask)
        {
            await scheduledTaskService.AddTaskAsync(scheduledTask);

            return(await scheduledTaskService.GetAllAsync());
        }