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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Hive.Contracts;
|
---|
26 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
27 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
28 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
29 | using HeuristicLab.Clients.Common;
|
---|
30 |
|
---|
31 | namespace 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 | IsPolling = false;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void FetchJobResults() {
|
---|
99 | int repetitions = 5;
|
---|
100 | ResponseObject<JobResultList> response = null;
|
---|
101 | while (response == null && repetitions > 0) {
|
---|
102 | repetitions--;
|
---|
103 | try {
|
---|
104 | using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) {
|
---|
105 | response = service.Obj.GetChildJobResults(hiveJob.JobDto.Id, true, true);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | catch (Exception e) {
|
---|
109 | if (repetitions == 0)
|
---|
110 | throw e;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if (response.StatusMessage == ResponseStatus.Ok) {
|
---|
114 | OnJobResultsReceived(response.Obj);
|
---|
115 | } else {
|
---|
116 | throw new JobResultPollingException(response.StatusMessage.ToString());
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public event EventHandler<EventArgs<JobResultList>> JobResultsReceived;
|
---|
121 | private void OnJobResultsReceived(JobResultList jobResults) {
|
---|
122 | var handler = JobResultsReceived;
|
---|
123 | if (handler != null) handler(this, new EventArgs<JobResultList>(jobResults));
|
---|
124 | }
|
---|
125 |
|
---|
126 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
127 | private void OnExceptionOccured(Exception e) {
|
---|
128 | var handler = ExceptionOccured;
|
---|
129 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
130 | }
|
---|
131 |
|
---|
132 | public event EventHandler IsPollingChanged;
|
---|
133 | private void OnIsPollingChanged() {
|
---|
134 | var handler = IsPollingChanged;
|
---|
135 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
136 | }
|
---|
137 | public event EventHandler PollingStarted;
|
---|
138 | private void OnPollingStarted() {
|
---|
139 | var handler = PollingStarted;
|
---|
140 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
141 | }
|
---|
142 | public event EventHandler PollingFinished;
|
---|
143 | private void OnPollingFinished() {
|
---|
144 | var handler = PollingFinished;
|
---|
145 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|