/// <summary> /// Runs a job on a specific group - used when the user clicks on "run now" /// </summary> /// <param name="groupId">The id of the group</param> /// <param name="job">The job object</param> /// <returns></returns> public async Task RunJobOnGroup(int?groupId, Job job) { await Task.Run(async() => { if (job != null) { //Get available groups IEnumerable <Group> groups = await _groupsMethods.GetGroupsAsync(); if (groupId == null) //Run job on all nodes { //Run job locally await ExecuteAsync(job); foreach (Node node in await _nodesMethods.GetNodesAsync()) { try { using HttpClient client = new HttpClient(); StringContent content = new StringContent(JsonSerializer.Serialize(job), Encoding.UTF8, "application/json"); await client.PostAsync($"{node.IPStr}:{node.Port}/api/jobs/start", content); } catch (Exception) { } } } else //Run job on defined group { IEnumerable <Node> nodes = groups.FirstOrDefault(x => x.Id == groupId).GroupNodes.Select(x => x.Node); foreach (Node node in nodes) { if (node.Role == NodeRole.Master) { //Run job locally await ExecuteAsync(job); } else { try { using HttpClient client = new HttpClient(); StringContent content = new StringContent(JsonSerializer.Serialize(job), Encoding.UTF8, "application/json"); HttpResponseMessage res = await client.PostAsync($"{node.IPStr}:{node.Port}/api/jobs/start", content); } catch (Exception) { } } } } } }); }
/// <summary> /// Runs the first job in the queue and updates the queue /// </summary> private async void WakeUp(object sender, ElapsedEventArgs e) { Job job = Jobs?.FirstOrDefault()?.Job; if (job != null) { //Get available groups IEnumerable <Group> groups = await _groupsMethods.GetGroupsAsync(); //Group id int?groupId = job.GroupId; if (groupId == null) { //Run job locally await _jobRunner.ExecuteAsync(Jobs.FirstOrDefault()?.Job); foreach (Node node in await _nodesMethods.GetNodesAsync()) { RunJobOnNodes(node, job); } } else { IEnumerable <Node> nodes = groups.FirstOrDefault(x => x.Id == groupId).GroupNodes.Select(x => x.Node); foreach (Node node in nodes) { if (node.Role == NodeRole.Master) { //Run job locally await _jobRunner.ExecuteAsync(Jobs.FirstOrDefault()?.Job); } else { RunJobOnNodes(node, job); } } } } RemoveExecutedJob(); UpdateWakeUpTimer(); }