Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.ExperimentManager/3.3/JobResultPoller.cs @ 5707

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

#1260

  • some changes due to the removal of Disposable (r5706)
  • copy PluginInfrastructure files into PluginCache folder in slaves (needed due to r5703)
File size: 4.6 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Hive.Contracts;
26using HeuristicLab.Hive.Contracts.BusinessObjects;
27using HeuristicLab.Hive.Contracts.Interfaces;
28using HeuristicLab.Hive.Contracts.ResponseObjects;
29
30namespace HeuristicLab.Hive.ExperimentManager {
31  internal class JobResultPoller {
32    private bool stopRequested { get; set; }
33    private AutoResetEvent waitHandle;
34    private Thread thread;
35
36    private HiveJob hiveJob;
37    public HiveJob HiveJob {
38      get { return hiveJob; }
39      set { hiveJob = value; }
40    }
41
42    private TimeSpan interval;
43    public TimeSpan Interval {
44      get { return interval; }
45      set { interval = value; }
46    }
47
48    private bool isPolling;
49    public bool IsPolling {
50      get { return isPolling; }
51      set {
52        if (isPolling != value) {
53          isPolling = value;
54          OnIsPollingChanged();
55        }
56      }
57    }
58
59    public JobResultPoller(HiveJob hiveJob, TimeSpan interval) {
60      this.isPolling = false;
61      this.hiveJob = hiveJob;
62      this.interval = interval;
63    }
64
65    public void Start() {
66      stopRequested = false;
67      thread = new Thread(RunPolling);
68      thread.Start();
69      IsPolling = true;
70    }
71
72    public void Stop() {
73      // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
74      stopRequested = true;
75      waitHandle.Set();
76      IsPolling = false;
77      thread = null;
78    }
79
80    public void RunPolling() {
81      try {
82        waitHandle = new AutoResetEvent(false);
83        while (!stopRequested) {
84          OnPollingStarted();
85          FetchJobResults();
86          OnPollingFinished();
87          waitHandle.WaitOne(Interval);
88        }
89        waitHandle.Close();
90      }
91      catch (Exception e) {
92        OnExceptionOccured(e);
93        IsPolling = false;
94      }
95    }
96
97    private void FetchJobResults() {
98      int repetitions = 5;
99      ResponseObject<JobResultList> response = null;
100      while (response == null && repetitions > 0) {
101        repetitions--;
102        try {
103          using (DisposableWrapper<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) {
104            response = service.Obj.GetChildJobResults(hiveJob.JobDto.Id, true, true);
105          }
106        }
107        catch (Exception e) {
108          if (repetitions == 0)
109            throw e;
110        }
111      }
112      if (response.StatusMessage == ResponseStatus.Ok) {
113        OnJobResultsReceived(response.Obj);
114      } else {
115        throw new JobResultPollingException(response.StatusMessage.ToString());
116      }
117    }
118
119    public event EventHandler<EventArgs<JobResultList>> JobResultsReceived;
120    private void OnJobResultsReceived(JobResultList jobResults) {
121      var handler = JobResultsReceived;
122      if (handler != null) handler(this, new EventArgs<JobResultList>(jobResults));
123    }
124
125    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
126    private void OnExceptionOccured(Exception e) {
127      var handler = ExceptionOccured;
128      if (handler != null) handler(this, new EventArgs<Exception>(e));
129    }
130
131    public event EventHandler IsPollingChanged;
132    private void OnIsPollingChanged() {
133      var handler = IsPollingChanged;
134      if (handler != null) handler(this, EventArgs.Empty);
135    }
136    public event EventHandler PollingStarted;
137    private void OnPollingStarted() {
138      var handler = PollingStarted;
139      if (handler != null) handler(this, EventArgs.Empty);
140    }
141    public event EventHandler PollingFinished;
142    private void OnPollingFinished() {
143      var handler = PollingFinished;
144      if (handler != null) handler(this, EventArgs.Empty);
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.