Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3962 was 1812, checked in by kgrading, 16 years ago

changed job restart behaviour - the job will now be restarted when the serialization is done, not before (#571)

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 StartOnlyJob() {
68      Job.Start();
69    }
70
71    public void Abort() {
72      CurrentMessage = MessageContainer.MessageType.AbortJob;
73      Job.Stop();     
74    }
75
76    void Job_JobStopped(object sender, EventArgs e) {
77      if (CurrentMessage == MessageContainer.MessageType.NoMessage)
78        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
79      else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
80        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
81      else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
82        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
83    }
84
85    public byte[] GetSnapshot() {
86      //if the job is still running, something went VERY bad.
87      if (Job.Running) {
88        return null;
89      } else {
90        // Clear the Status message
91        CurrentMessage = MessageContainer.MessageType.NoMessage;
92        // Pack the whole job inside an xml document
93        byte[] job = SerializeJobObject();       
94        // Restart the job
95        // Return the Snapshot
96        return job;
97      }
98    }
99
100    public byte[] GetFinishedJob() {
101      //Job isn't finished!
102      if (Job.Running) {
103        throw new InvalidStateException("Job is still running");
104      } else {
105        return SerializeJobObject();
106      }
107    }
108
109
110    public void RequestSnapshot() {
111      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
112      Job.Stop();
113    }
114
115    private byte[] SerializeJobObject() {
116      return PersistenceManager.SaveToGZip(Job);
117    }
118
119    private void RestoreJobObject(byte[] sjob) {
120      Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
121    }
122
123    public Executor() {
124      CurrentMessage = MessageContainer.MessageType.NoMessage;
125      Job = new TestJob();
126    }   
127  }
128}
Note: See TracBrowser for help on using the repository browser.