Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • added Pause/Stop/Abort mechanisms to the slave
  • added Pause to Jobs
File size: 6.2 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;
29using HeuristicLab.Services.Hive.Common.DataTransfer;
30
31
32namespace HeuristicLab.Clients.Hive.Slave {
33  public class Executor : MarshalByRefObject, IDisposable {
34    public Guid JobId { get; set; }
35    public IJob Job { get; set; }
36    private bool wasJobAborted = false;
37    public Core Core { get; set; }
38
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    }
49
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
73        if (Job.CollectChildJobs) {
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) {
82        this.currentException = e;
83      }
84    }
85
86    public void Pause() {
87      Job.Pause();
88     
89    }
90
91    public void Stop() {
92      wasJobAborted = true;
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>();
118      foreach (IPluginDescription desc in PluginUtil.GetDeclaringPlugins(obj)) {
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();
130      childJob.JobState = JobState.Offline;
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;
142
143      JobData jdata = new JobData();
144      jdata.Data = PersistenceUtil.Serialize(Job);
145      jdata.JobId = this.JobId;
146
147      Core.PauseWaitJob(jdata);
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;
158      Core.SendFinishedJob(JobId);
159    }
160
161    private void Job_JobStopped(object sender, EventArgs e) {
162      if (wasJobAborted) {
163        Core.KillAppDomain(JobId);
164      } else {
165        Core.SendFinishedJob(JobId);
166      }
167    }
168
169    public JobData GetFinishedJob() {
170      if (Job == null) {
171        throw new InvalidStateException("Job is null");
172      }
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) {
180        throw new InvalidStateException("Job is still running");
181      } else {
182        JobData jdata = new JobData();
183        jdata.Data = PersistenceUtil.Serialize(Job);
184        jdata.JobId = JobId;
185        return jdata;
186      }
187    }
188
189    public Executor() {
190    }
191
192    public void Dispose() {
193      if (Job != null)
194        DeregisterJobEvents();
195      Job = null;
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.