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

Last change on this file since 5718 was 5718, checked in by cneumuel, 11 years ago

#1233

  • fixed statelog when time on server differs from slave or client
  • fixed wrong creation of childjobs in experiment manager
  • made ganttchardview the default view for statelogs
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;
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.CoresNeeded = 1;
130      childJob.MemoryNeeded = 0;
131      childJob.PluginsNeededIds = FindPluginsNeeded(e.Value);
132
133      //TODO: is return value needed?
134      WcfService.Instance.AddChildJob(this.JobId, childJob, childJobData);
135    }
136
137    private void Job_WaitForChildJobs(object sender, EventArgs e) {
138      // Pause the job and send it back to the hive. The server will awake it when all child-jobs are finished
139      this.Job.CollectChildJobs = true;
140
141      JobData jdata = new JobData();
142      jdata.Data = PersistenceUtil.Serialize(Job);
143      jdata.JobId = this.JobId;
144
145      Core.PauseWaitJob(jdata);
146    }
147
148    private void Job_DeleteChildJobs(object sender, EventArgs e) {
149      WcfService.Instance.DeleteChildJobs(JobId);
150    }
151
152    private void Job_JobFailed(object sender, EventArgs e) {
153      //TODO: get exception to client
154      HeuristicLab.Common.EventArgs<Exception> ex = (HeuristicLab.Common.EventArgs<Exception>)e;
155      currentException = ex.Value;
156      Core.EnqueueExecutorMessage(Core.SendFinishedJob, JobId);
157    }
158
159    private void Job_JobStopped(object sender, EventArgs e) {
160      if (wasJobAborted) {
161        Core.EnqueueExecutorMessage(Core.KillAppDomain, JobId);
162      } else {
163        Core.EnqueueExecutorMessage(Core.SendFinishedJob, JobId);
164      }
165    }
166
167    public JobData GetFinishedJob() {
168      if (Job == null) {
169        throw new InvalidStateException("Job is null");
170      }
171
172      if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
173        try { Job.Stop(); }
174        catch { }
175      }
176
177      if (Job.ExecutionState == HeuristicLab.Core.ExecutionState.Started) {
178        throw new InvalidStateException("Job is still running");
179      } else {
180        JobData jdata = new JobData();
181        jdata.Data = PersistenceUtil.Serialize(Job);
182        jdata.JobId = JobId;
183        return jdata;
184      }
185    }
186
187    public Executor() {
188    }
189
190    public void Dispose() {
191      if (Job != null)
192        DeregisterJobEvents();
193      Job = null;
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.