Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/JobResultPoller.cs @ 6111

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

#1233

  • improved the way jobs are downloaded by ExperimentManager and HiveEngine
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Threading;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Clients.Hive {
28  public class JobResultPoller {
29    private bool stopRequested { get; set; }
30    private AutoResetEvent waitHandle;
31    private Thread thread;
32
33    private Guid hiveExperimentId;
34    public Guid HiveExperimentId {
35      get { return hiveExperimentId; }
36      set { hiveExperimentId = value; }
37    }
38
39    private TimeSpan interval;
40    public TimeSpan Interval {
41      get { return interval; }
42      set { interval = value; }
43    }
44
45    private bool isPolling;
46    public bool IsPolling {
47      get { return isPolling; }
48      set {
49        if (isPolling != value) {
50          isPolling = value;
51          OnIsPollingChanged();
52        }
53      }
54    }
55
56    public JobResultPoller(Guid hiveExperimentId, TimeSpan interval) {
57      this.isPolling = false;
58      this.hiveExperimentId = hiveExperimentId;
59      this.interval = interval;
60    }
61
62    public void Start() {
63      stopRequested = false;
64      thread = new Thread(RunPolling);
65      thread.Start();
66      IsPolling = true;
67    }
68
69    public void Stop() {
70      // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
71      stopRequested = true;
72      waitHandle.Set();
73      IsPolling = false;
74      thread = null;
75    }
76
77    private void RunPolling() {
78      try {
79        waitHandle = new AutoResetEvent(false);
80        while (!stopRequested) {
81          OnPollingStarted();
82          FetchJobResults();
83          OnPollingFinished();
84          waitHandle.WaitOne(Interval);
85        }
86        waitHandle.Close();
87      }
88      catch (Exception e) {
89        OnExceptionOccured(e);
90      }
91      finally {
92        IsPolling = false;
93      }
94    }
95
96    public IEnumerable<LightweightJob> FetchJobResults() {
97      return ServiceLocator.Instance.CallHiveService(service => {
98        var responses = new List<LightweightJob>();
99        responses.AddRange(service.GetLightweightExperimentJobs(hiveExperimentId));
100        OnJobResultsReceived(responses);
101        return responses;
102      });
103    }
104
105    public event EventHandler<EventArgs<IEnumerable<LightweightJob>>> JobResultsReceived;
106    private void OnJobResultsReceived(IEnumerable<LightweightJob> lightweightJobs) {
107      var handler = JobResultsReceived;
108      if (handler != null) handler(this, new EventArgs<IEnumerable<LightweightJob>>(lightweightJobs));
109    }
110
111    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
112    private void OnExceptionOccured(Exception e) {
113      var handler = ExceptionOccured;
114      if (handler != null) handler(this, new EventArgs<Exception>(e));
115    }
116
117    public event EventHandler IsPollingChanged;
118    private void OnIsPollingChanged() {
119      var handler = IsPollingChanged;
120      if (handler != null) handler(this, EventArgs.Empty);
121    }
122    public event EventHandler PollingStarted;
123    private void OnPollingStarted() {
124      var handler = PollingStarted;
125      if (handler != null) handler(this, EventArgs.Empty);
126    }
127    public event EventHandler PollingFinished;
128    private void OnPollingFinished() {
129      var handler = PollingFinished;
130      if (handler != null) handler(this, EventArgs.Empty);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.