Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs @ 16007

Last change on this file since 16007 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.9 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
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        }
43        finally { resultsLock.ExitReadLock(); }
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    }
58
59    public int FinishedCount {
60      get {
61        try {
62          resultsLock.EnterReadLock();
63          return results.Count;
64        }
65        finally { resultsLock.ExitReadLock(); }
66      }
67    }
68
69    public IDictionary<Guid, HiveTask> Results {
70      get {
71        try {
72          resultsLock.EnterReadLock();
73          return results;
74        }
75        finally { resultsLock.ExitReadLock(); }
76      }
77    }
78
79    public TaskDownloader(IEnumerable<Guid> jobIds) {
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>();
84    }
85
86    public void StartAsync() {
87      foreach (Guid taskId in taskIds) {
88        taskDownloader.DownloadTaskDataAndTask(taskId,
89          (localTask, itemTask) => {
90            if (localTask != null && itemTask != null) {
91              HiveTask hiveTask = itemTask.CreateHiveTask();
92              hiveTask.Task = localTask;
93              try {
94                resultsLock.EnterWriteLock();
95                results.Add(localTask.Id, hiveTask);
96              }
97              finally { resultsLock.ExitWriteLock(); }
98            }
99          });
100      }
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) {
109      exceptionOccured = true;
110      currentException = exception;
111      var handler = ExceptionOccured;
112      if (handler != null) handler(this, new EventArgs<Exception>(exception));
113    }
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
122  }
123}
Note: See TracBrowser for help on using the repository browser.