Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 More parameter datatypes, splitting fileopening service, approving users, reopen last file, change name tasks and repetitions

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