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.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Hive;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
32 | [Item("Mock Job", "Represents mocked Job which just SpinWaits.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class MockJob : NamedItem, IJob {
|
---|
35 | public bool IsParallelizable {
|
---|
36 | get { return true; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public bool ComputeInParallel {
|
---|
40 | get { return true; }
|
---|
41 | set { throw new NotImplementedException(); }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public bool CollectChildJobs {
|
---|
45 | get { return false; }
|
---|
46 | set { throw new NotImplementedException(); }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [Storable]
|
---|
50 | private ExecutionState executionState;
|
---|
51 | public virtual ExecutionState ExecutionState {
|
---|
52 | get { return executionState; }
|
---|
53 | set {
|
---|
54 | if (executionState != value) {
|
---|
55 | this.executionState = value;
|
---|
56 | OnExecutionStateChanged();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Storable]
|
---|
62 | private TimeSpan executionTime;
|
---|
63 | public TimeSpan ExecutionTime {
|
---|
64 | get { return executionTime; }
|
---|
65 | set {
|
---|
66 | if (executionTime != value) {
|
---|
67 | executionTime = value;
|
---|
68 | OnExecutionTimeChanged();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | private int ms = 1000; //ms
|
---|
75 |
|
---|
76 | [Storable]
|
---|
77 | private bool spinWait;
|
---|
78 |
|
---|
79 | private bool failRequested = false;
|
---|
80 | private bool pauseRequested = false;
|
---|
81 | private bool stopRequested = false;
|
---|
82 |
|
---|
83 | public MockJob(int ms, bool spinWait) {
|
---|
84 | this.ms = ms;
|
---|
85 | this.spinWait = spinWait;
|
---|
86 | }
|
---|
87 | public MockJob() { }
|
---|
88 | [StorableConstructor]
|
---|
89 | protected MockJob(bool deserializing) { }
|
---|
90 | protected MockJob(MockJob original, Cloner cloner)
|
---|
91 | : base(original, cloner) {
|
---|
92 | this.ExecutionTime = original.executionTime;
|
---|
93 | this.ExecutionState = original.executionState;
|
---|
94 | this.ms = original.ms;
|
---|
95 | this.spinWait = original.spinWait;
|
---|
96 | }
|
---|
97 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
98 | return new MockJob(this, cloner);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public virtual void Prepare() {
|
---|
102 | this.ExecutionState = HeuristicLab.Core.ExecutionState.Prepared;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public virtual void Start() {
|
---|
106 | new Thread(Run).Start();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void Run() {
|
---|
110 | try {
|
---|
111 | OnJobStarted();
|
---|
112 |
|
---|
113 | Stopwatch watch = new Stopwatch();
|
---|
114 | watch.Start();
|
---|
115 | while (!stopRequested && !pauseRequested && !failRequested && watch.ElapsedMilliseconds < ms) {
|
---|
116 | if (spinWait) {
|
---|
117 | Thread.SpinWait(100);
|
---|
118 | this.ExecutionTime = watch.Elapsed;
|
---|
119 | } else {
|
---|
120 | Thread.Sleep(100);
|
---|
121 | this.ExecutionTime = watch.Elapsed;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (failRequested) {
|
---|
126 | ExecutionState = ExecutionState.Stopped;
|
---|
127 | OnJobFailed(new Exception("A mock exception!"));
|
---|
128 | } else if (pauseRequested) {
|
---|
129 | this.ExecutionState = ExecutionState.Paused;
|
---|
130 | OnJobPaused();
|
---|
131 | } else if (stopRequested) {
|
---|
132 | ExecutionState = ExecutionState.Stopped;
|
---|
133 | OnJobStopped();
|
---|
134 | } else {
|
---|
135 | Stop();
|
---|
136 | OnJobStopped();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | catch (ThreadAbortException) {
|
---|
140 | //this happens when the appdomain is killed (e.g. abort from server)
|
---|
141 | Stop();
|
---|
142 | }
|
---|
143 | catch (Exception e) {
|
---|
144 | this.ExecutionState = ExecutionState.Stopped;
|
---|
145 | OnJobFailed(e);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public virtual void Pause() {
|
---|
150 | pauseRequested = true;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public virtual void Stop() {
|
---|
154 | stopRequested = true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void Fail() {
|
---|
158 | failRequested = true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
162 | throw new NotImplementedException();
|
---|
163 | }
|
---|
164 |
|
---|
165 | #region Events
|
---|
166 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
167 | protected virtual void OnNewChildJob(IJob job) {
|
---|
168 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
169 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
170 | }
|
---|
171 |
|
---|
172 | public event EventHandler WaitForChildJobs;
|
---|
173 | protected virtual void OnWaitForChildJobs() {
|
---|
174 | EventHandler handler = WaitForChildJobs;
|
---|
175 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public event EventHandler DeleteChildJobs;
|
---|
179 | protected virtual void OnDeleteChildJobs() {
|
---|
180 | EventHandler handler = DeleteChildJobs;
|
---|
181 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public event EventHandler ComputeInParallelChanged;
|
---|
185 | protected virtual void OnComputeInParallelChanged() {
|
---|
186 | EventHandler handler = ComputeInParallelChanged;
|
---|
187 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public event EventHandler ExecutionTimeChanged;
|
---|
191 | protected virtual void OnExecutionTimeChanged() {
|
---|
192 | EventHandler handler = ExecutionTimeChanged;
|
---|
193 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
194 | }
|
---|
195 |
|
---|
196 | public event EventHandler ExecutionStateChanged;
|
---|
197 | protected virtual void OnExecutionStateChanged() {
|
---|
198 | EventHandler handler = ExecutionStateChanged;
|
---|
199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
200 | }
|
---|
201 |
|
---|
202 | public event EventHandler JobStarted;
|
---|
203 | protected virtual void OnJobStarted() {
|
---|
204 | EventHandler handler = JobStarted;
|
---|
205 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public event EventHandler JobFailed;
|
---|
209 | protected virtual void OnJobFailed(Exception e) {
|
---|
210 | EventHandler handler = JobFailed;
|
---|
211 | EventArgs<Exception> ev = new EventArgs<Exception>(e);
|
---|
212 | if (handler != null) handler(this, ev);
|
---|
213 | }
|
---|
214 |
|
---|
215 | public event EventHandler JobStopped;
|
---|
216 | protected virtual void OnJobStopped() {
|
---|
217 | EventHandler handler = JobStopped;
|
---|
218 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
219 | }
|
---|
220 |
|
---|
221 | public event EventHandler JobPaused;
|
---|
222 | protected virtual void OnJobPaused() {
|
---|
223 | EventHandler handler = JobPaused;
|
---|
224 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
225 | }
|
---|
226 | #endregion
|
---|
227 | }
|
---|
228 | }
|
---|