Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5106 was 5106, checked in by cneumuel, 13 years ago

#1233

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