Free cookie consent management tool by TermsFeed Policy Generator

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

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

various updates (#467)

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