Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/JobResultPoller.cs @ 4423

Last change on this file since 4423 was 4423, checked in by cneumuel, 14 years ago
  • Refactored HL.Hive.Experiment. JobItems are not called HiveJobs and OptimizerJobs do not contain a hierarchy anymore.
  • Dynamic generation of jobs on a slave are not reflected on the client user interface.
  • Optimizer-Trees are now strictly synchronized with the HiveJob-Trees (also the ComputeInParallel property is taken into account when the Child HiveJobs are created)
  • Improved the way a class can report progress and lock the UI (IProgressReporter, IProgress, Progress, ProgressView)
  • Changes were made to the config-files, so that server and clients work with blade12.hpc.fh-hagenberg.at
  • Lots of small changes and bugfixes
File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using HeuristicLab.Hive.Contracts;
7using HeuristicLab.Hive.Contracts.Interfaces;
8using HeuristicLab.Hive.Contracts.BusinessObjects;
9using HeuristicLab.Hive.Contracts.ResponseObjects;
10using HeuristicLab.Common;
11
12namespace HeuristicLab.Hive.Experiment {
13  internal class JobResultPoller {
14    private bool stopRequested { get; set; }
15    private AutoResetEvent waitHandle;
16    private Thread thread;
17
18    private HiveJob hiveJob;
19    public HiveJob HiveJob {
20      get { return hiveJob; }
21      set { hiveJob = value; }
22    }
23
24    private TimeSpan interval;
25    public TimeSpan Interval {
26      get { return interval; }
27      set { interval = value; }
28    }
29
30    private bool isPolling;
31    public bool IsPolling {
32      get { return isPolling; }
33      set {
34        if (isPolling != value) {
35          isPolling = value;
36          OnIsPollingChanged();
37        }
38      }
39    }
40
41    public JobResultPoller(HiveJob hiveJob, TimeSpan interval) {
42      this.isPolling = false;
43      this.hiveJob = hiveJob;
44      this.interval = interval;
45    }
46
47    public void Start() {
48      stopRequested = false;
49      thread = new Thread(RunPolling);
50      thread.Start();
51      IsPolling = true;
52    }
53
54    public void Stop() {
55      // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
56      stopRequested = true;
57      waitHandle.Set();
58      IsPolling = false;
59      thread = null;
60    }
61
62    public void RunPolling() {
63      try {
64        waitHandle = new AutoResetEvent(false);
65        while (!stopRequested) {
66          OnPollingStarted();
67          FetchJobResults();
68          OnPollingFinished();
69          waitHandle.WaitOne(Interval);
70        }
71        waitHandle.Close();
72      }
73      catch (Exception e) {
74        OnExceptionOccured(e);
75      }
76    }
77
78    private void FetchJobResults() {
79      using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) {
80        ResponseObject<JobResultList> response = service.Obj.GetChildJobResults(hiveJob.JobDto.Id, true, true);
81        if (response.StatusMessage == ResponseStatus.Ok) {
82          OnJobResultsReceived(response.Obj);
83        } else {
84          throw new JobResultPollingException(response.StatusMessage.ToString());
85        }
86      }
87    }
88
89    public event EventHandler<EventArgs<JobResultList>> JobResultsReceived;
90    private void OnJobResultsReceived(JobResultList jobResults) {
91      var handler = JobResultsReceived;
92      if (handler != null) handler(this, new EventArgs<JobResultList>(jobResults));
93    }
94
95    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
96    private void OnExceptionOccured(Exception e) {
97      var handler = ExceptionOccured;
98      if (handler != null) handler(this, new EventArgs<Exception>(e));
99    }
100
101    public event EventHandler IsPollingChanged;
102    private void OnIsPollingChanged() {
103      var handler = IsPollingChanged;
104      if (handler != null) handler(this, EventArgs.Empty);
105    }
106    public event EventHandler PollingStarted;
107    private void OnPollingStarted() {
108      var handler = PollingStarted;
109      if (handler != null) handler(this, EventArgs.Empty);
110    }
111    public event EventHandler PollingFinished;
112    private void OnPollingFinished() {
113      var handler = PollingFinished;
114      if (handler != null) handler(this, EventArgs.Empty);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.