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 @ 5054

Last change on this file since 5054 was 4914, checked in by cneumuel, 14 years ago

#1260

  • implemented single sign on with services
  • fixed plugin-deployment
  • removed hive-specific userConfig sections
  • minor bugfixes
File size: 4.3 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;
29using HeuristicLab.Clients.Common;
30
31namespace HeuristicLab.Hive.ExperimentManager {
32  internal class JobResultPoller {
33    private bool stopRequested { get; set; }
34    private AutoResetEvent waitHandle;
35    private Thread thread;
36
37    private HiveJob hiveJob;
38    public HiveJob HiveJob {
39      get { return hiveJob; }
40      set { hiveJob = value; }
41    }
42
43    private TimeSpan interval;
44    public TimeSpan Interval {
45      get { return interval; }
46      set { interval = value; }
47    }
48
49    private bool isPolling;
50    public bool IsPolling {
51      get { return isPolling; }
52      set {
53        if (isPolling != value) {
54          isPolling = value;
55          OnIsPollingChanged();
56        }
57      }
58    }
59
60    public JobResultPoller(HiveJob hiveJob, TimeSpan interval) {
61      this.isPolling = false;
62      this.hiveJob = hiveJob;
63      this.interval = interval;
64    }
65
66    public void Start() {
67      stopRequested = false;
68      thread = new Thread(RunPolling);
69      thread.Start();
70      IsPolling = true;
71    }
72
73    public void Stop() {
74      // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
75      stopRequested = true;
76      waitHandle.Set();
77      IsPolling = false;
78      thread = null;
79    }
80
81    public void RunPolling() {
82      try {
83        waitHandle = new AutoResetEvent(false);
84        while (!stopRequested) {
85          OnPollingStarted();
86          FetchJobResults();
87          OnPollingFinished();
88          waitHandle.WaitOne(Interval);
89        }
90        waitHandle.Close();
91      }
92      catch (Exception e) {
93        OnExceptionOccured(e);
94      }
95    }
96
97    private void FetchJobResults() {
98      ResponseObject<JobResultList> response;
99      using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) {
100        response = service.Obj.GetChildJobResults(hiveJob.JobDto.Id, true, true);
101      }
102      if (response.StatusMessage == ResponseStatus.Ok) {
103        OnJobResultsReceived(response.Obj);
104      } else {
105        throw new JobResultPollingException(response.StatusMessage.ToString());
106      }
107    }
108
109    public event EventHandler<EventArgs<JobResultList>> JobResultsReceived;
110    private void OnJobResultsReceived(JobResultList jobResults) {
111      var handler = JobResultsReceived;
112      if (handler != null) handler(this, new EventArgs<JobResultList>(jobResults));
113    }
114
115    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
116    private void OnExceptionOccured(Exception e) {
117      var handler = ExceptionOccured;
118      if (handler != null) handler(this, new EventArgs<Exception>(e));
119    }
120
121    public event EventHandler IsPollingChanged;
122    private void OnIsPollingChanged() {
123      var handler = IsPollingChanged;
124      if (handler != null) handler(this, EventArgs.Empty);
125    }
126    public event EventHandler PollingStarted;
127    private void OnPollingStarted() {
128      var handler = PollingStarted;
129      if (handler != null) handler(this, EventArgs.Empty);
130    }
131    public event EventHandler PollingFinished;
132    private void OnPollingFinished() {
133      var handler = PollingFinished;
134      if (handler != null) handler(this, EventArgs.Empty);
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.