Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.ExperimentManager/3.3/HiveJobDownloader.cs @ 5179

Last change on this file since 5179 was 5179, checked in by cneumuel, 13 years ago

#1260

  • migrated to .NET 4.0
  • moved state-information about heartbeat timestamps into DB to reduce IIS-recycling issues
  • optimized memory usage of ExperimentManager when lots of large jobs are downloaded
File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.ExperimentManager.Jobs;
6using System.Threading;
7using System.Threading.Tasks;
8using HeuristicLab.Hive.Contracts.BusinessObjects;
9using HeuristicLab.Hive.Contracts.Interfaces;
10using HeuristicLab.Clients.Common;
11
12namespace HeuristicLab.Hive.ExperimentManager {
13  public class HiveJobDownloader {
14    private IEnumerable<Guid> jobIds;
15    private List<Task<HiveJob>> tasks;
16
17    public bool IsFinished {
18      get {
19        return tasks.TrueForAll(t => t.Status == TaskStatus.RanToCompletion ||
20                                     t.Status == TaskStatus.Faulted ||
21                                     t.Status == TaskStatus.Canceled);
22      }
23    }
24
25    public int FinishedCount {
26      get {
27        return tasks.Count(t => t.Status == TaskStatus.RanToCompletion ||
28                                t.Status == TaskStatus.Faulted ||
29                                t.Status == TaskStatus.Canceled);
30      }
31    }
32
33    public IDictionary<Guid, HiveJob> Results {
34      get {
35        var results = new Dictionary<Guid, HiveJob>();
36        foreach(var t in tasks) {
37          results.Add(t.Result.JobDto.Id, t.Result);
38        }
39        return results;
40      }
41    }
42
43    public HiveJobDownloader(IEnumerable<Guid> jobIds) {
44      this.jobIds = jobIds;
45    }
46
47   
48    public void StartAsync() {
49      tasks = new List<Task<HiveJob>>();
50      foreach (Guid jobId in jobIds) {
51        tasks.Add(Task<SerializedJob>.Factory.StartNew(
52          (x) => DownloadJob(x), jobId)
53          .ContinueWith((x) => DeserializeJob(x.Result)));
54      }
55    }
56    // use semaphore to ensure only few tasks download concurrently (so not too many connections are open)
57    private Semaphore taskSemaphore = new Semaphore(2, 2);
58    protected SerializedJob DownloadJob(object jobId) {
59      taskSemaphore.WaitOne(Timeout.Infinite);
60      using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) {
61        return service.Obj.GetLastSerializedResult((Guid)jobId).Obj;
62      }
63    }
64
65    protected HiveJob DeserializeJob(SerializedJob serializedJob) {
66      HiveJob job = new HiveJob(serializedJob, false);
67      job.Job.Prepare(); // reduce memory consumption.
68      taskSemaphore.Release();
69      job.JobDto = serializedJob.JobInfo;
70      return job;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.