Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.ExecutionEngine/3.3/Executor.cs @ 4247

Last change on this file since 4247 was 4170, checked in by cneumuel, 14 years ago

refactoring of Result-Polling of HiveExperiment, polling is now much faster and code is cleaner (1092#)

File size: 4.8 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 HeuristicLab.Hive.Client.Common;
24using HeuristicLab.Hive.Contracts;
25using HeuristicLab.Hive.JobBase;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Common;
28using System.Xml.Serialization;
29using HeuristicLab.Persistence.Default.Xml;
30using System.IO;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.Hive.Client.ExecutionEngine {
34  public class Executor : MarshalByRefObject, IDisposable {
35    public Guid JobId { get; set; }
36    public IJob Job { get; set; }
37    public MessageContainer.MessageType CurrentMessage { get; set; }
38    public Exception CurrentException { get; set; }
39    public MessageQueue Queue { get; set; }
40
41    public bool JobIsFinished { get; set; }
42
43    public ExecutionState ExecutionState {
44      get {
45        return Job.ExecutionState;
46      }
47    }
48
49    public double Progress {
50      get {
51        return Job.Progress;
52      }
53    }
54
55    public DateTime CreationTime { get; set; }
56
57    public void Start(byte[] serializedJob) {
58      CreationTime = DateTime.Now;
59      Job = XmlParser.Deserialize<IJob>(new MemoryStream(serializedJob));
60
61      Job.JobStopped += new EventHandler(Job_JobStopped);
62      Job.JobFailed += new EventHandler(Job_JobFailed);
63      Job.Prepare();
64      Job.Start();
65    }
66
67    void Job_JobFailed(object sender, EventArgs e) {
68      HeuristicLab.Common.EventArgs<Exception> ex = (HeuristicLab.Common.EventArgs<Exception>)e;
69      CurrentException = ex.Value;
70      Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
71    }
72
73    public void StartOnlyJob() {
74      Job.Start();
75    }
76
77    public void Abort() {
78      CurrentMessage = MessageContainer.MessageType.AbortJob;
79      Job.Stop();
80    }
81
82    void Job_JobStopped(object sender, EventArgs e) {
83      if (CurrentMessage == MessageContainer.MessageType.NoMessage) {
84        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
85        JobIsFinished = true;
86      } else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot) {
87        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
88      } else if (CurrentMessage == MessageContainer.MessageType.AbortJob) {
89        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
90      }
91    }
92
93    public byte[] GetSnapshot() {
94      //if the job is still running, something went VERY bad.
95      if (Job.ExecutionState == Core.ExecutionState.Started) {
96        return null;
97      } else {
98        // Clear the Status message
99        CurrentMessage = MessageContainer.MessageType.NoMessage;
100        // Pack the whole job inside an xml document
101        byte[] job = SerializeJobObject();
102        // Restart the job
103        // Return the Snapshot
104        return job;
105      }
106    }
107
108    public byte[] GetFinishedJob() {
109      //Job isn't finished!
110      if (Job.ExecutionState == Core.ExecutionState.Started) {
111        throw new InvalidStateException("Job is still running");
112      } else {
113        byte[] jobArr = SerializeJobObject();
114        return jobArr;
115      }
116    }
117
118    public void RequestSnapshot() {
119      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
120      Job.Stop();
121    }
122
123    private byte[] SerializeJobObject() {
124      MemoryStream stream = new MemoryStream();
125      XmlGenerator.Serialize(Job, stream);
126      return stream.ToArray();
127    }
128
129    private void RestoreJobObject(byte[] sjob) {
130      Job = XmlParser.Deserialize<IJob>(new MemoryStream(sjob));
131    }
132
133    public Executor() {
134      CurrentMessage = MessageContainer.MessageType.NoMessage;
135      JobIsFinished = false;
136      //Job = new TestJob();
137    }
138
139    #region IDisposable Members
140
141    public void Dispose() {
142      Job.JobFailed -= new EventHandler(Job_JobFailed);
143      Job.JobFailed -= new EventHandler(Job_JobStopped);
144      Queue = null;
145      Job = null;
146    }
147
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.