Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 RC2 migration fixed. OKB query implemented. Preparing for OKB manager

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using HeuristicLab.Clients.Hive.WebJobManager.Services;
24using Microsoft.AspNetCore.SignalR;
25using Newtonsoft.Json;
26using System;
27using System.Collections.Generic;
28
29namespace HeuristicLab.Clients.Hive.WebJobManager
30{
31    /// <summary>
32    /// SignalR Hub for updating a current selected job
33    /// </summary>
34    public class JobUpdaterHub : Hub
35    {
36       
37        private RefreshableJob Job;//Current job (only used as reference, instance gets lost)
38
39        private WebLoginService weblog;
40        private FileOpeningService fileopener;
41        private Guid userId;
42
43        private void loader()
44        {
45            weblog = WebLoginService.Instance;
46            string uid = Context.QueryString["userid"];
47            if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty)
48            {
49                userId = Guid.Empty;
50            }
51            else {
52                userId = Guid.Parse(uid);
53                fileopener = weblog.getJobOpener(userId);
54            }
55        }
56        /// <summary>
57        /// Initial connection call from client
58        /// </summary>
59        public void initConnection()
60        {
61            loader();
62            fileopener.previousids = new List<Guid>();
63            fileopener.previousLogs = new List<int>();
64            Job = fileopener.Job;
65            updateAll();//start initial update
66
67        }
68        /// <summary>
69        /// Starts update loop for selected job. Sends end token when done and calls GC
70        /// </summary>
71        public void updateAll()
72        {
73            loader();
74            fileopener.refreshJob();//refresh all data from job
75            Job = fileopener.Job;
76            updateJob();
77            foreach (var t in Job.HiveTasks)
78            {
79                looperTasks(t);//starts recursive loop
80            }
81            Clients.Caller.requestDone();//Final note from server when all tasks are checked.
82            //Client starts countdown and resends the updateAll when done
83            GC.Collect();
84            GC.WaitForPendingFinalizers();
85
86        }
87        private void updateJob()
88        {
89            Clients.Caller.processJobData(Job.Job.CalculatingCount, Job.Job.FinishedCount);
90        }
91        /// <summary>
92        /// Recursive loop for updating the selected job view
93        /// </summary>
94        /// <param name="task">Loops on all children</param>
95        private void looperTasks(HiveTask task)
96        {
97            try
98            {
99                int index;
100                bool test = false;
101                if (fileopener.previousids.Contains(task.Task.Id))
102                {
103                    index = fileopener.previousids.IndexOf(task.Task.Id);
104                }
105                else
106                {//initial add to previous list, used to check if updates happened
107                    fileopener.previousids.Add(task.Task.Id);
108                    index = fileopener.previousids.IndexOf(task.Task.Id);
109                    fileopener.previousLogs.Add(task.Task.StateLog.Count);
110                    test = true;//initial added task, data must be sent
111                }
112                var previous = fileopener.previousLogs[index];
113                if (test || previous < task.Task.StateLog.Count)
114                {//Checks if change happened so data is not sent unnecessary
115                    fileopener.previousLogs[index] = task.Task.StateLog.Count;
116                    JsonSerializerSettings settings = new JsonSerializerSettings();
117                    settings.ContractResolver = new JsonTaskResolver();
118                    //limits loaded data
119                    var json = JsonConvert.SerializeObject(task.Task, settings);
120                    Clients.Caller.processData(task.Task.Id, json, task.ItemTask.Name.ToString());
121                    //data sent to user
122                }
123                foreach (var t in task.ChildHiveTasks)
124                {//loop on childs
125                    looperTasks(t);
126                }
127            }
128            catch (JsonSerializationException e) { }//Taskresolver fixes much but img tries to throw this sometimes
129        }
130        /// <summary>
131        /// Restarts a failed task, sent by client
132        /// </summary>
133        /// <param name="id">Task id from failed task</param>
134        public void restartTask(Guid id)
135        {
136            loader();
137            weblog.getServiceLocator(userId).getHiveServiceClient().RestartTask(id);
138        }
139    }
140}
Note: See TracBrowser for help on using the repository browser.