[771] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Hive.Client.Common;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using System.Xml.Serialization;
|
---|
| 29 | using System.IO;
|
---|
[1001] | 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Hive.JobBase;
|
---|
[793] | 32 | using HeuristicLab.Hive.Contracts;
|
---|
[771] | 33 |
|
---|
| 34 | namespace HeuristicLab.Hive.Client.ExecutionEngine {
|
---|
| 35 | public class Executor: MarshalByRefObject {
|
---|
[1449] | 36 | public Guid JobId { get; set; }
|
---|
[779] | 37 | public IJob Job { get; set; }
|
---|
[771] | 38 | public MessageContainer.MessageType CurrentMessage { get; set; }
|
---|
| 39 | public MessageQueue Queue { get; set; }
|
---|
[1097] | 40 | public bool Running {
|
---|
| 41 | get {
|
---|
| 42 | return Job.Running;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
[908] | 45 | public double Progress {
|
---|
| 46 | get {
|
---|
| 47 | return Job.Progress;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[771] | 50 |
|
---|
[908] | 51 | public DateTime CreationTime { get; set; }
|
---|
| 52 |
|
---|
| 53 | public void Start(byte[] serializedJob) {
|
---|
| 54 | CreationTime = DateTime.Now;
|
---|
[1132] | 55 | Job = (IJob)PersistenceManager.RestoreFromGZip(serializedJob);
|
---|
[1119] | 56 | //debug
|
---|
[1132] | 57 | //Job = new TestJob();
|
---|
[771] | 58 | Job.JobStopped += new EventHandler(Job_JobStopped);
|
---|
[1655] | 59 | Job.JobFailed += new EventHandler(Job_JobFailed);
|
---|
[771] | 60 | Job.Start();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1655] | 63 | void Job_JobFailed(object sender, EventArgs e) {
|
---|
| 64 | Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobFailed, JobId));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[1812] | 67 | public void StartOnlyJob() {
|
---|
| 68 | Job.Start();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[771] | 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 |
|
---|
[816] | 85 | public byte[] GetSnapshot() {
|
---|
[771] | 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
|
---|
[816] | 93 | byte[] job = SerializeJobObject();
|
---|
[771] | 94 | // Restart the job
|
---|
| 95 | // Return the Snapshot
|
---|
[779] | 96 | return job;
|
---|
[771] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[816] | 100 | public byte[] GetFinishedJob() {
|
---|
[771] | 101 | //Job isn't finished!
|
---|
| 102 | if (Job.Running) {
|
---|
[1368] | 103 | throw new InvalidStateException("Job is still running");
|
---|
[771] | 104 | } else {
|
---|
[779] | 105 | return SerializeJobObject();
|
---|
[771] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | public void RequestSnapshot() {
|
---|
| 111 | CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
|
---|
| 112 | Job.Stop();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[816] | 115 | private byte[] SerializeJobObject() {
|
---|
| 116 | return PersistenceManager.SaveToGZip(Job);
|
---|
[771] | 117 | }
|
---|
| 118 |
|
---|
[816] | 119 | private void RestoreJobObject(byte[] sjob) {
|
---|
| 120 | Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
|
---|
[771] | 121 | }
|
---|
| 122 |
|
---|
| 123 | public Executor() {
|
---|
| 124 | CurrentMessage = MessageContainer.MessageType.NoMessage;
|
---|
[816] | 125 | Job = new TestJob();
|
---|
[771] | 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|