Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs @ 14927

Last change on this file since 14927 was 14927, checked in by gkronber, 7 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive {
29  public class TaskDownloader : IDisposable {
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;
35    private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim();
36
37    public bool IsFinished {
38      get {
39        try {
40          resultsLock.EnterReadLock();
41          return results.Count == taskIds.Count();
42        } finally { resultsLock.ExitReadLock(); }
43      }
44    }
45
46    public bool IsFaulted {
47      get {
48        return exceptionOccured;
49      }
50    }
51
52    public Exception Exception {
53      get {
54        return currentException;
55      }
56    }
57
58    public int FinishedCount {
59      get {
60        try {
61          resultsLock.EnterReadLock();
62          return results.Count;
63        } finally { resultsLock.ExitReadLock(); }
64      }
65    }
66
67    public IDictionary<Guid, HiveTask> Results {
68      get {
69        try {
70          resultsLock.EnterReadLock();
71          return results;
72        } finally { resultsLock.ExitReadLock(); }
73      }
74    }
75
76    public TaskDownloader(IEnumerable<Guid> jobIds) {
77      taskIds = jobIds;
78      taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
79      taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
80      results = new Dictionary<Guid, HiveTask>();
81    }
82
83    public void StartAsync() {
84      foreach (Guid taskId in taskIds) {
85        taskDownloader.DownloadTaskDataAndTask(taskId,
86          (localTask, itemTask) => {
87            if (localTask != null && itemTask != null) {
88              HiveTask hiveTask = itemTask.CreateHiveTask();
89              hiveTask.Task = localTask;
90              try {
91                resultsLock.EnterWriteLock();
92                results.Add(localTask.Id, hiveTask);
93              } finally { resultsLock.ExitWriteLock(); }
94            }
95          });
96      }
97    }
98
99    private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
100      OnExceptionOccured(e.Value);
101    }
102
103    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
104    private void OnExceptionOccured(Exception exception) {
105      exceptionOccured = true;
106      currentException = exception;
107      var handler = ExceptionOccured;
108      if (handler != null) handler(this, new EventArgs<Exception>(exception));
109    }
110
111    #region IDisposable Members
112    public void Dispose() {
113      taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
114      resultsLock.Dispose();
115      taskDownloader.Dispose();
116    }
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.