Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Hive.Client.ExecutionEngine/Executor.cs @ 1106

Last change on this file since 1106 was 908, checked in by kgrading, 16 years ago

implemented the Status method for the web interface (#401)

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