public async Task <IActionResult> Post([FromBody] CreateJobViewModel request) { if (request == null) { ModelState.AddModelError("Save", "No data passed"); return(BadRequest(ModelState)); } Guid entityId = Guid.NewGuid(); if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty)) { request.Id = entityId; } try { Job job = request.Map(request); //assign request to job entity Automation automation = automationRepo.GetOne(job.AutomationId ?? Guid.Empty); if (automation == null) //no automation was found { ModelState.AddModelError("Save", "No automation was found for the specified automation id"); return(NotFound(ModelState)); } AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == automation.Id).Items?.FirstOrDefault(); job.AutomationVersion = automationVersion.VersionNumber; job.AutomationVersionId = automationVersion.Id; foreach (var parameter in request.JobParameters ?? Enumerable.Empty <JobParameter>()) { parameter.JobId = entityId; parameter.CreatedBy = applicationUser?.UserName; parameter.CreatedOn = DateTime.UtcNow; parameter.Id = Guid.NewGuid(); jobParameterRepo.Add(parameter); } //send SignalR notification to all connected clients await _hub.Clients.All.SendAsync("botnewjobnotification", request.AgentId.ToString()); await _hub.Clients.All.SendAsync("sendjobnotification", "New Job added."); await _hub.Clients.All.SendAsync("broadcastnewjobs", Tuple.Create(request.Id, request.AgentId, request.AutomationId)); await webhookPublisher.PublishAsync("Jobs.NewJobCreated", job.Id.ToString()).ConfigureAwait(false); return(await base.PostEntity(job)); } catch (Exception ex) { return(ex.GetActionResult()); } }
public IEnumerable <JobParameter> AddJobParameters(IEnumerable <JobParameter> jobParameters, Guid?jobId) { List <JobParameter> parameterList = new List <JobParameter>(); foreach (var parameter in jobParameters ?? Enumerable.Empty <JobParameter>()) { parameter.JobId = jobId ?? Guid.Empty; parameter.CreatedBy = _caller.Identity.Name; parameter.CreatedOn = DateTime.UtcNow; parameter.Id = Guid.NewGuid(); _jobParameterRepo.Add(parameter); parameterList.Add(parameter); } return(parameterList.AsEnumerable()); }
public string CreateJob(string scheduleSerializeObject, IEnumerable <ParametersViewModel>?parameters) { var schedule = JsonSerializer.Deserialize <Schedule>(scheduleSerializeObject); if (organizationSettingManager.HasDisallowedExecution()) { return("DisallowedExecution"); } var automationVersion = automationVersionRepository.Find(null, a => a.AutomationId == schedule.AutomationId).Items?.FirstOrDefault(); //if this is a scheduled job get the schedule parameters if (schedule.StartingType.Equals("RunNow") == false) { List <ParametersViewModel> parametersList = new List <ParametersViewModel>(); var scheduleParameters = scheduleParameterRepository.Find(null, p => p.ScheduleId == schedule.Id).Items; foreach (var scheduleParameter in scheduleParameters) { ParametersViewModel parametersViewModel = new ParametersViewModel { Name = scheduleParameter.Name, DataType = scheduleParameter.DataType, Value = scheduleParameter.Value, CreatedBy = scheduleParameter.CreatedBy, CreatedOn = DateTime.UtcNow }; parametersList.Add(parametersViewModel); } parameters = parametersList.AsEnumerable(); } Job job = new Job(); job.AgentId = schedule.AgentId == null ? Guid.Empty : schedule.AgentId.Value; job.CreatedBy = schedule.CreatedBy; job.CreatedOn = DateTime.UtcNow; job.EnqueueTime = DateTime.UtcNow; job.JobStatus = JobStatusType.New; job.AutomationId = schedule.AutomationId == null ? Guid.Empty : schedule.AutomationId.Value; job.AutomationVersion = automationVersion != null ? automationVersion.VersionNumber : 0; job.AutomationVersionId = automationVersion != null ? automationVersion.Id : Guid.Empty; job.Message = "Job is created through internal system logic."; foreach (var parameter in parameters ?? Enumerable.Empty <ParametersViewModel>()) { JobParameter jobParameter = new JobParameter { Name = parameter.Name, DataType = parameter.DataType, Value = parameter.Value, JobId = job.Id ?? Guid.Empty, CreatedBy = schedule.CreatedBy, CreatedOn = DateTime.UtcNow, Id = Guid.NewGuid() }; jobParameterRepository.Add(jobParameter); } jobRepository.Add(job); _hub.Clients.All.SendAsync("botnewjobnotification", job.AgentId.ToString()); webhookPublisher.PublishAsync("Jobs.NewJobCreated", job.Id.ToString()).ConfigureAwait(false); return("Success"); }
public string CreateJob(string scheduleSerializeObject, IEnumerable<ParametersViewModel>? parameters) { var schedule = JsonSerializer.Deserialize<Schedule>(scheduleSerializeObject); //if schedule has expired if (DateTime.UtcNow > schedule.ExpiryDate) { _recurringJobManager.RemoveIfExists(schedule.Id.Value.ToString());//removes an existing recurring job return "ScheduleExpired"; } if (_organizationSettingManager.HasDisallowedExecution()) { return "DisallowedExecution"; } //if this is not a "RunNow" job, then use the schedule parameters if (schedule.StartingType.Equals("RunNow") == false) { if (schedule.MaxRunningJobs != null) { if (ActiveJobLimitReached(schedule.Id, schedule.MaxRunningJobs)) { return "ActiveJobLimitReached"; } } List<ParametersViewModel> parametersList = new List<ParametersViewModel>(); var scheduleParameters = _scheduleParameterRepository.Find(null, p => p.ScheduleId == schedule.Id).Items; foreach (var scheduleParameter in scheduleParameters) { ParametersViewModel parametersViewModel = new ParametersViewModel { Name = scheduleParameter.Name, DataType = scheduleParameter.DataType, Value = scheduleParameter.Value, CreatedBy = scheduleParameter.CreatedBy, CreatedOn = DateTime.UtcNow }; parametersList.Add(parametersViewModel); } parameters = parametersList.AsEnumerable(); } var automationVersion = _automationVersionRepository.Find(null, a => a.AutomationId == schedule.AutomationId).Items?.FirstOrDefault(); Job job = new Job(); job.AgentId = schedule.AgentId == null ? Guid.Empty : schedule.AgentId.Value; job.AgentGroupId = schedule.AgentGroupId == null ? Guid.Empty : schedule.AgentGroupId.Value; job.CreatedBy = schedule.CreatedBy; job.CreatedOn = DateTime.UtcNow; job.EnqueueTime = DateTime.UtcNow; job.JobStatus = JobStatusType.New; job.AutomationId = schedule.AutomationId == null ? Guid.Empty : schedule.AutomationId.Value; job.AutomationVersion = automationVersion != null ? automationVersion.VersionNumber : 0; job.AutomationVersionId = automationVersion != null ? automationVersion.Id : Guid.Empty; job.Message = "Job is created through internal system logic."; job.ScheduleId = schedule.Id; foreach (var parameter in parameters ?? Enumerable.Empty<ParametersViewModel>()) { JobParameter jobParameter = new JobParameter { Name = parameter.Name, DataType = parameter.DataType, Value = parameter.Value, JobId = job.Id ?? Guid.Empty, CreatedBy = schedule.CreatedBy, CreatedOn = DateTime.UtcNow, Id = Guid.NewGuid() }; _jobParameterRepository.Add(jobParameter); } _jobRepository.Add(job); if (job.AgentGroupId == null || job.AgentGroupId == Guid.Empty) { _hub.Clients.All.SendAsync("botnewjobnotification", job.AgentId.ToString()); } else //notify all group members { var agentsInGroup = _agentGroupManager.GetAllMembersInGroup(job.AgentGroupId.ToString()); foreach (var groupMember in agentsInGroup ?? Enumerable.Empty<AgentGroupMember>()) { _hub.Clients.All.SendAsync("botnewjobnotification", groupMember.AgentId.ToString()); } } _webhookPublisher.PublishAsync("Jobs.NewJobCreated", job.Id.ToString()).ConfigureAwait(false); return "Success"; }