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

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

added experiment plugins (#1115)

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