[13733] | 1 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
[13739] | 3 | using Microsoft.AspNet.Http;
|
---|
[13733] | 4 | using Microsoft.AspNet.SignalR;
|
---|
| 5 | using Newtonsoft.Json;
|
---|
| 6 | using System;
|
---|
| 7 | using System.Collections.Generic;
|
---|
[13739] | 8 | using System.Threading.Tasks;
|
---|
[13733] | 9 |
|
---|
| 10 | namespace 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)
|
---|
[13739] | 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.getFileOpener(userId);
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
[13733] | 37 | /// <summary>
|
---|
| 38 | /// Initial connection call from client
|
---|
| 39 | /// </summary>
|
---|
| 40 | public void initConnection()
|
---|
| 41 | {
|
---|
[13739] | 42 | loader();
|
---|
| 43 | fileopener.previousids = new List<Guid>();
|
---|
| 44 | fileopener.previousLogs = new List<int>();
|
---|
| 45 | Job = fileopener.Job;
|
---|
[13733] | 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 | {
|
---|
[13739] | 54 | loader();
|
---|
| 55 | fileopener.refreshJob();//refresh all data from job
|
---|
| 56 | Job = fileopener.Job;
|
---|
[13733] | 57 | updateJob();
|
---|
| 58 | foreach (var t in Job.HiveTasks)
|
---|
| 59 | {
|
---|
| 60 | looperTasks(t);//starts recursive loop
|
---|
| 61 | }
|
---|
[13735] | 62 | Clients.Caller.requestDone();//Final note from server when all tasks are checked.
|
---|
[13733] | 63 | //Client starts countdown and resends the updateAll when done
|
---|
| 64 | GC.Collect();
|
---|
| 65 | GC.WaitForPendingFinalizers();
|
---|
| 66 |
|
---|
| 67 | }
|
---|
[13739] | 68 | private void updateJob()
|
---|
[13733] | 69 | {
|
---|
[13735] | 70 | Clients.Caller.processJobData(Job.Job.CalculatingCount, Job.Job.FinishedCount);
|
---|
[13733] | 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;
|
---|
[13739] | 82 | if (fileopener.previousids.Contains(task.Task.Id))
|
---|
[13733] | 83 | {
|
---|
[13739] | 84 | index = fileopener.previousids.IndexOf(task.Task.Id);
|
---|
[13733] | 85 | }
|
---|
| 86 | else
|
---|
| 87 | {//initial add to previous list, used to check if updates happened
|
---|
[13739] | 88 | fileopener.previousids.Add(task.Task.Id);
|
---|
| 89 | index = fileopener.previousids.IndexOf(task.Task.Id);
|
---|
| 90 | fileopener.previousLogs.Add(task.Task.StateLog.Count);
|
---|
[13733] | 91 | test = true;//initial added task, data must be sent
|
---|
| 92 | }
|
---|
[13739] | 93 | var previous = fileopener.previousLogs[index];
|
---|
[13733] | 94 | if (test || previous < task.Task.StateLog.Count)
|
---|
| 95 | {//Checks if change happened so data is not sent unnecessary
|
---|
[13739] | 96 | fileopener.previousLogs[index] = task.Task.StateLog.Count;
|
---|
[13733] | 97 | JsonSerializerSettings settings = new JsonSerializerSettings();
|
---|
| 98 | settings.ContractResolver = new JsonTaskResolver();
|
---|
| 99 | //limits loaded data
|
---|
| 100 | var json = JsonConvert.SerializeObject(task.Task, settings);
|
---|
[13735] | 101 | Clients.Caller.processData(task.Task.Id, json, task.ItemTask.Name.ToString());
|
---|
[13733] | 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 | {
|
---|
[13739] | 117 | loader();
|
---|
| 118 | weblog.getServiceLocator(userId).getHiveServiceClient().RestartTask(id);
|
---|
[13733] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|