[5105] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
[5137] | 23 | using System.Collections.Generic;
|
---|
[5105] | 24 | using System.Linq;
|
---|
[5137] | 25 | using HeuristicLab.Clients.Hive.Slave;
|
---|
[5105] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Hive;
|
---|
[5137] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[5105] | 30 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 31 |
|
---|
[5137] | 32 |
|
---|
[5105] | 33 | namespace HeuristicLab.Clients.Hive.Salve {
|
---|
| 34 | public class Executor : MarshalByRefObject, IDisposable {
|
---|
| 35 | public Guid JobId { get; set; }
|
---|
| 36 | public IJob Job { get; set; }
|
---|
[5137] | 37 | private bool wasJobAborted = false;
|
---|
| 38 | public Core core { get; set; }
|
---|
| 39 |
|
---|
[5105] | 40 | private Exception currentException;
|
---|
| 41 | public String CurrentException {
|
---|
| 42 | get {
|
---|
| 43 | if (currentException != null) {
|
---|
| 44 | return currentException.ToString();
|
---|
| 45 | } else {
|
---|
| 46 | return string.Empty;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[5137] | 50 |
|
---|
[5105] | 51 | public ExecutionState ExecutionState {
|
---|
| 52 | get {
|
---|
| 53 | return Job != null ? Job.ExecutionState : HeuristicLab.Core.ExecutionState.Stopped;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public TimeSpan ExecutionTime {
|
---|
| 58 | get {
|
---|
| 59 | return Job != null ? Job.ExecutionTime : new TimeSpan(0, 0, 0);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public DateTime CreationTime { get; set; }
|
---|
| 64 |
|
---|
| 65 | /// <param name="serializedJob"></param>
|
---|
| 66 | /// <param name="collectChildJobs">if true, all child-jobs are downloaded and the job will be resumed.</param>
|
---|
| 67 | public void Start(byte[] serializedJob) {
|
---|
| 68 | try {
|
---|
| 69 | CreationTime = DateTime.Now;
|
---|
| 70 | Job = PersistenceUtil.Deserialize<IJob>(serializedJob);
|
---|
| 71 |
|
---|
| 72 | RegisterJobEvents();
|
---|
| 73 |
|
---|
[5137] | 74 | if (Job.CollectChildJobs) {
|
---|
[5105] | 75 | IEnumerable<JobData> childjobs = WcfService.Instance.GetChildJobs(JobId);
|
---|
| 76 | Job.Resume(childjobs.Select(j => PersistenceUtil.Deserialize<IJob>(j.Data)));
|
---|
| 77 | } else {
|
---|
| 78 | Job.Prepare();
|
---|
| 79 | Job.Start();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | catch (Exception e) {
|
---|
[5137] | 83 | this.currentException = e;
|
---|
[5105] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void StartOnlyJob() {
|
---|
| 88 | try {
|
---|
| 89 | Job.Start();
|
---|
| 90 | }
|
---|
| 91 | catch (Exception e) {
|
---|
[5137] | 92 | this.currentException = e;
|
---|
[5105] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public void Abort() {
|
---|
[5137] | 97 | wasJobAborted = true;
|
---|
[5105] | 98 | if ((ExecutionState == ExecutionState.Started) || (ExecutionState == ExecutionState.Paused)) {
|
---|
| 99 | Job.Stop();
|
---|
| 100 | } else {
|
---|
| 101 | Job_JobStopped(this, EventArgs.Empty);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private void RegisterJobEvents() {
|
---|
| 106 | Job.JobStopped += new EventHandler(Job_JobStopped);
|
---|
| 107 | Job.JobFailed += new EventHandler(Job_JobFailed);
|
---|
| 108 | Job.NewChildJob += new EventHandler<EventArgs<IJob>>(Job_NewChildJob);
|
---|
| 109 | Job.WaitForChildJobs += new EventHandler(Job_WaitForChildJobs);
|
---|
| 110 | Job.DeleteChildJobs += new EventHandler(Job_DeleteChildJobs);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private void DeregisterJobEvents() {
|
---|
| 114 | Job.JobStopped -= new EventHandler(Job_JobStopped);
|
---|
| 115 | Job.JobFailed -= new EventHandler(Job_JobFailed);
|
---|
| 116 | Job.NewChildJob -= new EventHandler<EventArgs<IJob>>(Job_NewChildJob);
|
---|
| 117 | Job.WaitForChildJobs -= new EventHandler(Job_WaitForChildJobs);
|
---|
| 118 | Job.DeleteChildJobs -= new EventHandler(Job_DeleteChildJobs);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private List<Guid> FindPluginsNeeded(IJob obj) {
|
---|
| 122 | List<Guid> guids = new List<Guid>();
|
---|
[5137] | 123 | foreach (IPluginDescription desc in PluginUtil.GetDeclaringPlugins(obj)) {
|
---|
[5105] | 124 | }
|
---|
| 125 | throw new NotImplementedException("FindPluginsNeeded for Job_NewChildJob");
|
---|
| 126 |
|
---|
| 127 | return guids;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | private void Job_NewChildJob(object sender, EventArgs<IJob> e) {
|
---|
| 131 | JobData childJobData = new JobData();
|
---|
| 132 | childJobData.Data = PersistenceUtil.Serialize(e.Value);
|
---|
| 133 |
|
---|
| 134 | Job childJob = new Job();
|
---|
| 135 | childJob.JobState = JobState.Offline;
|
---|
| 136 | childJob.CoresNeeded = 1;
|
---|
| 137 | childJob.MemoryNeeded = 0;
|
---|
| 138 | childJob.PluginsNeededIds = FindPluginsNeeded(e.Value);
|
---|
| 139 |
|
---|
| 140 | //TODO: is return value needed?
|
---|
| 141 | WcfService.Instance.AddChildJob(this.JobId, childJob, childJobData);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private void Job_WaitForChildJobs(object sender, EventArgs e) {
|
---|
| 145 | // Pause the job and send it back to the hive. The server will awake it when all child-jobs are finished
|
---|
| 146 | this.Job.CollectChildJobs = true;
|
---|
[5137] | 147 |
|
---|
[5105] | 148 | JobData jdata = new JobData();
|
---|
| 149 | jdata.Data = PersistenceUtil.Serialize(Job);
|
---|
| 150 | jdata.JobId = this.JobId;
|
---|
[5137] | 151 |
|
---|
[5105] | 152 | core.PauseJob(jdata);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void Job_DeleteChildJobs(object sender, EventArgs e) {
|
---|
| 156 | WcfService.Instance.DeleteChildJobs(JobId);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | private void Job_JobFailed(object sender, EventArgs e) {
|
---|
| 160 | //TODO: get exception to client
|
---|
| 161 | HeuristicLab.Common.EventArgs<Exception> ex = (HeuristicLab.Common.EventArgs<Exception>)e;
|
---|
| 162 | currentException = ex.Value;
|
---|
| 163 | core.SendFinishedJob(JobId);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private void Job_JobStopped(object sender, EventArgs e) {
|
---|
[5137] | 167 | if (wasJobAborted) {
|
---|
| 168 | core.KillAppDomain(JobId);
|
---|
[5105] | 169 | } else {
|
---|
| 170 | core.SendFinishedJob(JobId);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public JobData GetFinishedJob() {
|
---|
| 175 | if (Job == null) {
|
---|
[5137] | 176 | throw new InvalidStateException("Job is null");
|
---|
| 177 | }
|
---|
[5105] | 178 |
|
---|
| 179 | if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
|
---|
| 180 | try { Job.Stop(); }
|
---|
| 181 | catch { }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
|
---|
[5137] | 185 | throw new InvalidStateException("Job is still running");
|
---|
[5105] | 186 | } else {
|
---|
| 187 | JobData jdata = new JobData();
|
---|
| 188 | jdata.Data = PersistenceUtil.Serialize(Job);
|
---|
| 189 | jdata.JobId = JobId;
|
---|
| 190 | return jdata;
|
---|
| 191 | }
|
---|
[5137] | 192 | }
|
---|
| 193 |
|
---|
[5105] | 194 | public Executor() {
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | public void Dispose() {
|
---|
| 198 | if (Job != null)
|
---|
| 199 | DeregisterJobEvents();
|
---|
| 200 | Job = null;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|