Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive/3.3/HiveTasks/HiveTask.cs @ 8228

Last change on this file since 8228 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 19.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.ComponentModel;
25using System.Drawing;
26using System.Linq;
27using System.Threading;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.Hive {
35
36  [Item("Hive Task", "Represents a hive task.")]
37  [StorableClass]
38  public class HiveTask : NamedItem, IItemTree<HiveTask> {
39    protected static object locker = new object();
40    protected ReaderWriterLockSlim childHiveTasksLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
41
42    public static new Image StaticItemImage {
43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
44    }
45    public override Image ItemImage {
46      get {
47        if (task.Id == Guid.Empty) { // not yet uploaded
48          return HeuristicLab.Common.Resources.VSImageLibrary.Event;
49        } else {
50          if (task.State == TaskState.Waiting) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
51          else if (task.State == TaskState.Calculating) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
52          else if (task.State == TaskState.Transferring) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
53          else if (task.State == TaskState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
54          else if (task.State == TaskState.Aborted) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
55          else if (task.State == TaskState.Failed) return HeuristicLab.Common.Resources.VSImageLibrary.Error;
56          else if (task.State == TaskState.Finished) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
57          else return base.ItemImage;
58        }
59      }
60    }
61
62    [Storable]
63    protected Task task;
64    public Task Task {
65      get { return task; }
66      set {
67        if (task != value) {
68          DeregisterJobEvents();
69          task = value;
70          RegisterJobEvents();
71          IsFinishedTaskDownloaded = false;
72          OnTaskChanged();
73          OnToStringChanged();
74          OnItemImageChanged();
75        }
76      }
77    }
78
79    [Storable]
80    protected ItemTask itemTask;
81    public ItemTask ItemTask {
82      get { return itemTask; }
83      set {
84        if (itemTask != null && syncTasksWithOptimizers) {
85          this.childHiveTasks.Clear();
86        }
87        if (itemTask != value) {
88          DergisterItemTaskEvents();
89          itemTask = value;
90          RegisterItemTaskEvents();
91          OnItemTaskChanged();
92          IsFinishedTaskDownloaded = true;
93        }
94      }
95    }
96
97    // task downloaded since last status change
98    [Storable]
99    private bool isFinishedTaskDownloaded = false;
100    public bool IsFinishedTaskDownloaded {
101      get { return isFinishedTaskDownloaded; }
102      set {
103        if (value != isFinishedTaskDownloaded) {
104          this.isFinishedTaskDownloaded = value;
105          OnIsFinishedJobDownloadedChanged();
106        }
107      }
108    }
109
110    public bool IsDownloading { get; set; }
111
112    // if true, all control buttons should be enabled. otherwise disabled
113    private bool isControllable = true;
114    public bool IsControllable {
115      get { return isControllable; }
116      set {
117        if (value != isControllable) {
118          isControllable = value;
119          OnIsControllableChanged();
120          childHiveTasksLock.EnterReadLock();
121          try {
122            foreach (var hiveJob in childHiveTasks) {
123              hiveJob.IsControllable = value;
124            }
125          }
126          finally {
127            childHiveTasksLock.ExitReadLock();
128          }
129        }
130      }
131    }
132
133    [Storable]
134    protected ItemList<HiveTask> childHiveTasks;
135    public virtual ReadOnlyItemList<HiveTask> ChildHiveTasks {
136      get {
137        childHiveTasksLock.EnterReadLock();
138        try {
139          return childHiveTasks.AsReadOnly();
140        }
141        finally { childHiveTasksLock.ExitReadLock(); }
142      }
143    }
144
145    [Storable]
146    protected bool syncTasksWithOptimizers = true;
147
148    public StateLogList StateLog {
149      get {
150        var list = new StateLogList(this.task.StateLog);
151        list.ForEach(s => { s.TaskName = itemTask.Name; });
152        return list;
153      }
154    }
155
156    public StateLogListList ChildStateLogList {
157      get { return new StateLogListList(this.childHiveTasks.Select(x => x.StateLog)); }
158    }
159
160    #region Constructors and Cloning
161    [StorableConstructor]
162    protected HiveTask(bool deserializing) { }
163
164    public HiveTask() {
165      this.Task = new Task() { CoresNeeded = 1, MemoryNeeded = 128 };
166      task.State = TaskState.Offline;
167      this.childHiveTasks = new ItemList<HiveTask>();
168      syncTasksWithOptimizers = true;
169      RegisterChildHiveJobEvents();
170    }
171
172    public HiveTask(ItemTask itemJob, bool autoCreateChildHiveJobs)
173      : this() {
174      this.syncTasksWithOptimizers = autoCreateChildHiveJobs;
175      this.ItemTask = itemJob;
176      this.syncTasksWithOptimizers = true;
177    }
178
179    public HiveTask(Task job, TaskData taskData, bool autoCreateChildHiveTasks) {
180      this.syncTasksWithOptimizers = autoCreateChildHiveTasks;
181      this.Task = job;
182      try {
183        this.ItemTask = PersistenceUtil.Deserialize<ItemTask>(taskData.Data);
184      }
185      catch {
186        this.ItemTask = null;
187      }
188      this.childHiveTasks = new ItemList<HiveTask>();
189      this.syncTasksWithOptimizers = true;
190      RegisterChildHiveJobEvents();
191    }
192
193    protected HiveTask(HiveTask original, Cloner cloner)
194      : base(original, cloner) {
195      this.Task = cloner.Clone(original.task);
196      this.ItemTask = cloner.Clone(original.ItemTask);
197      original.childHiveTasksLock.EnterReadLock();
198      try {
199        this.childHiveTasks = cloner.Clone(original.childHiveTasks);
200      }
201      finally { original.childHiveTasksLock.ExitReadLock(); }
202      this.syncTasksWithOptimizers = original.syncTasksWithOptimizers;
203      this.isFinishedTaskDownloaded = original.isFinishedTaskDownloaded;
204    }
205    public override IDeepCloneable Clone(Cloner cloner) {
206      return new HiveTask(this, cloner);
207    }
208    #endregion
209
210    protected virtual void UpdateChildHiveTasks() { }
211
212    protected virtual void RegisterItemTaskEvents() {
213      if (ItemTask != null) {
214        ItemTask.ComputeInParallelChanged += new EventHandler(ItemJob_ComputeInParallelChanged);
215        ItemTask.ToStringChanged += new EventHandler(ItemJob_ToStringChanged);
216      }
217    }
218    protected virtual void DergisterItemTaskEvents() {
219      if (ItemTask != null) {
220        ItemTask.ComputeInParallelChanged -= new EventHandler(ItemJob_ComputeInParallelChanged);
221        ItemTask.ToStringChanged -= new EventHandler(ItemJob_ToStringChanged);
222      }
223    }
224
225    protected virtual void RegisterChildHiveJobEvents() {
226      this.childHiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnItemsAdded);
227      this.childHiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnItemsRemoved);
228      this.childHiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnCollectionReset);
229    }
230    protected virtual void DeregisterChildHiveJobEvents() {
231      this.childHiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnItemsAdded);
232      this.childHiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnItemsRemoved);
233      this.childHiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<HiveTask>>(OnCollectionReset);
234    }
235
236    protected virtual void ItemJob_ToStringChanged(object sender, EventArgs e) {
237      this.OnToStringChanged();
238    }
239
240    protected virtual void ItemJob_ComputeInParallelChanged(object sender, EventArgs e) {
241      if (ItemTask != null && syncTasksWithOptimizers) {
242        this.UpdateChildHiveTasks();
243      }
244    }
245
246    public virtual void AddChildHiveTask(HiveTask hiveTask) {
247      childHiveTasksLock.EnterWriteLock();
248      try {
249        this.childHiveTasks.Add(hiveTask);
250      }
251      finally { childHiveTasksLock.ExitWriteLock(); }
252    }
253
254    public override string ToString() {
255      if (itemTask != null && itemTask.Item != null) {
256        return itemTask.ToString();
257      } else {
258        return Task.Id.ToString();
259      }
260    }
261
262    public virtual void UpdateFromLightweightJob(LightweightTask lightweightJob) {
263      if (lightweightJob != null) {
264        task.Id = lightweightJob.Id;
265        task.ParentTaskId = lightweightJob.ParentTaskId;
266        task.ExecutionTime = lightweightJob.ExecutionTime;
267        task.State = lightweightJob.State;
268        task.StateLog = new List<StateLog>(lightweightJob.StateLog);
269        task.Command = lightweightJob.Command;
270
271        OnTaskStateChanged();
272        OnToStringChanged();
273        OnItemImageChanged();
274        OnStateLogChanged();
275      }
276    }
277
278    /// <summary>
279    /// Creates a TaskData object containing the Task and the IJob-Object as byte[]
280    /// </summary>
281    /// <param name="withoutChildOptimizers">
282    ///   if true the Child-Optimizers will not be serialized (if the task contains an Experiment)
283    /// </param>
284    public virtual TaskData GetAsTaskData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) {
285      plugins = new List<IPluginDescription>();
286      if (this.itemTask == null)
287        return null;
288
289      IEnumerable<Type> usedTypes;
290      byte[] taskByteArray = PersistenceUtil.Serialize(this.ItemTask, out usedTypes);
291      TaskData taskData = new TaskData() { TaskId = task.Id, Data = taskByteArray };
292      PluginUtil.CollectDeclaringPlugins(plugins, usedTypes);
293      return taskData;
294    }
295
296    #region Event Handler
297    public event EventHandler TaskChanged;
298    private void OnTaskChanged() {
299      EventHandler handler = TaskChanged;
300      if (handler != null) handler(this, EventArgs.Empty);
301    }
302
303    public event EventHandler TaskStateChanged;
304    private void OnTaskStateChanged() {
305      EventHandler handler = TaskStateChanged;
306      if (handler != null) handler(this, EventArgs.Empty);
307    }
308
309    public event EventHandler ItemTaskChanged;
310    private void OnItemTaskChanged() {
311      ItemJob_ComputeInParallelChanged(this, EventArgs.Empty);
312      var handler = ItemTaskChanged;
313      if (handler != null) handler(this, EventArgs.Empty);
314    }
315
316    public event EventHandler IsFinishedJobDownloadedChanged;
317    private void OnIsFinishedJobDownloadedChanged() {
318      var handler = IsFinishedJobDownloadedChanged;
319      if (handler != null) handler(this, EventArgs.Empty);
320    }
321
322    public event EventHandler StateLogChanged;
323    private void OnStateLogChanged() {
324      var handler = StateLogChanged;
325      if (handler != null) handler(this, EventArgs.Empty);
326    }
327
328    public event EventHandler IsControllableChanged;
329    private void OnIsControllableChanged() {
330      var handler = IsControllableChanged;
331      if (handler != null) handler(this, EventArgs.Empty);
332    }
333
334    private void RegisterJobEvents() {
335      if (task != null)
336        task.PropertyChanged += new PropertyChangedEventHandler(job_PropertyChanged);
337    }
338
339    private void DeregisterJobEvents() {
340      if (task != null)
341        task.PropertyChanged += new PropertyChangedEventHandler(job_PropertyChanged);
342    }
343
344    private void job_PropertyChanged(object sender, PropertyChangedEventArgs e) {
345      if (e.PropertyName == "State") {
346        IsFinishedTaskDownloaded = false;
347      }
348      if (e.PropertyName == "Priority" && Task != null) {
349        foreach (var task in childHiveTasks) {
350          task.Task.Priority = Task.Priority;
351        }
352      }
353    }
354    #endregion
355
356    /// <summary>
357    /// Returns a list of HiveTasks including this and all its child-jobs recursively
358    /// </summary>
359    public IEnumerable<HiveTask> GetAllHiveTasks() {
360      childHiveTasksLock.EnterReadLock();
361      try {
362        var tasks = new List<HiveTask>();
363        tasks.Add(this);
364        foreach (HiveTask child in this.childHiveTasks) {
365          tasks.AddRange(child.GetAllHiveTasks());
366        }
367        return tasks;
368      }
369      finally { childHiveTasksLock.ExitReadLock(); }
370    }
371
372    public HiveTask GetParentByJobId(Guid taskId) {
373      childHiveTasksLock.EnterReadLock();
374      try {
375        if (this.ChildHiveTasks.SingleOrDefault(j => j.task.Id == taskId) != null)
376          return this;
377        foreach (HiveTask child in this.childHiveTasks) {
378          HiveTask result = child.GetParentByJobId(taskId);
379          if (result != null)
380            return result;
381        }
382        return null;
383      }
384      finally { childHiveTasksLock.ExitWriteLock(); }
385    }
386
387    /// <summary>
388    /// Searches for an HiveTask object with the correct taskId recursively
389    /// </summary>
390    public HiveTask GetHiveTaskByTaskId(Guid jobId) {
391      if (this.Task.Id == jobId) {
392        return this;
393      } else {
394        childHiveTasksLock.EnterReadLock();
395        try {
396          foreach (HiveTask child in this.childHiveTasks) {
397            HiveTask result = child.GetHiveTaskByTaskId(jobId);
398            if (result != null)
399              return result;
400          }
401        }
402        finally { childHiveTasksLock.ExitReadLock(); }
403      }
404      return null;
405    }
406
407    public void RemoveByTaskId(Guid taskId) {
408      childHiveTasksLock.EnterWriteLock();
409      try {
410        IEnumerable<HiveTask> tasks = childHiveTasks.Where(j => j.Task.Id == taskId).ToList();
411        foreach (HiveTask t in tasks) {
412          this.childHiveTasks.Remove(t);
413        }
414        foreach (HiveTask child in childHiveTasks) {
415          child.RemoveByTaskId(taskId);
416        }
417      }
418      finally { childHiveTasksLock.ExitWriteLock(); }
419    }
420
421    public IEnumerable<IItemTree<HiveTask>> GetChildItems() {
422      return this.ChildHiveTasks;
423    }
424
425    #region INotifyObservableCollectionItemsChanged<IItemTree> Members
426
427    public event CollectionItemsChangedEventHandler<IItemTree<HiveTask>> CollectionReset;
428    private void OnCollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveTask>> e) {
429      foreach (var item in e.Items) {
430        item.Value.StateLogChanged -= new EventHandler(ChildHiveJob_StateLogChanged);
431      }
432      var handler = CollectionReset;
433      if (handler != null) handler(this, ToCollectionItemsChangedEventArgs(e));
434    }
435
436    public event CollectionItemsChangedEventHandler<IItemTree<HiveTask>> ItemsAdded;
437    private void OnItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveTask>> e) {
438      foreach (var item in e.Items) {
439        item.Value.StateLogChanged += new EventHandler(ChildHiveJob_StateLogChanged);
440        item.Value.IsControllable = this.IsControllable;
441      }
442      var handler = ItemsAdded;
443      if (handler != null) handler(this, ToCollectionItemsChangedEventArgs(e));
444    }
445
446    public event CollectionItemsChangedEventHandler<IItemTree<HiveTask>> ItemsRemoved;
447    private void OnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveTask>> e) {
448      foreach (var item in e.Items) {
449        item.Value.StateLogChanged -= new EventHandler(ChildHiveJob_StateLogChanged);
450      }
451      var handler = ItemsRemoved;
452      if (handler != null) handler(this, ToCollectionItemsChangedEventArgs(e));
453    }
454
455    private static CollectionItemsChangedEventArgs<IItemTree<HiveTask>> ToCollectionItemsChangedEventArgs(CollectionItemsChangedEventArgs<IndexedItem<HiveTask>> e) {
456      return new CollectionItemsChangedEventArgs<IItemTree<HiveTask>>(e.Items.Select(x => x.Value), e.OldItems == null ? null : e.OldItems.Select(x => x.Value));
457    }
458
459    private void ChildHiveJob_StateLogChanged(object sender, EventArgs e) {
460      OnStateLogChanged();
461    }
462    #endregion
463
464    public void Pause() {
465      if (this.Task.IsParentTask) {
466        childHiveTasksLock.EnterReadLock();
467        try {
468          foreach (var child in childHiveTasks) {
469            HiveServiceLocator.Instance.CallHiveService(s => s.PauseTask(child.task.Id));
470          }
471        }
472        finally { childHiveTasksLock.ExitReadLock(); }
473      } else {
474        HiveServiceLocator.Instance.CallHiveService(s => s.PauseTask(this.task.Id));
475      }
476    }
477
478    public void Stop() {
479      if (this.Task.IsParentTask) {
480        childHiveTasksLock.EnterReadLock();
481        try {
482          foreach (var child in childHiveTasks) {
483            HiveServiceLocator.Instance.CallHiveService(s => s.StopTask(child.task.Id));
484          }
485        }
486        finally { childHiveTasksLock.ExitReadLock(); }
487      } else {
488        HiveServiceLocator.Instance.CallHiveService(s => s.StopTask(this.task.Id));
489      }
490    }
491
492    public void Restart() {
493      HiveServiceLocator.Instance.CallHiveService(service => {
494        TaskData taskData = new TaskData();
495        taskData.TaskId = this.task.Id;
496        taskData.Data = PersistenceUtil.Serialize(this.itemTask);
497        service.UpdateTaskData(this.Task, taskData);
498        service.RestartTask(this.task.Id);
499        Task task = service.GetTask(this.task.Id);
500        this.task.LastTaskDataUpdate = task.LastTaskDataUpdate;
501      });
502    }
503
504    public ICollection<IItemTreeNodeAction<HiveTask>> Actions {
505      get {
506        return new List<IItemTreeNodeAction<HiveTask>>();
507      }
508    }
509
510    public virtual void IntegrateChild(ItemTask task, Guid childTaskId) { }
511
512    /// <summary>
513    /// Delete ItemTask
514    /// </summary>
515    public virtual void ClearData() {
516      this.ItemTask.Item = null;
517    }
518  }
519
520  [Item("Hive Task", "Represents a hive task.")]
521  [StorableClass]
522  public class HiveTask<T> : HiveTask where T : ItemTask {
523
524    public new T ItemTask {
525      get { return (T)base.ItemTask; }
526      internal set { base.ItemTask = value; }
527    }
528
529    #region Constructors and Cloning
530    public HiveTask() : base() { }
531    [StorableConstructor]
532    protected HiveTask(bool deserializing) { }
533    public HiveTask(T itemJob) : base(itemJob, true) { }
534    protected HiveTask(HiveTask<T> original, Cloner cloner)
535      : base(original, cloner) {
536    }
537    public override IDeepCloneable Clone(Cloner cloner) {
538      return new HiveTask<T>(this, cloner);
539    }
540    #endregion
541  }
542}
Note: See TracBrowser for help on using the repository browser.