Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.ExecutionEngine/3.3/Executor.cs @ 4141

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

merged with changes from Hive-3.2

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.Start();
64    }
65
66    void Job_JobFailed(object sender, EventArgs e) {
67      HeuristicLab.Common.EventArgs<Exception> ex = (HeuristicLab.Common.EventArgs<Exception>)e;
68      CurrentException = ex.Value;
69      Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
70    }
71
72    public void StartOnlyJob() {
73      Job.Start();
74    }
75
76    public void Abort() {
77      CurrentMessage = MessageContainer.MessageType.AbortJob;
78      Job.Stop();
79    }
80
81    void Job_JobStopped(object sender, EventArgs e) {
82      if (CurrentMessage == MessageContainer.MessageType.NoMessage) {
83        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
84        JobIsFinished = true;
85      } else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot) {
86        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
87      } else if (CurrentMessage == MessageContainer.MessageType.AbortJob) {
88        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
89      }
90    }
91
92    public byte[] GetSnapshot() {
93      //if the job is still running, something went VERY bad.
94      if (Job.ExecutionState == Core.ExecutionState.Started) {
95        return null;
96      } else {
97        // Clear the Status message
98        CurrentMessage = MessageContainer.MessageType.NoMessage;
99        // Pack the whole job inside an xml document
100        byte[] job = SerializeJobObject();
101        // Restart the job
102        // Return the Snapshot
103        return job;
104      }
105    }
106
107    public byte[] GetFinishedJob() {
108      //Job isn't finished!
109      if (Job.ExecutionState == Core.ExecutionState.Started) {
110        throw new InvalidStateException("Job is still running");
111      } else {
112        byte[] jobArr = SerializeJobObject();
113        return jobArr;
114      }
115    }
116
117    public void RequestSnapshot() {
118      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
119      Job.Stop();
120    }
121
122    private byte[] SerializeJobObject() {
123      MemoryStream stream = new MemoryStream();
124      XmlGenerator.Serialize(Job, stream);
125      return stream.ToArray();
126    }
127
128    private void RestoreJobObject(byte[] sjob) {
129      Job = XmlParser.Deserialize<IJob>(new MemoryStream(sjob));
130    }
131
132    public Executor() {
133      CurrentMessage = MessageContainer.MessageType.NoMessage;
134      JobIsFinished = false;
135      //Job = new TestJob();
136    }
137
138    #region IDisposable Members
139
140    public void Dispose() {
141      Job.JobFailed -= new EventHandler(Job_JobFailed);
142      Job.JobFailed -= new EventHandler(Job_JobStopped);
143      Queue = null;
144      Job = null;
145    }
146
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.