1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading;
|
---|
6 | using HeuristicLab.Hive.Contracts;
|
---|
7 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
8 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
9 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
10 | using HeuristicLab.Common;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|