protected ScheduleTaskModel PrepareScheduleTaskModel(ScheduleTask task)
        {
            var model = new ScheduleTaskModel()
            {
                Id = task.Id,
                Name = task.Name,
                Seconds = task.Seconds,
                Enabled = task.Enabled,
                StopOnError = task.StopOnError,
                LastStartUtc = task.LastStartUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastStartUtc.Value, DateTimeKind.Utc).ToString("G") : "",
                LastEndUtc = task.LastEndUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastEndUtc.Value, DateTimeKind.Utc).ToString("G") : "",
                LastSuccessUtc = task.LastSuccessUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastSuccessUtc.Value, DateTimeKind.Utc).ToString("G") : "",
				LastError = task.LastError.EmptyNull(),
				IsRunning =	task.IsRunning,		//task.LastStartUtc.GetValueOrDefault() > task.LastEndUtc.GetValueOrDefault(),
				Duration = ""
            };

			var span = TimeSpan.Zero;
			if (task.LastStartUtc.HasValue)
			{
				span = model.IsRunning ? DateTime.UtcNow - task.LastStartUtc.Value : task.LastEndUtc.Value - task.LastStartUtc.Value;
				model.Duration = span.ToString("g");
			}

            return model;
        }
 protected ScheduleTaskModel PrepareScheduleTaskModel(ScheduleTask task)
 {
     var model = new ScheduleTaskModel()
     {
         Id = task.Id,
         Name = task.Name,
         Seconds = task.Seconds,
         Enabled = task.Enabled,
         StopOnError = task.StopOnError,
         LastStartUtc = task.LastStartUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastStartUtc.Value, DateTimeKind.Utc).ToString("G") : "",
         LastEndUtc = task.LastEndUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastEndUtc.Value, DateTimeKind.Utc).ToString("G") : "",
         LastSuccessUtc = task.LastSuccessUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastSuccessUtc.Value, DateTimeKind.Utc).ToString("G") : "",
     };
     return model;
 }
        public ActionResult TaskUpdate(ScheduleTaskModel model, GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageScheduleTasks))
                return AccessDeniedView();

            if (!ModelState.IsValid)
            {
                //display the first model error
                var modelStateErrors = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                return Content(modelStateErrors.FirstOrDefault());
            }

            var scheduleTask = _scheduleTaskService.GetTaskById(model.Id);
            if (scheduleTask == null)
                return Content("Schedule task cannot be loaded");

            scheduleTask.Name = model.Name;
            scheduleTask.Seconds = model.Seconds;
            scheduleTask.Enabled = model.Enabled;
            scheduleTask.StopOnError = model.StopOnError;

            int max = Int32.MaxValue / 1000;

            scheduleTask.Seconds = (model.Seconds > max ? max : model.Seconds);

            _scheduleTaskService.UpdateTask(scheduleTask);

            return List(command);
        }
        public ActionResult Edit(ScheduleTaskModel model, bool continueEditing, string returnUrl = null)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageScheduleTasks))
                return AccessDeniedView();

            ViewBag.ReturnUrl = returnUrl;

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var reloadResult = RedirectToAction("Edit", new { id = model.Id, returnUrl = returnUrl });
            var returnResult = Redirect(returnUrl.NullEmpty() ?? Url.Action("List"));

            var scheduleTask = _scheduleTaskService.GetTaskById(model.Id);
            if (scheduleTask == null)
            {
                NotifyError("Schedule task cannot be loaded");
                return reloadResult;
            }

            scheduleTask.Name = model.Name;
            scheduleTask.Enabled = model.Enabled;
            scheduleTask.StopOnError = model.StopOnError;
            scheduleTask.CronExpression = model.CronExpression;

            if (model.Enabled)
            {
                scheduleTask.NextRunUtc = _scheduleTaskService.GetNextSchedule(scheduleTask);
            }
            else
            {
                scheduleTask.NextRunUtc = null;
            }

            _scheduleTaskService.UpdateTask(scheduleTask);

            NotifySuccess(T("Admin.System.ScheduleTasks.UpdateSuccess"));

            if (continueEditing)
                return reloadResult;

            return returnResult;
        }
        protected ScheduleTaskModel PrepareScheduleTaskModel(ScheduleTask task)
        {
            var now = DateTime.UtcNow;

            TimeSpan? dueIn = null;
            if (task.NextRunUtc.HasValue)
            {
                dueIn = task.NextRunUtc.Value - now;
            }

            var nextRunPretty = "";
            bool isOverdue = false;
            if (dueIn.HasValue)
            {
                if (dueIn.Value.TotalSeconds > 0)
                {
                    nextRunPretty = dueIn.Value.Prettify();
                }
                else
                {
                    nextRunPretty = T("Common.Waiting") + "...";
                    isOverdue = true;
                }
            }

            var isRunning = task.IsRunning;
            var lastStartOn = task.LastStartUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastStartUtc.Value, DateTimeKind.Utc) : (DateTime?)null;
            var lastEndOn = task.LastEndUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastEndUtc.Value, DateTimeKind.Utc) : (DateTime?)null;
            var lastSuccessOn = task.LastSuccessUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.LastSuccessUtc.Value, DateTimeKind.Utc) : (DateTime?)null;
            var nextRunOn = task.NextRunUtc.HasValue ? _dateTimeHelper.ConvertToUserTime(task.NextRunUtc.Value, DateTimeKind.Utc) : (DateTime?)null;

            var model = new ScheduleTaskModel
            {
                Id = task.Id,
                Name = task.Name,
                CronExpression = task.CronExpression,
                CronDescription = CronExpression.GetFriendlyDescription(task.CronExpression),
                Enabled = task.Enabled,
                StopOnError = task.StopOnError,
                LastStart = lastStartOn,
                LastStartPretty = task.LastStartUtc.HasValue ? task.LastStartUtc.Value.RelativeFormat(true, "f") : "",
                LastEnd = lastEndOn,
                LastEndPretty = lastEndOn.HasValue ? lastEndOn.Value.ToString("G") : "",
                LastSuccess = lastSuccessOn,
                LastSuccessPretty = lastSuccessOn.HasValue ? lastSuccessOn.Value.ToString("G") : "",
                NextRun = nextRunOn,
                NextRunPretty = nextRunPretty,
                LastError = task.LastError.EmptyNull(),
                IsRunning = isRunning,
                CancelUrl = Url.Action("CancelJob", new { id = task.Id }),
                ExecuteUrl = Url.Action("RunJob", new { id = task.Id }),
                EditUrl = Url.Action("Edit", new { id = task.Id }),
                ProgressPercent = task.ProgressPercent,
                ProgressMessage = task.ProgressMessage,
                IsOverdue = isOverdue,
                Duration = ""
            };

            var span = TimeSpan.Zero;
            if (task.LastStartUtc.HasValue)
            {
                span = model.IsRunning ? now - task.LastStartUtc.Value : task.LastEndUtc.Value - task.LastStartUtc.Value;
                if (span > TimeSpan.Zero)
                {
                    model.Duration = span.ToString("g");
                }
            }

            return model;
        }