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