- Timestamp:
- 03/29/16 17:02:16 (9 years ago)
- Location:
- branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services
- Files:
-
- 1 added
- 2 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/ConcurrentTaskDownloaderWeb.cs
r13733 r13739 30 30 31 31 // 32 /// <summary> 33 /// Rewritten to use HiveServiceLocatorWeb and HiveClientWeb 34 /// </summary> 35 public class ConcurrentTaskDownloaderWeb<T> : IDisposable where T : class, ITask { 36 private bool abort = false; 37 // use semaphore to ensure only few concurrenct connections and few SerializedJob objects in memory 38 private Semaphore downloadSemaphore; 39 private Semaphore deserializeSemaphore; 32 /// <summary> 33 /// Rewritten to use HiveServiceLocatorWeb and HiveClientWeb 34 /// </summary> 35 public class ConcurrentTaskDownloaderWeb<T> : IDisposable where T : class, ITask 36 { 37 private bool abort = false; 38 // use semaphore to ensure only few concurrenct connections and few SerializedJob objects in memory 39 private Semaphore downloadSemaphore; 40 private Semaphore deserializeSemaphore; 41 private HiveServiceLocatorWeb serviceLocator; 42 public ConcurrentTaskDownloaderWeb(int concurrentDownloads, int concurrentDeserializations, HiveServiceLocatorWeb hiv) 43 { 44 downloadSemaphore = new Semaphore(concurrentDownloads, concurrentDownloads); 45 deserializeSemaphore = new Semaphore(concurrentDeserializations, concurrentDeserializations); 46 serviceLocator = hiv; 47 } 40 48 41 public ConcurrentTaskDownloaderWeb(int concurrentDownloads, int concurrentDeserializations) { 42 downloadSemaphore = new Semaphore(concurrentDownloads, concurrentDownloads); 43 deserializeSemaphore = new Semaphore(concurrentDeserializations, concurrentDeserializations); 49 public void DownloadTaskData(Task t, Action<Task, T> onFinishedAction) 50 { 51 Task<Tuple<Task, T>> task = Task<Tuple<Task, TaskData>>.Factory.StartNew(DownloadTaskData, t) 52 .ContinueWith((y) => DeserializeTask(y.Result)); 53 54 task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion); 55 task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted); 56 } 57 58 public void DownloadTaskDataAndTask(Guid taskId, Action<Task, T> onFinishedAction) 59 { 60 Task<Tuple<Task, T>> task = Task<Task>.Factory.StartNew(DownloadTask, taskId) 61 .ContinueWith((x) => DownloadTaskData(x.Result)) 62 .ContinueWith((y) => DeserializeTask(y.Result)); 63 64 task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion); 65 task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted); 66 } 67 68 private void OnTaskFinished(Task<Tuple<Task, T>> task, Action<Task, T> onFinishedAction) 69 { 70 onFinishedAction(task.Result.Item1, task.Result.Item2); 71 } 72 private void OnTaskFailed(Task<Tuple<Task, T>> task) 73 { 74 task.Exception.Flatten().Handle((e) => { return true; }); 75 OnExceptionOccured(task.Exception.Flatten()); 76 } 77 78 private Task DownloadTask(object taskId) 79 { 80 Task t = null; 81 HiveClientWeb.TryAndRepeat(() => 82 { 83 t = serviceLocator.CallHiveService(s => s.GetTask((Guid)taskId)); 84 }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task."); 85 return t; 86 } 87 88 protected Tuple<Task, TaskData> DownloadTaskData(object taskId) 89 { 90 return DownloadTaskData((Task)taskId); 91 } 92 93 protected Tuple<Task, TaskData> DownloadTaskData(Task task) 94 { 95 downloadSemaphore.WaitOne(); 96 TaskData result = null; 97 try 98 { 99 if (abort) return null; 100 HiveClientWeb.TryAndRepeat(() => 101 { 102 result = serviceLocator.CallHiveService(s => s.GetTaskData(task.Id)); 103 }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task data."); 104 } 105 finally 106 { 107 downloadSemaphore.Release(); 108 } 109 return new Tuple<Task, TaskData>(task, result); 110 } 111 112 protected Tuple<Task, T> DeserializeTask(Tuple<Task, TaskData> taskData) 113 { 114 deserializeSemaphore.WaitOne(); 115 try 116 { 117 if (abort || taskData.Item2 == null || taskData.Item1 == null) return null; 118 var deserializedJob = PersistenceUtil.Deserialize<T>(taskData.Item2.Data); 119 taskData.Item2.Data = null; // reduce memory consumption. 120 return new Tuple<Task, T>(taskData.Item1, deserializedJob); 121 } 122 finally 123 { 124 deserializeSemaphore.Release(); 125 } 126 } 127 128 public event EventHandler<EventArgs<Exception>> ExceptionOccured; 129 private void OnExceptionOccured(Exception exception) 130 { 131 var handler = ExceptionOccured; 132 if (handler != null) handler(this, new EventArgs<Exception>(exception)); 133 } 134 135 #region IDisposable Members 136 public void Dispose() 137 { 138 deserializeSemaphore.Dispose(); 139 downloadSemaphore.Dispose(); 140 } 141 #endregion 44 142 } 45 46 public void DownloadTaskData(Task t, Action<Task, T> onFinishedAction) {47 Task<Tuple<Task, T>> task = Task<Tuple<Task, TaskData>>.Factory.StartNew(DownloadTaskData, t)48 .ContinueWith((y) => DeserializeTask(y.Result));49 50 task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);51 task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);52 }53 54 public void DownloadTaskDataAndTask(Guid taskId, Action<Task, T> onFinishedAction) {55 Task<Tuple<Task, T>> task = Task<Task>.Factory.StartNew(DownloadTask, taskId)56 .ContinueWith((x) => DownloadTaskData(x.Result))57 .ContinueWith((y) => DeserializeTask(y.Result));58 59 task.ContinueWith((x) => OnTaskFinished(x, onFinishedAction), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion);60 task.ContinueWith((x) => OnTaskFailed(x), TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);61 }62 63 private void OnTaskFinished(Task<Tuple<Task, T>> task, Action<Task, T> onFinishedAction) {64 onFinishedAction(task.Result.Item1, task.Result.Item2);65 }66 private void OnTaskFailed(Task<Tuple<Task, T>> task) {67 task.Exception.Flatten().Handle((e) => { return true; });68 OnExceptionOccured(task.Exception.Flatten());69 }70 71 private Task DownloadTask(object taskId) {72 Task t = null;73 HiveClientWeb.TryAndRepeat(() => {74 t = HiveServiceLocatorWeb.Instance.CallHiveService(s => s.GetTask((Guid)taskId));75 }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task.");76 return t;77 }78 79 protected Tuple<Task, TaskData> DownloadTaskData(object taskId) {80 return DownloadTaskData((Task)taskId);81 }82 83 protected Tuple<Task, TaskData> DownloadTaskData(Task task) {84 downloadSemaphore.WaitOne();85 TaskData result = null;86 try {87 if (abort) return null;88 HiveClientWeb.TryAndRepeat(() => {89 result = HiveServiceLocatorWeb.Instance.CallHiveService(s => s.GetTaskData(task.Id));90 }, Settings.Default.MaxRepeatServiceCalls, "Failed to download task data.");91 }92 finally {93 downloadSemaphore.Release();94 }95 return new Tuple<Task, TaskData>(task, result);96 }97 98 protected Tuple<Task, T> DeserializeTask(Tuple<Task, TaskData> taskData) {99 deserializeSemaphore.WaitOne();100 try {101 if (abort || taskData.Item2 == null || taskData.Item1 == null) return null;102 var deserializedJob = PersistenceUtil.Deserialize<T>(taskData.Item2.Data);103 taskData.Item2.Data = null; // reduce memory consumption.104 return new Tuple<Task, T>(taskData.Item1, deserializedJob);105 }106 finally {107 deserializeSemaphore.Release();108 }109 }110 111 public event EventHandler<EventArgs<Exception>> ExceptionOccured;112 private void OnExceptionOccured(Exception exception) {113 var handler = ExceptionOccured;114 if (handler != null) handler(this, new EventArgs<Exception>(exception));115 }116 117 #region IDisposable Members118 public void Dispose() {119 deserializeSemaphore.Dispose();120 downloadSemaphore.Dispose();121 }122 #endregion123 }124 143 } -
branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/HiveClientWeb.cs
r13733 r13739 46 46 public sealed class HiveClientWeb : IContent 47 47 { 48 private static HiveClientWeb instance; 49 public static HiveClientWeb Instance 50 { 51 get 52 { 53 if (instance == null) instance = new HiveClientWeb(); 54 return instance; 55 } 56 } 57 public static IHostingEnvironment CurrentEnv { get; set; } //Current building area 48 49 public IHostingEnvironment CurrentEnv { get; set; } //Current building area 58 50 #region Properties 59 51 private HiveItemCollection<RefreshableJob> jobs; … … 86 78 #endregion 87 79 88 private HiveClientWeb() 89 { 90 } 80 private HiveServiceLocatorWeb serviceLocator; 81 public Guid UserId { get; set; } 82 public HiveClientWeb(HiveServiceLocatorWeb serv, Guid u) 83 { 84 serviceLocator = serv; 85 UserId = u; 86 } 87 /// <summary> 88 /// Reaches to the webloginservice to get the current instance of the servicelocator. 89 /// </summary> 90 public void updateServiceLocator() 91 {// No idea if needed 92 serviceLocator = WebLoginService.Instance.getServiceLocator(UserId); 93 } 94 91 95 92 96 public void ClearHiveClientWeb() … … 117 121 { 118 122 jobs = new HiveItemCollection<RefreshableJob>(); 119 var jobsLoaded = HiveServiceLocatorWeb.Instance.CallHiveService<IEnumerable<Job>>(s => s.GetJobs());123 var jobsLoaded = serviceLocator.CallHiveService<IEnumerable<Job>>(s => s.GetJobs()); 120 124 121 125 foreach (var j in jobsLoaded) … … 158 162 159 163 #region Store 160 public staticvoid Store(IHiveItem item, CancellationToken cancellationToken)164 public void Store(IHiveItem item, CancellationToken cancellationToken) 161 165 { 162 166 if (item.Id == Guid.Empty) … … 164 168 if (item is RefreshableJob) 165 169 { 166 HiveClientWeb.Instance.UploadJob((RefreshableJob)item, cancellationToken);170 this.UploadJob((RefreshableJob)item, cancellationToken); 167 171 } 168 172 if (item is JobPermission) 169 173 { 170 174 var hep = (JobPermission)item; 171 hep.GrantedUserId = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName));175 hep.GrantedUserId = serviceLocator.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName)); 172 176 if (hep.GrantedUserId == Guid.Empty) 173 177 { 174 178 throw new ArgumentException(string.Format("The user {0} was not found.", hep.GrantedUserName)); 175 179 } 176 HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.GrantPermission(hep.JobId, hep.GrantedUserId, hep.Permission));180 serviceLocator.CallHiveService((s) => s.GrantPermission(hep.JobId, hep.GrantedUserId, hep.Permission)); 177 181 } 178 182 } 179 183 else { 180 184 if (item is Job) 181 HiveServiceLocatorWeb.Instance.CallHiveService(s => s.UpdateJob((Job)item));182 } 183 } 184 public staticvoid StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken)185 serviceLocator.CallHiveService(s => s.UpdateJob((Job)item)); 186 } 187 } 188 public void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken) 185 189 { 186 190 var call = new Func<Exception>(delegate () … … 205 209 206 210 #region Delete 207 public staticvoid Delete(IHiveItem item)211 public void Delete(IHiveItem item) 208 212 { 209 213 if (item.Id == Guid.Empty && item.GetType() != typeof(JobPermission)) … … 211 215 212 216 if (item is Job) 213 HiveServiceLocatorWeb.Instance.CallHiveService(s => s.DeleteJob(item.Id));217 serviceLocator.CallHiveService(s => s.DeleteJob(item.Id)); 214 218 if (item is RefreshableJob) 215 219 { … … 219 223 job.StopResultPolling(); 220 224 } 221 HiveServiceLocatorWeb.Instance.CallHiveService(s => s.DeleteJob(item.Id));225 serviceLocator.CallHiveService(s => s.DeleteJob(item.Id)); 222 226 } 223 227 if (item is JobPermission) 224 228 { 225 229 var hep = (JobPermission)item; 226 HiveServiceLocatorWeb.Instance.CallHiveService(s => s.RevokePermission(hep.JobId, hep.GrantedUserId));230 serviceLocator.CallHiveService(s => s.RevokePermission(hep.JobId, hep.GrantedUserId)); 227 231 } 228 232 item.Id = Guid.Empty; … … 251 255 #endregion 252 256 253 public staticvoid StartJob(Action<Exception> exceptionCallback, RefreshableJob refreshableJob, CancellationToken cancellationToken)254 { 255 HiveClientWeb.StoreAsync(257 public void StartJob(Action<Exception> exceptionCallback, RefreshableJob refreshableJob, CancellationToken cancellationToken) 258 { 259 this.StoreAsync( 256 260 new Action<Exception>((Exception ex) => 257 261 { … … 262 266 } 263 267 264 public staticvoid PauseJob(RefreshableJob refreshableJob)265 { 266 HiveServiceLocatorWeb.Instance.CallHiveService(service =>268 public void PauseJob(RefreshableJob refreshableJob) 269 { 270 serviceLocator.CallHiveService(service => 267 271 { 268 272 foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) … … 274 278 } 275 279 276 public staticvoid StopJob(RefreshableJob refreshableJob)277 { 278 HiveServiceLocatorWeb.Instance.CallHiveService(service =>280 public void StopJob(RefreshableJob refreshableJob) 281 { 282 serviceLocator.CallHiveService(service => 279 283 { 280 284 foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) … … 286 290 } 287 291 288 public staticvoid ResumeJob(RefreshableJob refreshableJob)289 { 290 HiveServiceLocatorWeb.Instance.CallHiveService(service =>292 public void ResumeJob(RefreshableJob refreshableJob) 293 { 294 serviceLocator.CallHiveService(service => 291 295 { 292 296 foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) … … 315 319 foreach (var resourceName in resourceNames) 316 320 { 317 Guid resourceId = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.GetResourceId(resourceName));321 Guid resourceId = serviceLocator.CallHiveService((s) => s.GetResourceId(resourceName)); 318 322 if (resourceId == Guid.Empty) 319 323 { … … 330 334 // upload Job 331 335 refreshableJob.Progress.Status = "Uploading Job..."; 332 refreshableJob.Job.Id = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.AddJob(refreshableJob.Job));333 refreshableJob.Job = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions336 refreshableJob.Job.Id = serviceLocator.CallHiveService((s) => s.AddJob(refreshableJob.Job)); 337 refreshableJob.Job = serviceLocator.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions 334 338 cancellationToken.ThrowIfCancellationRequested(); 335 339 … … 340 344 // upload plugins 341 345 refreshableJob.Progress.Status = "Uploading plugins..."; 342 this.OnlinePlugins = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.GetPlugins());346 this.OnlinePlugins = serviceLocator.CallHiveService((s) => s.GetPlugins()); 343 347 this.AlreadyUploadedPlugins = new List<Plugin>(); 344 Plugin configFilePlugin = HiveServiceLocatorWeb.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));348 Plugin configFilePlugin = serviceLocator.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins)); 345 349 this.alreadyUploadedPlugins.Add(configFilePlugin); 346 350 cancellationToken.ThrowIfCancellationRequested(); … … 371 375 /// Uploads the local configuration file as plugin 372 376 /// </summary> 373 private staticPlugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins)377 private Plugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins) 374 378 { 375 379 string exeFilePath = Path.Combine( CurrentEnv.WebRootPath, "bin" , HeuristicLab.Clients.Hive.Settings.Default.HLBinaryName); … … 436 440 lock (pluginLocker) 437 441 { 438 HiveServiceLocatorWeb.Instance.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));442 serviceLocator.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins)); 439 443 } 440 444 } … … 451 455 if (parentHiveTask != null) 452 456 { 453 hiveTask.Task.Id = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData));457 hiveTask.Task.Id = serviceLocator.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData)); 454 458 } 455 459 else { 456 hiveTask.Task.Id = HiveServiceLocatorWeb.Instance.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList()));460 hiveTask.Task.Id = serviceLocator.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList())); 457 461 } 458 462 } … … 488 492 489 493 #region Download Experiment 490 public staticvoid LoadJob(RefreshableJob refreshableJob)494 public void LoadJob(RefreshableJob refreshableJob) 491 495 { 492 496 var hiveExperiment = refreshableJob.Job; … … 501 505 // fetch all task objects to create the full tree of tree of HiveTask objects 502 506 refreshableJob.Progress.Start("Downloading list of tasks..."); 503 allTasks = HiveServiceLocatorWeb.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(hiveExperiment.Id));507 allTasks = serviceLocator.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(hiveExperiment.Id)); 504 508 totalJobCount = allTasks.Count(); 505 509 506 510 refreshableJob.Progress.Status = "Downloading tasks..."; 507 downloader = new TaskDownloaderWeb(allTasks.Select(x => x.Id) );511 downloader = new TaskDownloaderWeb(allTasks.Select(x => x.Id), serviceLocator); 508 512 downloader.StartAsync(); 509 513 … … 572 576 } 573 577 574 public staticItemTask LoadItemJob(Guid jobId)575 { 576 TaskData taskData = HiveServiceLocatorWeb.Instance.CallHiveService(s => s.GetTaskData(jobId));578 public ItemTask LoadItemJob(Guid jobId) 579 { 580 TaskData taskData = serviceLocator.CallHiveService(s => s.GetTaskData(jobId)); 577 581 try 578 582 { … … 603 607 } 604 608 605 public staticHiveItemCollection<JobPermission> GetJobPermissions(Guid jobId)606 { 607 return HiveServiceLocatorWeb.Instance.CallHiveService((service) =>609 public HiveItemCollection<JobPermission> GetJobPermissions(Guid jobId) 610 { 611 return serviceLocator.CallHiveService((service) => 608 612 { 609 613 IEnumerable<JobPermission> jps = service.GetJobPermissions(jobId); -
branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/HiveServiceLocatorWeb.cs
r13733 r13739 2 2 using HeuristicLab.Clients.Common.Properties; 3 3 using HeuristicLab.Clients.Hive.WebJobManager.ViewModels; 4 using Microsoft.AspNet.Http; 4 5 using Microsoft.AspNet.Mvc; 5 6 using System; … … 17 18 public class HiveServiceLocatorWeb : IHiveServiceLocator 18 19 { 19 private static IHiveServiceLocator instance = null;20 20 private HiveServiceClient clientinst; 21 public static IHiveServiceLocator Instance 22 { 23 get 24 { 25 if (instance == null) 26 { 27 instance = new HiveServiceLocatorWeb(); 28 } 29 return instance; 30 } 31 } 21 public Guid UserId {get; set; } 32 22 private string username; 33 23 public string Username … … 47 37 public string WorkingEndpoint { get; private set; } 48 38 49 public static void clear()50 {51 instance = null;52 }53 39 54 40 #region #unknownCalls … … 132 118 return false; 133 119 } 134 public static void SetLoginErrorMessage()135 {136 LoginViewModelService.Instance.GetLoginViewModel().errorMessage = "Login timed out";137 }138 120 139 121 public string GetEndpointInformation() … … 195 177 } 196 178 197 public void destroy ()179 public void destroyClient() 198 180 { 199 181 clientinst = null; 200 instance = null;201 182 } 202 183 } -
branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/TaskDownloaderWeb.cs
r13733 r13739 26 26 using HeuristicLab.Common; 27 27 28 namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports { 28 namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports 29 { 29 30 30 31 /// <summary> 31 32 /// Rewritten to use HiveClientWeb and HiveServiceLocatorWeb 32 33 /// </summary> 33 public class TaskDownloaderWeb : IDisposable { 34 private IEnumerable<Guid> taskIds; 35 private ConcurrentTaskDownloaderWeb<ItemTask> taskDownloader; 36 private IDictionary<Guid, HiveTask> results; 37 private bool exceptionOccured = false; 38 private Exception currentException; 39 private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim(); 34 public class TaskDownloaderWeb : IDisposable 35 { 36 private HiveServiceLocatorWeb serviceLocator; 37 private IEnumerable<Guid> taskIds; 38 private ConcurrentTaskDownloaderWeb<ItemTask> taskDownloader; 39 private IDictionary<Guid, HiveTask> results; 40 private bool exceptionOccured = false; 41 private Exception currentException; 42 private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim(); 40 43 41 public bool IsFinished { 42 get { 43 try { 44 resultsLock.EnterReadLock(); 45 return results.Count == taskIds.Count(); 44 public bool IsFinished 45 { 46 get 47 { 48 try 49 { 50 resultsLock.EnterReadLock(); 51 return results.Count == taskIds.Count(); 52 } 53 finally { resultsLock.ExitReadLock(); } 54 } 46 55 } 47 finally { resultsLock.ExitReadLock(); } 48 } 56 57 public bool IsFaulted 58 { 59 get 60 { 61 return exceptionOccured; 62 } 63 } 64 65 public Exception Exception 66 { 67 get 68 { 69 return currentException; 70 } 71 } 72 73 public int FinishedCount 74 { 75 get 76 { 77 try 78 { 79 resultsLock.EnterReadLock(); 80 return results.Count; 81 } 82 finally { resultsLock.ExitReadLock(); } 83 } 84 } 85 86 public IDictionary<Guid, HiveTask> Results 87 { 88 get 89 { 90 try 91 { 92 resultsLock.EnterReadLock(); 93 return results; 94 } 95 finally { resultsLock.ExitReadLock(); } 96 } 97 } 98 99 public TaskDownloaderWeb(IEnumerable<Guid> jobIds, HiveServiceLocatorWeb hiv) 100 { 101 taskIds = jobIds; 102 serviceLocator = hiv; 103 taskDownloader = new ConcurrentTaskDownloaderWeb<ItemTask>( 104 Settings.Default.MaxParallelDownloads, 105 Settings.Default.MaxParallelDownloads, 106 serviceLocator); 107 taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured); 108 results = new Dictionary<Guid, HiveTask>(); 109 110 } 111 112 public void StartAsync() 113 { 114 foreach (Guid taskId in taskIds) 115 { 116 taskDownloader.DownloadTaskDataAndTask(taskId, 117 (localTask, itemTask) => 118 { 119 if (localTask != null && itemTask != null) 120 { 121 HiveTask hiveTask = itemTask.CreateHiveTask(); 122 hiveTask.Task = localTask; 123 try 124 { 125 resultsLock.EnterWriteLock(); 126 results.Add(localTask.Id, hiveTask); 127 } 128 finally { resultsLock.ExitWriteLock(); } 129 } 130 }); 131 } 132 } 133 134 private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) 135 { 136 OnExceptionOccured(e.Value); 137 } 138 139 public event EventHandler<EventArgs<Exception>> ExceptionOccured; 140 private void OnExceptionOccured(Exception exception) 141 { 142 exceptionOccured = true; 143 currentException = exception; 144 var handler = ExceptionOccured; 145 if (handler != null) handler(this, new EventArgs<Exception>(exception)); 146 } 147 148 #region IDisposable Members 149 public void Dispose() 150 { 151 taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured); 152 resultsLock.Dispose(); 153 taskDownloader.Dispose(); 154 } 155 #endregion 49 156 } 50 51 public bool IsFaulted {52 get {53 return exceptionOccured;54 }55 }56 57 public Exception Exception {58 get {59 return currentException;60 }61 }62 63 public int FinishedCount {64 get {65 try {66 resultsLock.EnterReadLock();67 return results.Count;68 }69 finally { resultsLock.ExitReadLock(); }70 }71 }72 73 public IDictionary<Guid, HiveTask> Results {74 get {75 try {76 resultsLock.EnterReadLock();77 return results;78 }79 finally { resultsLock.ExitReadLock(); }80 }81 }82 83 public TaskDownloaderWeb(IEnumerable<Guid> jobIds) {84 taskIds = jobIds;85 taskDownloader = new ConcurrentTaskDownloaderWeb<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);86 taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);87 results = new Dictionary<Guid, HiveTask>();88 }89 90 public void StartAsync() {91 foreach (Guid taskId in taskIds) {92 taskDownloader.DownloadTaskDataAndTask(taskId,93 (localTask, itemTask) => {94 if (localTask != null && itemTask != null) {95 HiveTask hiveTask = itemTask.CreateHiveTask();96 hiveTask.Task = localTask;97 try {98 resultsLock.EnterWriteLock();99 results.Add(localTask.Id, hiveTask);100 }101 finally { resultsLock.ExitWriteLock(); }102 }103 });104 }105 }106 107 private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {108 OnExceptionOccured(e.Value);109 }110 111 public event EventHandler<EventArgs<Exception>> ExceptionOccured;112 private void OnExceptionOccured(Exception exception) {113 exceptionOccured = true;114 currentException = exception;115 var handler = ExceptionOccured;116 if (handler != null) handler(this, new EventArgs<Exception>(exception));117 }118 119 #region IDisposable Members120 public void Dispose() {121 taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);122 resultsLock.Dispose();123 taskDownloader.Dispose();124 }125 #endregion126 }127 157 } -
branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Jobs/FileOpeningService.cs
r13733 r13739 16 16 public class FileOpeningService 17 17 { 18 public Guid UserId { get; set; } 19 20 private WebLoginService weblog; 18 21 public RefreshableJob Job { get; set; } 19 22 public FileOpeningViewModel vm { get; set; } … … 22 25 get; set; } 23 26 public List<int> previousLogs { get; set; } 24 private static FileOpeningService instance; 25 public static FileOpeningService Instance 27 28 29 public FileOpeningService(Guid u) 26 30 { 27 get 28 { 29 if (instance == null) 30 { 31 instance = new FileOpeningService(); 32 } 33 return instance; 34 } 31 weblog = WebLoginService.Instance; 32 UserId = u; 35 33 } 34 35 36 36 /// <summary> 37 37 /// Clears loaded jobs and start new one … … 58 58 { 59 59 // HiveClientWeb.Instance.Refresh(); 60 HiveClientWeb.LoadJob(Job);60 weblog.getClientWeb(UserId).LoadJob(Job); 61 61 } 62 62 /// <summary> … … 68 68 if (vm != null) 69 69 { 70 71 72 73 HiveServiceLocatorWeb serv = (HiveServiceLocatorWeb)HiveServiceLocatorWeb.Instance; 74 //job.Job.Id = serv.getHiveServiceClient().AddJob(job.Job); 75 // job.Job = serv.getHiveServiceClient().GetJob(job.Job.Id); 76 77 HiveClientWeb.CurrentEnv = env; 78 HiveClientWeb.Instance.Refresh(); 79 HiveClientWeb.StartJob((ex) => { throw ex; }, Job, CancellationToken.None); 70 var clientWeb = weblog.getClientWeb(UserId); 71 clientWeb.CurrentEnv = env; 72 clientWeb.Refresh(); 73 clientWeb.StartJob((ex) => { throw ex; }, Job, CancellationToken.None); 80 74 return Job; 81 75 }
Note: See TracChangeset
for help on using the changeset viewer.