Free cookie consent management tool by TermsFeed Policy Generator

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

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

added minor speedups and better transaction handling to the server (#828)

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