Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.ExecutionEngine/3.2/Executor.cs @ 1655

Last change on this file since 1655 was 1655, checked in by kgrading, 15 years ago

added proper exception handling (#601)

File size: 4.0 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 {
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    public bool Running {
41      get {
42        return Job.Running;
43      }
44    }
45    public double Progress {
46      get {
47        return Job.Progress;
48      }
49    }
50
51    public DateTime CreationTime { get; set; }
52
53    public void Start(byte[] serializedJob) {
54      CreationTime = DateTime.Now;
55      Job = (IJob)PersistenceManager.RestoreFromGZip(serializedJob);
56      //debug
57      //Job = new TestJob();
58      Job.JobStopped += new EventHandler(Job_JobStopped);
59      Job.JobFailed += new EventHandler(Job_JobFailed);
60      Job.Start();
61    }
62
63    void Job_JobFailed(object sender, EventArgs e) {
64      Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobFailed, JobId));
65    }
66
67    public void Abort() {
68      CurrentMessage = MessageContainer.MessageType.AbortJob;
69      Job.Stop();     
70    }
71
72    void Job_JobStopped(object sender, EventArgs e) {
73      if (CurrentMessage == MessageContainer.MessageType.NoMessage)
74        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
75      else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
76        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
77      else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
78        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
79    }
80
81    public byte[] GetSnapshot() {
82      //if the job is still running, something went VERY bad.
83      if (Job.Running) {
84        return null;
85      } else {
86        // Clear the Status message
87        CurrentMessage = MessageContainer.MessageType.NoMessage;
88        // Pack the whole job inside an xml document
89        byte[] job = SerializeJobObject();       
90        // Restart the job
91        Job.Start();
92        // Return the Snapshot
93        return job;
94      }
95    }
96
97    public byte[] GetFinishedJob() {
98      //Job isn't finished!
99      if (Job.Running) {
100        throw new InvalidStateException("Job is still running");
101      } else {
102        return SerializeJobObject();
103      }
104    }
105
106
107    public void RequestSnapshot() {
108      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
109      Job.Stop();
110    }
111
112    private byte[] SerializeJobObject() {
113      return PersistenceManager.SaveToGZip(Job);
114    }
115
116    private void RestoreJobObject(byte[] sjob) {
117      Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
118    }
119
120    public Executor() {
121      CurrentMessage = MessageContainer.MessageType.NoMessage;
122      Job = new TestJob();
123    }   
124  }
125}
Note: See TracBrowser for help on using the repository browser.