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