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