Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5599 was 5599, checked in by ascheibe, 13 years ago

#1233

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