Exemple #1
0
        public async Task <IActionResult> Put(string id, [FromBody] CreateScheduleViewModel request)
        {
            try
            {
                //validate the cron expression
                if (!string.IsNullOrWhiteSpace(request.CRONExpression))
                {
                    try
                    {
                        CronExpression expression = CronExpression.Parse(request.CRONExpression, CronFormat.Standard);
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("Save", string.Concat("Invalid cron expression, ", ex.Message));
                        return(BadRequest(ModelState));
                    }
                }

                Guid entityId = new Guid(id);

                var existingSchedule = repository.GetOne(entityId);
                if (existingSchedule == null)
                {
                    return(NotFound());
                }

                existingSchedule.Name           = request.Name;
                existingSchedule.AgentId        = request.AgentId;
                existingSchedule.CRONExpression = request.CRONExpression;
                existingSchedule.LastExecution  = request.LastExecution;
                existingSchedule.NextExecution  = request.NextExecution;
                existingSchedule.IsDisabled     = request.IsDisabled;
                existingSchedule.ProjectId      = request.ProjectId;
                existingSchedule.StartingType   = request.StartingType;
                existingSchedule.Status         = request.Status;
                existingSchedule.ExpiryDate     = request.ExpiryDate;
                existingSchedule.StartDate      = request.StartDate;
                existingSchedule.AutomationId   = request.AutomationId;

                var response = await base.PutEntity(id, existingSchedule);

                manager.DeleteExistingParameters(entityId);

                var set = new HashSet <string>();
                foreach (var parameter in request.Parameters ?? Enumerable.Empty <ParametersViewModel>())
                {
                    if (!set.Add(parameter.Name))
                    {
                        ModelState.AddModelError("ScheduleParameter", "ScheduleParameter Name Already Exists");
                        return(BadRequest(ModelState));
                    }

                    ScheduleParameter scheduleParameter = new ScheduleParameter
                    {
                        Name       = parameter.Name,
                        DataType   = parameter.DataType,
                        Value      = parameter.Value,
                        ScheduleId = entityId,
                        CreatedBy  = applicationUser?.UserName,
                        CreatedOn  = DateTime.UtcNow,
                        Id         = Guid.NewGuid()
                    };
                    scheduleParameterRepository.Add(scheduleParameter);
                }

                recurringJobManager.RemoveIfExists(existingSchedule.Id?.ToString());

                if (request.IsDisabled == false && !request.StartingType.ToLower().Equals("manual"))
                {
                    var jsonScheduleObj = JsonSerializer.Serialize <Schedule>(existingSchedule);

                    backgroundJobClient.Schedule(() => hubManager.ScheduleNewJob(jsonScheduleObj),
                                                 new DateTimeOffset(existingSchedule.StartDate.Value));
                }

                return(response);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Schedule", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Put(string id, [FromBody] CreateScheduleViewModel request)
        {
            try
            {
                ParametersViewModel.VerifyParameterNameAvailability(request.Parameters);
                //validate the cron expression
                if (!string.IsNullOrWhiteSpace(request.CRONExpression))
                {
                    if (string.IsNullOrWhiteSpace(request.CRONExpressionTimeZone))
                    {
                        request.CRONExpressionTimeZone = "UTC";
                    }
                    try
                    {
                        CronExpression expression = CronExpression.Parse(request.CRONExpression, CronFormat.Standard);
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("Save", string.Concat("Invalid cron expression, ", ex.Message));
                        return(BadRequest(ModelState));
                    }
                }

                Schedule existingSchedule = _manager.UpdateSchedule(id, request);
                var      response         = await base.PutEntity(id, existingSchedule);

                _manager.DeleteExistingParameters(id);

                var set = new HashSet <string>();
                foreach (var parameter in request.Parameters ?? Enumerable.Empty <ParametersViewModel>())
                {
                    if (!set.Add(parameter.Name))
                    {
                        ModelState.AddModelError("ScheduleParameter", "ScheduleParameter Name Already Exists");
                        return(BadRequest(ModelState));
                    }

                    ScheduleParameter scheduleParameter = new ScheduleParameter
                    {
                        Name       = parameter.Name,
                        DataType   = parameter.DataType,
                        Value      = parameter.Value,
                        ScheduleId = existingSchedule.Id.Value,
                        CreatedBy  = applicationUser?.UserName,
                        CreatedOn  = DateTime.UtcNow,
                        Id         = Guid.NewGuid()
                    };
                    _scheduleParameterRepository.Add(scheduleParameter);
                }

                _recurringJobManager.RemoveIfExists(existingSchedule.Id?.ToString());

                if (request.IsDisabled == false && !request.StartingType.ToLower().Equals("manual"))
                {
                    var jsonScheduleObj = JsonSerializer.Serialize <Schedule>(existingSchedule);

                    _backgroundJobClient.Schedule(() => _hubManager.ScheduleNewJob(jsonScheduleObj),
                                                  new DateTimeOffset(existingSchedule.StartDate.Value));
                }

                return(response);
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }