Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Hubs/JobUpdaterHub.cs @ 13733

Last change on this file since 13733 was 13733, checked in by jlodewyc, 8 years ago

#2582 Last fixes Job Manager

File size: 4.1 KB
Line 
1using HeuristicLab.Clients.Hive.WebJobManager.Services;
2using HeuristicLab.Common;
3using Microsoft.AspNet.SignalR;
4using Newtonsoft.Json;
5using System;
6using System.Collections.Generic;
7
8namespace HeuristicLab.Clients.Hive.WebJobManager
9{
10    /// <summary>
11    /// SignalR Hub for updating a current selected job
12    /// </summary>
13    public class JobUpdaterHub : Hub
14    {
15       
16        private RefreshableJob Job;//Current job (only used as reference, instance gets lost)
17     
18        /// <summary>
19        /// Initial connection call from client
20        /// </summary>
21        public void initConnection()
22        {
23            FileOpeningService.Instance.previousids = new List<Guid>();
24            FileOpeningService.Instance.previousLogs = new List<int>();
25            Job = FileOpeningService.Instance.Job;
26            updateAll();//start initial update
27
28        }
29        /// <summary>
30        /// Starts update loop for selected job. Sends end token when done and calls GC
31        /// </summary>
32        public void updateAll()
33        {
34            FileOpeningService.Instance.refreshJob();//refresh all data from job
35            Job = FileOpeningService.Instance.Job;
36            updateJob();
37            foreach (var t in Job.HiveTasks)
38            {
39                looperTasks(t);//starts recursive loop
40            }
41            Clients.All.requestDone();//Final note from server when all tasks are checked.
42            //Client starts countdown and resends the updateAll when done
43            GC.Collect();
44            GC.WaitForPendingFinalizers();
45
46        }
47        public void updateJob()
48        {
49            Clients.All.processJobData(Job.Job.CalculatingCount, Job.Job.FinishedCount);
50        }
51        /// <summary>
52        /// Recursive loop for updating the selected job view
53        /// </summary>
54        /// <param name="task">Loops on all children</param>
55        private void looperTasks(HiveTask task)
56        {
57            try
58            {
59                int index;
60                bool test = false;
61                if (FileOpeningService.Instance.previousids.Contains(task.Task.Id))
62                {
63                    index = FileOpeningService.Instance.previousids.IndexOf(task.Task.Id);
64                }
65                else
66                {//initial add to previous list, used to check if updates happened
67                    FileOpeningService.Instance.previousids.Add(task.Task.Id);
68                    index = FileOpeningService.Instance.previousids.IndexOf(task.Task.Id);
69                    FileOpeningService.Instance.previousLogs.Add(task.Task.StateLog.Count);
70                    test = true;//initial added task, data must be sent
71                }
72                var previous = FileOpeningService.Instance.previousLogs[index];
73                if (test || previous < task.Task.StateLog.Count)
74                {//Checks if change happened so data is not sent unnecessary
75                    FileOpeningService.Instance.previousLogs[index] = task.Task.StateLog.Count;
76                    JsonSerializerSettings settings = new JsonSerializerSettings();
77                    settings.ContractResolver = new JsonTaskResolver();
78                    //limits loaded data
79                    var json = JsonConvert.SerializeObject(task.Task, settings);
80                    Clients.All.processData(task.Task.Id, json, task.ItemTask.Name.ToString());
81                    //data sent to user
82                }
83                foreach (var t in task.ChildHiveTasks)
84                {//loop on childs
85                    looperTasks(t);
86                }
87            }
88            catch (JsonSerializationException e) { }//Taskresolver fixes much but img tries to throw this sometimes
89        }
90        /// <summary>
91        /// Restarts a failed task, sent by client
92        /// </summary>
93        /// <param name="id">Task id from failed task</param>
94        public void restartTask(Guid id)
95        {
96            ((HiveServiceLocatorWeb)(HiveServiceLocatorWeb.Instance)).getHiveServiceClient().RestartTask(id);
97        }
98    }
99}
Note: See TracBrowser for help on using the repository browser.