1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using System.Drawing;
|
---|
10 | using System.Threading;
|
---|
11 | using System.Diagnostics;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
14 | [Item("Mock Job", "Represents mocked Job which just SpinWaits.")]
|
---|
15 | [StorableClass]
|
---|
16 | public class MockJob : NamedItem, IJob {
|
---|
17 | public virtual bool IsParallelizable {
|
---|
18 | get { return false; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | [Storable]
|
---|
22 | protected bool computeInParallel;
|
---|
23 | public bool ComputeInParallel {
|
---|
24 | get { return computeInParallel; }
|
---|
25 | set {
|
---|
26 | if (computeInParallel != value) {
|
---|
27 | computeInParallel = value;
|
---|
28 | OnComputeInParallelChanged();
|
---|
29 | }
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | [Storable]
|
---|
34 | private int indexInParentOptimizerList = -1;
|
---|
35 | public int IndexInParentOptimizerList {
|
---|
36 | get { return indexInParentOptimizerList; }
|
---|
37 | set { this.indexInParentOptimizerList = value; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private bool collectChildJobs;
|
---|
42 | public bool CollectChildJobs {
|
---|
43 | get { return collectChildJobs; }
|
---|
44 | set { collectChildJobs = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private int ms = 1000; //ms
|
---|
48 | private bool spinWait;
|
---|
49 | public MockJob(int ms, bool spinWait) {
|
---|
50 | this.ms = ms;
|
---|
51 | this.spinWait = spinWait;
|
---|
52 | }
|
---|
53 | public MockJob() { }
|
---|
54 | [StorableConstructor]
|
---|
55 | protected MockJob(bool deserializing) { }
|
---|
56 | protected MockJob(MockJob original, Cloner cloner)
|
---|
57 | : base(original, cloner) {
|
---|
58 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
59 | this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
|
---|
60 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
61 | this.ExecutionTime = original.executionTime;
|
---|
62 | this.ExecutionState = original.executionState;
|
---|
63 | this.ms = original.ms;
|
---|
64 | }
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new MockJob(this, cloner);
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | private ExecutionState executionState;
|
---|
71 | public virtual ExecutionState ExecutionState {
|
---|
72 | get { return executionState; }
|
---|
73 | set {
|
---|
74 | if (executionState != value) {
|
---|
75 | this.executionState = value;
|
---|
76 | OnExecutionStateChanged();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [Storable]
|
---|
82 | private TimeSpan executionTime;
|
---|
83 | public TimeSpan ExecutionTime {
|
---|
84 | get { return executionTime; }
|
---|
85 | set {
|
---|
86 | if (executionTime != value) {
|
---|
87 | executionTime = value;
|
---|
88 | OnExecutionTimeChanged();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public virtual void Prepare() {
|
---|
94 | this.ExecutionState = Core.ExecutionState.Prepared;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public virtual void Start() {
|
---|
98 | new Thread(Run).Start();
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void Run() {
|
---|
102 | try {
|
---|
103 | if (spinWait) {
|
---|
104 | Stopwatch watch = new Stopwatch();
|
---|
105 | watch.Start();
|
---|
106 | do {
|
---|
107 | Thread.SpinWait(1000);
|
---|
108 | } while (watch.ElapsedMilliseconds < ms);
|
---|
109 | watch.Stop();
|
---|
110 | } else {
|
---|
111 | Thread.Sleep(ms);
|
---|
112 | }
|
---|
113 | Stop();
|
---|
114 | OnJobStopped();
|
---|
115 | }
|
---|
116 | catch (ThreadAbortException) {
|
---|
117 | //this happens when the appdomain is killed (e.g. abort from server)
|
---|
118 | Stop();
|
---|
119 | }
|
---|
120 | catch (Exception e) {
|
---|
121 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
122 | OnJobFailed(e);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public virtual void Pause() {
|
---|
127 | this.ExecutionState = Core.ExecutionState.Paused;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public virtual void Stop() {
|
---|
131 | ExecutionState = Core.ExecutionState.Stopped;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
135 | ExecutionState = Core.ExecutionState.Stopped;
|
---|
136 | }
|
---|
137 |
|
---|
138 | #region Events
|
---|
139 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
140 | protected virtual void OnNewChildJob(IJob job) {
|
---|
141 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
142 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
143 | }
|
---|
144 |
|
---|
145 | public event EventHandler WaitForChildJobs;
|
---|
146 | protected virtual void OnWaitForChildJobs() {
|
---|
147 | EventHandler handler = WaitForChildJobs;
|
---|
148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public event EventHandler DeleteChildJobs;
|
---|
152 | protected virtual void OnDeleteChildJobs() {
|
---|
153 | EventHandler handler = DeleteChildJobs;
|
---|
154 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public event EventHandler ComputeInParallelChanged;
|
---|
158 | protected virtual void OnComputeInParallelChanged() {
|
---|
159 | EventHandler handler = ComputeInParallelChanged;
|
---|
160 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public event EventHandler ExecutionTimeChanged;
|
---|
164 | protected virtual void OnExecutionTimeChanged() {
|
---|
165 | EventHandler handler = ExecutionTimeChanged;
|
---|
166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public event EventHandler ExecutionStateChanged;
|
---|
170 | protected virtual void OnExecutionStateChanged() {
|
---|
171 | EventHandler handler = ExecutionStateChanged;
|
---|
172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public event EventHandler JobFailed;
|
---|
176 | protected virtual void OnJobFailed(Exception e) {
|
---|
177 | EventHandler handler = JobFailed;
|
---|
178 | EventArgs<Exception> ev = new EventArgs<Exception>(e);
|
---|
179 | if (handler != null) handler(this, ev);
|
---|
180 | }
|
---|
181 |
|
---|
182 | public event EventHandler JobStopped;
|
---|
183 | protected virtual void OnJobStopped() {
|
---|
184 | EventHandler handler = JobStopped;
|
---|
185 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
186 | }
|
---|
187 |
|
---|
188 | #endregion
|
---|
189 | }
|
---|
190 | }
|
---|