Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HiveJobDownloader.cs @ 6373

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

#1233

  • moved ExperimentManager into separate plugin
  • moved Administration into separate plugin
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Clients.Hive.Jobs;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive {
29  public class HiveJobDownloader {
30    private IEnumerable<Guid> jobIds;
31    private ConcurrentJobDownloader<ItemJob> jobDownloader;
32    private IDictionary<Guid, HiveJob> results;
33    private bool exceptionOccured = false;
34    private Exception currentException;
35
36    public bool IsFinished {
37      get {
38        return results.Count == jobIds.Count();
39      }
40    }
41
42    public bool IsFaulted {
43      get {
44        return exceptionOccured;
45      }
46    }
47
48    public Exception Exception {
49      get {
50        return currentException;
51      }
52    }
53   
54    public int FinishedCount {
55      get {
56        return results.Count;
57      }
58    }
59
60    public IDictionary<Guid, HiveJob> Results {
61      get {
62        return results;
63      }
64    }
65
66    public HiveJobDownloader(IEnumerable<Guid> jobIds) {
67      this.jobIds = jobIds;
68      this.jobDownloader = new ConcurrentJobDownloader<ItemJob>(2, 2);
69      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
70      this.results = new Dictionary<Guid, HiveJob>();
71    }
72   
73    public void StartAsync() {
74      foreach (Guid jobId in jobIds) {
75        Job job = ServiceLocator.Instance.CallHiveService(s => s.GetJob(jobId));
76
77        jobDownloader.DownloadJob(job,
78          (localJob, itemJob) => {
79            if (localJob != null && itemJob != null) {
80              HiveJob hiveJob;
81              if (itemJob is OptimizerJob) {
82                hiveJob = new OptimizerHiveJob((OptimizerJob)itemJob);
83              } else {
84                hiveJob = new HiveJob(itemJob, true);
85              }
86              hiveJob.Job = localJob;
87              this.results.Add(localJob.Id, hiveJob);
88            }
89          });
90      }     
91    }
92
93    private void jobDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
94      OnExceptionOccured(e.Value);
95    }
96
97    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
98    private void OnExceptionOccured(Exception exception) {
99      this.exceptionOccured = true;
100      this.currentException = exception;
101      var handler = ExceptionOccured;
102      if (handler != null) handler(this, new EventArgs<Exception>(exception));
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.