Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Engine/3.2/Job.cs @ 3484

Last change on this file since 3484 was 3472, checked in by gkronber, 15 years ago

Implemented quick fix for #987 (HiveEngine doesn't raise the JobFailed event if an exception occurs in the underlying engine)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.Core;
26using System.Threading;
27using HeuristicLab.Hive.JobBase;
28using System.Xml;
29using HeuristicLab.Data;
30using HeuristicLab.Common;
31
32namespace HeuristicLab.Hive.Engine {
33  /// <summary>
34  /// Represents an job that wraps an engine that should be executed in the hive.
35  /// </summary>
36  public class Job : StorableBase, IJob {
37    private IEngine engine;
38    public IEngine Engine {
39      get { return engine; }
40    }
41
42    public Job()
43      : base() {
44      engine = new SequentialEngine.SequentialEngine();
45      engine.Priority = ThreadPriority.Lowest;
46      RegisterEvents();
47    }
48
49    private void RegisterEvents() {
50      engine.Finished += new EventHandler(engine_Finished);
51      engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(engine_ExceptionOccurred);
52    }
53
54    private void DeregisterEvents() {
55      engine.Finished -= new EventHandler(engine_Finished);
56      engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(engine_ExceptionOccurred);
57    }
58
59    void engine_Finished(object sender, EventArgs e) {
60      if (Engine.Canceled) this.progress = 0.0;
61      else this.progress = 1.0;
62      var listeners = JobStopped;
63      if (listeners != null)
64        listeners(this, e);
65    }
66
67    void engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
68      this.progress = 0.0;
69      var listeners = JobFailed;
70      if (listeners != null)
71        listeners(this, e);
72    }
73
74
75    #region IJob Members
76
77    public event EventHandler JobStopped;
78    public event EventHandler JobFailed;
79
80    public long JobId {
81      get;
82      set;
83    }
84
85    private double progress;
86    public double Progress {
87      get {
88        DoubleData progress = Engine.GlobalScope.GetVariableValue<DoubleData>("Progress", false,
89          false);
90        return progress == null ? this.progress : progress.Data;
91      }
92      set { throw new NotSupportedException(); }
93    }
94
95    public bool Running {
96      get {
97        return Engine.Running;
98      }
99      set {
100        throw new NotSupportedException();
101      }
102    }
103
104    public void Run() {
105      throw new NotSupportedException();
106    }
107
108    public void Start() {
109      Engine.Execute();
110    }
111
112    public void Stop() {
113      Engine.Abort();
114    }
115
116    #endregion
117
118    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
119      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
120      XmlAttribute idAttr = document.CreateAttribute("JobId");
121      idAttr.Value = XmlConvert.ToString(JobId);
122      XmlAttribute progressAttr = document.CreateAttribute("Progress");
123      progressAttr.Value = XmlConvert.ToString(progress);
124      node.Attributes.Append(idAttr);
125      node.Attributes.Append(progressAttr);
126      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
127      return node;
128    }
129    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
130      base.Populate(node, restoredObjects);
131      JobId = XmlConvert.ToInt64(node.Attributes["JobId"].Value);
132      progress = XmlConvert.ToDouble(node.Attributes["Progress"].Value);
133      DeregisterEvents();
134      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
135      RegisterEvents();
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.