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