Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/Executor.cs @ 5280

Last change on this file since 5280 was 5137, checked in by ascheibe, 14 years ago

#1233

  • more tests for the hive slave
  • implemented a better way to write tests for the slave
  • get rid of MessageTypes for core and executor
  • various improvements in core and executor (better communication, bugfixes,...)
File size: 6.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Hive.Slave;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Hive;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Services.Hive.Common.DataTransfer;
31
32
33namespace HeuristicLab.Clients.Hive.Salve {
34  public class Executor : MarshalByRefObject, IDisposable {
35    public Guid JobId { get; set; }
36    public IJob Job { get; set; }
37    private bool wasJobAborted = false;
38    public Core core { get; set; }
39
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    }
50
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
74        if (Job.CollectChildJobs) {
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) {
83        this.currentException = e;
84      }
85    }
86
87    public void StartOnlyJob() {
88      try {
89        Job.Start();
90      }
91      catch (Exception e) {
92        this.currentException = e;
93      }
94    }
95
96    public void Abort() {
97      wasJobAborted = true;
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>();
123      foreach (IPluginDescription desc in PluginUtil.GetDeclaringPlugins(obj)) {
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;
147
148      JobData jdata = new JobData();
149      jdata.Data = PersistenceUtil.Serialize(Job);
150      jdata.JobId = this.JobId;
151
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) {
167      if (wasJobAborted) {
168        core.KillAppDomain(JobId);
169      } else {
170        core.SendFinishedJob(JobId);
171      }
172    }
173
174    public JobData GetFinishedJob() {
175      if (Job == null) {
176        throw new InvalidStateException("Job is null");
177      }
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) {
185        throw new InvalidStateException("Job is still running");
186      } else {
187        JobData jdata = new JobData();
188        jdata.Data = PersistenceUtil.Serialize(Job);
189        jdata.JobId = JobId;
190        return jdata;
191      }
192    }
193
194    public Executor() {
195    }
196
197    public void Dispose() {
198      if (Job != null)
199        DeregisterJobEvents();
200      Job = null;
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.