Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Client.ExecutionEngine/3.2/Executor.cs @ 5955

Last change on this file since 5955 was 4140, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

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