Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • added ItemView and Item for the Slave
  • added a Tray Icon App for data visualization and control of the slave windows service
  • added control methods to SlaveCommunication for controlling the slave core
  • fixed typo in namespace
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.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 StartOnlyJob() {
87      try {
88        Job.Start();
89      }
90      catch (Exception e) {
91        this.currentException = e;
92      }
93    }
94
95    public void Pause() {
96      Job.Pause();
97    }
98
99    public void Abort() {
100      wasJobAborted = true;
101      if ((ExecutionState == ExecutionState.Started) || (ExecutionState == ExecutionState.Paused)) {
102        Job.Stop();
103      } else {
104        Job_JobStopped(this, EventArgs.Empty);
105      }
106    }
107
108    private void RegisterJobEvents() {
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 void DeregisterJobEvents() {
117      Job.JobStopped -= new EventHandler(Job_JobStopped);
118      Job.JobFailed -= new EventHandler(Job_JobFailed);
119      Job.NewChildJob -= new EventHandler<EventArgs<IJob>>(Job_NewChildJob);
120      Job.WaitForChildJobs -= new EventHandler(Job_WaitForChildJobs);
121      Job.DeleteChildJobs -= new EventHandler(Job_DeleteChildJobs);
122    }
123
124    private List<Guid> FindPluginsNeeded(IJob obj) {
125      List<Guid> guids = new List<Guid>();
126      foreach (IPluginDescription desc in PluginUtil.GetDeclaringPlugins(obj)) {
127      }
128      throw new NotImplementedException("FindPluginsNeeded for Job_NewChildJob");
129
130      return guids;
131    }
132
133    private void Job_NewChildJob(object sender, EventArgs<IJob> e) {
134      JobData childJobData = new JobData();
135      childJobData.Data = PersistenceUtil.Serialize(e.Value);
136
137      Job childJob = new Job();
138      childJob.JobState = JobState.Offline;
139      childJob.CoresNeeded = 1;
140      childJob.MemoryNeeded = 0;
141      childJob.PluginsNeededIds = FindPluginsNeeded(e.Value);
142
143      //TODO: is return value needed?
144      WcfService.Instance.AddChildJob(this.JobId, childJob, childJobData);
145    }
146
147    private void Job_WaitForChildJobs(object sender, EventArgs e) {
148      // Pause the job and send it back to the hive. The server will awake it when all child-jobs are finished
149      this.Job.CollectChildJobs = true;
150
151      JobData jdata = new JobData();
152      jdata.Data = PersistenceUtil.Serialize(Job);
153      jdata.JobId = this.JobId;
154
155      core.PauseJob(jdata);
156    }
157
158    private void Job_DeleteChildJobs(object sender, EventArgs e) {
159      WcfService.Instance.DeleteChildJobs(JobId);
160    }
161
162    private void Job_JobFailed(object sender, EventArgs e) {
163      //TODO: get exception to client
164      HeuristicLab.Common.EventArgs<Exception> ex = (HeuristicLab.Common.EventArgs<Exception>)e;
165      currentException = ex.Value;
166      core.SendFinishedJob(JobId);
167    }
168
169    private void Job_JobStopped(object sender, EventArgs e) {
170      if (wasJobAborted) {
171        core.KillAppDomain(JobId);
172      } else {
173        core.SendFinishedJob(JobId);
174      }
175    }
176
177    public JobData GetFinishedJob() {
178      if (Job == null) {
179        throw new InvalidStateException("Job is null");
180      }
181
182      if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
183        try { Job.Stop(); }
184        catch { }
185      }
186
187      if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
188        throw new InvalidStateException("Job is still running");
189      } else {
190        JobData jdata = new JobData();
191        jdata.Data = PersistenceUtil.Serialize(Job);
192        jdata.JobId = JobId;
193        return jdata;
194      }
195    }
196
197    public Executor() {
198    }
199
200    public void Dispose() {
201      if (Job != null)
202        DeregisterJobEvents();
203      Job = null;
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.