1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Collections.Generic;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Clients.Hive {
|
---|
28 | public class JobResultPoller {
|
---|
29 | private bool stopRequested;
|
---|
30 | private AutoResetEvent waitHandle;
|
---|
31 | private Thread thread;
|
---|
32 |
|
---|
33 | private Guid jobId;
|
---|
34 | public Guid JobId {
|
---|
35 | get { return jobId; }
|
---|
36 | set { jobId = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private TimeSpan interval;
|
---|
40 | public TimeSpan Interval {
|
---|
41 | get { return interval; }
|
---|
42 | set { interval = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private bool isPolling;
|
---|
46 | public bool IsPolling {
|
---|
47 | get { return isPolling; }
|
---|
48 | set {
|
---|
49 | if (isPolling != value) {
|
---|
50 | isPolling = value;
|
---|
51 | OnIsPollingChanged();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private bool autoResumeOnException = false;
|
---|
57 | public bool AutoResumeOnException {
|
---|
58 | get { return autoResumeOnException; }
|
---|
59 | set { autoResumeOnException = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | public JobResultPoller(Guid jobId, TimeSpan interval) {
|
---|
64 | this.isPolling = false;
|
---|
65 | this.jobId = jobId;
|
---|
66 | this.interval = interval;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void Start() {
|
---|
70 | IsPolling = true;
|
---|
71 | stopRequested = false;
|
---|
72 | thread = new Thread(RunPolling);
|
---|
73 | thread.Start();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void Stop() {
|
---|
77 | stopRequested = true;
|
---|
78 | waitHandle.Set();
|
---|
79 | IsPolling = false;
|
---|
80 | thread = null;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void RunPolling() {
|
---|
84 | IsPolling = true;
|
---|
85 | while (true) {
|
---|
86 | try {
|
---|
87 | waitHandle = new AutoResetEvent(false);
|
---|
88 | while (!stopRequested) {
|
---|
89 | OnPollingStarted();
|
---|
90 | FetchJobResults();
|
---|
91 | OnPollingFinished();
|
---|
92 | waitHandle.WaitOne(Interval);
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (stopRequested) {
|
---|
96 | waitHandle.Close();
|
---|
97 | IsPolling = false;
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | catch (Exception e) {
|
---|
102 | OnExceptionOccured(e);
|
---|
103 | if (!autoResumeOnException) {
|
---|
104 | waitHandle.Close();
|
---|
105 | IsPolling = false;
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public IEnumerable<LightweightTask> FetchJobResults() {
|
---|
113 | return HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
114 | var responses = new List<LightweightTask>();
|
---|
115 | responses.AddRange(service.GetLightweightJobTasks(jobId));
|
---|
116 | OnJobResultsReceived(responses);
|
---|
117 | return responses;
|
---|
118 | });
|
---|
119 | }
|
---|
120 |
|
---|
121 | public event EventHandler<EventArgs<IEnumerable<LightweightTask>>> JobResultsReceived;
|
---|
122 | private void OnJobResultsReceived(IEnumerable<LightweightTask> lightweightJobs) {
|
---|
123 | var handler = JobResultsReceived;
|
---|
124 | if (handler != null) handler(this, new EventArgs<IEnumerable<LightweightTask>>(lightweightJobs));
|
---|
125 | }
|
---|
126 |
|
---|
127 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
128 | private void OnExceptionOccured(Exception e) {
|
---|
129 | var handler = ExceptionOccured;
|
---|
130 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
131 | }
|
---|
132 |
|
---|
133 | public event EventHandler IsPollingChanged;
|
---|
134 | private void OnIsPollingChanged() {
|
---|
135 | var handler = IsPollingChanged;
|
---|
136 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
137 | }
|
---|
138 | public event EventHandler PollingStarted;
|
---|
139 | private void OnPollingStarted() {
|
---|
140 | var handler = PollingStarted;
|
---|
141 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
142 | }
|
---|
143 | public event EventHandler PollingFinished;
|
---|
144 | private void OnPollingFinished() {
|
---|
145 | var handler = PollingFinished;
|
---|
146 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|