[5156] | 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;
|
---|
[5062] | 23 | using System.Collections.Generic;
|
---|
[5156] | 24 | using System.Diagnostics;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[5062] | 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 virtual bool IsParallelizable {
|
---|
| 36 | get { return false; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [Storable]
|
---|
| 40 | protected bool computeInParallel;
|
---|
| 41 | public bool ComputeInParallel {
|
---|
| 42 | get { return computeInParallel; }
|
---|
| 43 | set {
|
---|
| 44 | if (computeInParallel != value) {
|
---|
| 45 | computeInParallel = value;
|
---|
| 46 | OnComputeInParallelChanged();
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [Storable]
|
---|
| 52 | private int indexInParentOptimizerList = -1;
|
---|
| 53 | public int IndexInParentOptimizerList {
|
---|
| 54 | get { return indexInParentOptimizerList; }
|
---|
| 55 | set { this.indexInParentOptimizerList = value; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [Storable]
|
---|
| 59 | private bool collectChildJobs;
|
---|
| 60 | public bool CollectChildJobs {
|
---|
| 61 | get { return collectChildJobs; }
|
---|
| 62 | set { collectChildJobs = value; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private int ms = 1000; //ms
|
---|
[5106] | 66 | private bool spinWait;
|
---|
| 67 | public MockJob(int ms, bool spinWait) {
|
---|
[5062] | 68 | this.ms = ms;
|
---|
[5106] | 69 | this.spinWait = spinWait;
|
---|
[5062] | 70 | }
|
---|
| 71 | public MockJob() { }
|
---|
| 72 | [StorableConstructor]
|
---|
| 73 | protected MockJob(bool deserializing) { }
|
---|
[5106] | 74 | protected MockJob(MockJob original, Cloner cloner)
|
---|
| 75 | : base(original, cloner) {
|
---|
[5062] | 76 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
| 77 | this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
|
---|
| 78 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
| 79 | this.ExecutionTime = original.executionTime;
|
---|
| 80 | this.ExecutionState = original.executionState;
|
---|
| 81 | this.ms = original.ms;
|
---|
| 82 | }
|
---|
| 83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 84 | return new MockJob(this, cloner);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | [Storable]
|
---|
| 88 | private ExecutionState executionState;
|
---|
| 89 | public virtual ExecutionState ExecutionState {
|
---|
| 90 | get { return executionState; }
|
---|
| 91 | set {
|
---|
| 92 | if (executionState != value) {
|
---|
| 93 | this.executionState = value;
|
---|
| 94 | OnExecutionStateChanged();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | [Storable]
|
---|
| 100 | private TimeSpan executionTime;
|
---|
| 101 | public TimeSpan ExecutionTime {
|
---|
| 102 | get { return executionTime; }
|
---|
| 103 | set {
|
---|
| 104 | if (executionTime != value) {
|
---|
| 105 | executionTime = value;
|
---|
| 106 | OnExecutionTimeChanged();
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public virtual void Prepare() {
|
---|
[5314] | 112 | this.ExecutionState = HeuristicLab.Core.ExecutionState.Prepared;
|
---|
[5062] | 113 | }
|
---|
| 114 |
|
---|
| 115 | public virtual void Start() {
|
---|
[5106] | 116 | new Thread(Run).Start();
|
---|
[5062] | 117 | }
|
---|
| 118 |
|
---|
[5106] | 119 | private void Run() {
|
---|
| 120 | try {
|
---|
| 121 | if (spinWait) {
|
---|
| 122 | Stopwatch watch = new Stopwatch();
|
---|
| 123 | watch.Start();
|
---|
| 124 | do {
|
---|
| 125 | Thread.SpinWait(1000);
|
---|
| 126 | } while (watch.ElapsedMilliseconds < ms);
|
---|
| 127 | watch.Stop();
|
---|
| 128 | } else {
|
---|
| 129 | Thread.Sleep(ms);
|
---|
| 130 | }
|
---|
| 131 | Stop();
|
---|
| 132 | OnJobStopped();
|
---|
| 133 | }
|
---|
[5137] | 134 | catch (ThreadAbortException) {
|
---|
| 135 | //this happens when the appdomain is killed (e.g. abort from server)
|
---|
| 136 | Stop();
|
---|
| 137 | }
|
---|
[5106] | 138 | catch (Exception e) {
|
---|
[5314] | 139 | this.ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
|
---|
[5137] | 140 | OnJobFailed(e);
|
---|
[5106] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[5062] | 144 | public virtual void Pause() {
|
---|
[5314] | 145 | this.ExecutionState = HeuristicLab.Core.ExecutionState.Paused;
|
---|
[5062] | 146 | }
|
---|
| 147 |
|
---|
| 148 | public virtual void Stop() {
|
---|
[5314] | 149 | ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
|
---|
[5062] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public virtual void Resume(IEnumerable<IJob> childJobs) {
|
---|
[5314] | 153 | ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
|
---|
[5062] | 154 | }
|
---|
| 155 |
|
---|
| 156 | #region Events
|
---|
| 157 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
| 158 | protected virtual void OnNewChildJob(IJob job) {
|
---|
| 159 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
| 160 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | public event EventHandler WaitForChildJobs;
|
---|
| 164 | protected virtual void OnWaitForChildJobs() {
|
---|
| 165 | EventHandler handler = WaitForChildJobs;
|
---|
| 166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | public event EventHandler DeleteChildJobs;
|
---|
| 170 | protected virtual void OnDeleteChildJobs() {
|
---|
| 171 | EventHandler handler = DeleteChildJobs;
|
---|
| 172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public event EventHandler ComputeInParallelChanged;
|
---|
| 176 | protected virtual void OnComputeInParallelChanged() {
|
---|
| 177 | EventHandler handler = ComputeInParallelChanged;
|
---|
| 178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public event EventHandler ExecutionTimeChanged;
|
---|
| 182 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 183 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | public event EventHandler ExecutionStateChanged;
|
---|
| 188 | protected virtual void OnExecutionStateChanged() {
|
---|
| 189 | EventHandler handler = ExecutionStateChanged;
|
---|
| 190 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 191 | }
|
---|
[5156] | 192 |
|
---|
[5104] | 193 | public event EventHandler JobFailed;
|
---|
[5137] | 194 | protected virtual void OnJobFailed(Exception e) {
|
---|
[5104] | 195 | EventHandler handler = JobFailed;
|
---|
[5156] | 196 | EventArgs<Exception> ev = new EventArgs<Exception>(e);
|
---|
[5137] | 197 | if (handler != null) handler(this, ev);
|
---|
[5104] | 198 | }
|
---|
| 199 |
|
---|
| 200 | public event EventHandler JobStopped;
|
---|
| 201 | protected virtual void OnJobStopped() {
|
---|
| 202 | EventHandler handler = JobStopped;
|
---|
| 203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[5062] | 206 | #endregion
|
---|
| 207 | }
|
---|
| 208 | }
|
---|