Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs @ 11128

Last change on this file since 11128 was 10130, checked in by ascheibe, 11 years ago

#2117

  • fixed drag and drop check in the RefreshableHiveJobView to be more generic
  • fixed TaskDownloader to generate the correct HiveTasks when downloading tasks
  • fixed some variable names (job => task) which were forgotten
  • the IsParallelizable property of a task can now be set (e.g. if a task gets parallelized, the generated child tasks should sometimes not be able to get further parallelized)
File size: 3.9 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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
22using System;
23using System.Collections.Generic;
24using System.Linq;
[9219]25using System.Threading;
[6976]26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive {
[9219]29  public class TaskDownloader : IDisposable {
[6976]30    private IEnumerable<Guid> taskIds;
31    private ConcurrentTaskDownloader<ItemTask> taskDownloader;
32    private IDictionary<Guid, HiveTask> results;
33    private bool exceptionOccured = false;
34    private Exception currentException;
[7115]35    private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim();
[6976]36
37    public bool IsFinished {
38      get {
[9219]39        try {
40          resultsLock.EnterReadLock();
41          return results.Count == taskIds.Count();
42        }
43        finally { resultsLock.ExitReadLock(); }
[6976]44      }
45    }
46
47    public bool IsFaulted {
48      get {
49        return exceptionOccured;
50      }
51    }
52
53    public Exception Exception {
54      get {
55        return currentException;
56      }
57    }
[7020]58
[6976]59    public int FinishedCount {
60      get {
[9219]61        try {
62          resultsLock.EnterReadLock();
63          return results.Count;
64        }
65        finally { resultsLock.ExitReadLock(); }
[6976]66      }
67    }
68
69    public IDictionary<Guid, HiveTask> Results {
70      get {
[9219]71        try {
72          resultsLock.EnterReadLock();
73          return results;
74        }
75        finally { resultsLock.ExitReadLock(); }
[6976]76      }
77    }
78
79    public TaskDownloader(IEnumerable<Guid> jobIds) {
[10130]80      taskIds = jobIds;
81      taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
82      taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
83      results = new Dictionary<Guid, HiveTask>();
[6976]84    }
[7020]85
[6976]86    public void StartAsync() {
87      foreach (Guid taskId in taskIds) {
[7115]88        taskDownloader.DownloadTaskDataAndTask(taskId,
[10130]89          (localTask, itemTask) => {
90            if (localTask != null && itemTask != null) {
91              HiveTask hiveTask = itemTask.CreateHiveTask();
92              hiveTask.Task = localTask;
[7115]93              try {
94                resultsLock.EnterWriteLock();
[10130]95                results.Add(localTask.Id, hiveTask);
[9219]96              }
97              finally { resultsLock.ExitWriteLock(); }
[6976]98            }
99          });
[7020]100      }
[6976]101    }
102
103    private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
104      OnExceptionOccured(e.Value);
105    }
106
107    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
108    private void OnExceptionOccured(Exception exception) {
[10130]109      exceptionOccured = true;
110      currentException = exception;
[6976]111      var handler = ExceptionOccured;
112      if (handler != null) handler(this, new EventArgs<Exception>(exception));
113    }
[9219]114
115    #region IDisposable Members
116    public void Dispose() {
117      taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
118      resultsLock.Dispose();
119      taskDownloader.Dispose();
120    }
121    #endregion
[6976]122  }
123}
Note: See TracBrowser for help on using the repository browser.