Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockJob.cs @ 6110

Last change on this file since 6110 was 6110, checked in by cneumuel, 13 years ago

#1233

  • renamed engines to executors
  • changed locking in StartJobInAppDomain
  • avoid destruction of proxy object after 5 minutes for Slave.Core
  • added JobStarted event and fixed ExecutionStateChanged and ExecutionTimeChanged
  • slaves which are moved to another slavegroup will pause their jobs now, if they must not calculate them
File size: 6.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Hive;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Clients.Hive.SlaveCore.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
66    private bool spinWait;
67    public MockJob(int ms, bool spinWait) {
68      this.ms = ms;
69      this.spinWait = spinWait;
70    }
71    public MockJob() { }
72    [StorableConstructor]
73    protected MockJob(bool deserializing) { }
74    protected MockJob(MockJob original, Cloner cloner)
75      : base(original, cloner) {
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() {
112      this.ExecutionState = HeuristicLab.Core.ExecutionState.Prepared;
113    }
114
115    public virtual void Start() {
116      new Thread(Run).Start();
117    }
118
119    private void Run() {
120      try {
121        if (spinWait) {
122          Stopwatch watch = new Stopwatch();
123          watch.Start();
124          OnJobStarted();
125          do {
126            Thread.SpinWait(1000);
127          } while (watch.ElapsedMilliseconds < ms);
128          watch.Stop();
129        } else {
130          Thread.Sleep(ms);
131        }
132        Stop();
133        OnJobStopped();
134      }
135      catch (ThreadAbortException) {
136        //this happens when the appdomain is killed (e.g. abort from server)
137        Stop();
138      }
139      catch (Exception e) {
140        this.ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
141        OnJobFailed(e);
142      }
143    }
144
145    public virtual void Pause() {
146      this.ExecutionState = HeuristicLab.Core.ExecutionState.Paused;
147    }
148
149    public virtual void Stop() {
150      ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
151    }
152
153    public virtual void Resume(IEnumerable<IJob> childJobs) {
154      ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
155    }
156
157    #region Events
158    public event EventHandler<EventArgs<IJob>> NewChildJob;
159    protected virtual void OnNewChildJob(IJob job) {
160      EventHandler<EventArgs<IJob>> handler = NewChildJob;
161      if (handler != null) handler(this, new EventArgs<IJob>(job));
162    }
163
164    public event EventHandler WaitForChildJobs;
165    protected virtual void OnWaitForChildJobs() {
166      EventHandler handler = WaitForChildJobs;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler DeleteChildJobs;
171    protected virtual void OnDeleteChildJobs() {
172      EventHandler handler = DeleteChildJobs;
173      if (handler != null) handler(this, EventArgs.Empty);
174    }
175
176    public event EventHandler ComputeInParallelChanged;
177    protected virtual void OnComputeInParallelChanged() {
178      EventHandler handler = ComputeInParallelChanged;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
181
182    public event EventHandler ExecutionTimeChanged;
183    protected virtual void OnExecutionTimeChanged() {
184      EventHandler handler = ExecutionTimeChanged;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187
188    public event EventHandler ExecutionStateChanged;
189    protected virtual void OnExecutionStateChanged() {
190      EventHandler handler = ExecutionStateChanged;
191      if (handler != null) handler(this, EventArgs.Empty);
192    }
193
194    public event EventHandler JobStarted;
195    protected virtual void OnJobStarted() {
196      EventHandler handler = JobStarted;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199
200    public event EventHandler JobFailed;
201    protected virtual void OnJobFailed(Exception e) {
202      EventHandler handler = JobFailed;
203      EventArgs<Exception> ev = new EventArgs<Exception>(e);
204      if (handler != null) handler(this, ev);
205    }
206
207    public event EventHandler JobStopped;
208    protected virtual void OnJobStopped() {
209      EventHandler handler = JobStopped;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212
213    public event EventHandler JobPaused;
214    protected virtual void OnJobPaused() {
215      EventHandler handler = JobPaused;
216      if (handler != null) handler(this, EventArgs.Empty);
217    }
218    #endregion
219
220  }
221}
Note: See TracBrowser for help on using the repository browser.