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 @ 4107

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

migration from 3.2 to 3.3 completed. Hive Server and Client are now executable and as functional as they were in 3.2. (#1096)

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