Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Clients.Hive/3.3/RefreshableJob.cs @ 12565

Last change on this file since 12565 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 22.3 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 HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Clients.Hive {
33  public class RefreshableJob : IHiveItem, IDeepCloneable, IContent, IComparable<RefreshableJob> {
34    private JobResultPoller jobResultPoller;
35    private ConcurrentTaskDownloader<ItemTask> jobDownloader;
36    private static object locker = new object();
37
38    public bool IsProgressing { get; set; }
39
40    private Job job;
41    public Job Job {
42      get { return job; }
43      set {
44        if (value != job) {
45          if (value == null)
46            throw new ArgumentNullException();
47
48          if (job != null) DergisterJobEvents();
49          job = value;
50          if (job != null) {
51            RegisterJobEvents();
52            job_PropertyChanged(job, new PropertyChangedEventArgs("Id"));
53          }
54          OnJobChanged();
55          OnToStringChanged(this, EventArgs.Empty);
56          job_ItemImageChanged(this, EventArgs.Empty);
57        }
58      }
59    }
60
61    private ItemCollection<HiveTask> hiveTasks;
62    public ItemCollection<HiveTask> HiveTasks {
63      get { return hiveTasks; }
64      set {
65        if (hiveTasks != value) {
66          if (hiveTasks != null) DeregisterHiveJobsEvents();
67          hiveTasks = value;
68          if (hiveTasks != null) RegisterHiveJobsEvents();
69          OnHiveTasksChanged();
70        }
71      }
72    }
73
74    private ExecutionState executionState;
75    public ExecutionState ExecutionState {
76      get { return executionState; }
77      internal set {
78        if (executionState != value) {
79          executionState = value;
80          OnExecutionStateChanged();
81        }
82      }
83    }
84
85    private TimeSpan executionTime;
86    public TimeSpan ExecutionTime {
87      get { return executionTime; }
88      internal set {
89        if (executionTime != value) {
90          executionTime = value;
91          OnExecutionTimeChanged();
92        }
93      }
94    }
95
96    private bool refreshAutomatically;
97    public bool RefreshAutomatically {
98      get { return refreshAutomatically; }
99      set {
100        lock (locker) {
101          if (refreshAutomatically != value) {
102            refreshAutomatically = value;
103            OnRefreshAutomaticallyChanged();
104          }
105          if (RefreshAutomatically) {
106            if (this.HiveTasks != null && this.HiveTasks.Count > 0 && (jobResultPoller == null || !jobResultPoller.IsPolling)) {
107              StartResultPolling();
108            }
109          } else {
110            StopResultPolling();
111          }
112        }
113      }
114    }
115
116    // indicates if download button is enabled
117    private bool isDownloadable = true;
118    public bool IsDownloadable {
119      get { return isDownloadable; }
120      set {
121        if (value != isDownloadable) {
122          isDownloadable = value;
123          OnIsDownloadableChanged();
124        }
125      }
126    }
127
128    // if true, all control buttons should be enabled. otherwise disabled
129    private bool isControllable = true;
130    public bool IsControllable {
131      get { return isControllable; }
132      private set {
133        if (value != isControllable) {
134          isControllable = value;
135          OnIsControllableChanged();
136          if (this.hiveTasks != null) {
137            foreach (var hiveJob in this.hiveTasks) {
138              hiveJob.IsControllable = value;
139            }
140          }
141        }
142      }
143    }
144
145    // indicates if a user is allowed to share this experiment
146    private bool isSharable = true;
147    public bool IsSharable {
148      get { return isSharable; }
149      private set {
150        if (value != isSharable) {
151          isSharable = value;
152          OnIsSharableChanged();
153        }
154      }
155    }
156
157    // may execute jobs with privileged permissions on slaves
158    private bool isAllowedPrivileged = true;
159    public bool IsAllowedPrivileged {
160      get { return isAllowedPrivileged; }
161      set {
162        if (value != isAllowedPrivileged) {
163          isAllowedPrivileged = value;
164          OnIsAllowedPrivilegedChanged();
165        }
166      }
167    }
168
169    private Progress progress;
170    public Progress Progress {
171      get { return progress; }
172      set {
173        this.progress = value;
174        OnIsProgressingChanged();
175      }
176    }
177
178
179    private ThreadSafeLog log;
180    public ILog Log {
181      get { return log; }
182    }
183
184    public StateLogListList StateLogList {
185      get { return new StateLogListList(this.GetAllHiveTasks().Select(x => x.StateLog)); }
186    }
187
188    #region Constructors and Cloning
189    public RefreshableJob() {
190      this.refreshAutomatically = true;
191      this.Job = new Job();
192      this.log = new ThreadSafeLog();
193      this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
194      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
195      this.HiveTasks = new ItemCollection<HiveTask>();
196    }
197    public RefreshableJob(Job hiveExperiment) {
198      this.refreshAutomatically = true;
199      this.Job = hiveExperiment;
200      this.log = new ThreadSafeLog();
201      this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
202      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
203      this.HiveTasks = new ItemCollection<HiveTask>();
204    }
205    protected RefreshableJob(RefreshableJob original, Cloner cloner) {
206      cloner.RegisterClonedObject(original, this);
207      this.Job = cloner.Clone(original.Job);
208      this.IsControllable = original.IsControllable;
209      this.log = cloner.Clone(original.log);
210      this.RefreshAutomatically = false; // do not start results polling automatically
211      this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
212      this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
213      this.HiveTasks = cloner.Clone(original.HiveTasks);
214      this.ExecutionTime = original.ExecutionTime;
215      this.ExecutionState = original.ExecutionState;
216    }
217    public IDeepCloneable Clone(Cloner cloner) {
218      return new RefreshableJob(this, cloner);
219    }
220    public object Clone() {
221      return this.Clone(new Cloner());
222    }
223    #endregion
224
225    #region JobResultPoller Events
226    public void StartResultPolling() {
227      if (jobResultPoller == null) {
228        jobResultPoller = new JobResultPoller(job.Id, Settings.Default.ResultPollingInterval);
229        RegisterResultPollingEvents();
230        jobResultPoller.AutoResumeOnException = true;
231      }
232
233      if (!jobResultPoller.IsPolling) {
234        jobResultPoller.Start();
235      }
236    }
237
238    public void StopResultPolling() {
239      if (jobResultPoller != null && jobResultPoller.IsPolling) {
240        jobResultPoller.Stop();
241      }
242    }
243
244    private void RegisterResultPollingEvents() {
245      jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
246      jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightTask>>>(jobResultPoller_JobResultReceived);
247      jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
248    }
249    private void DeregisterResultPollingEvents() {
250      jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
251      jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightTask>>>(jobResultPoller_JobResultReceived);
252      jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
253    }
254    private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
255      if (this.refreshAutomatically != jobResultPoller.IsPolling) {
256        this.refreshAutomatically = jobResultPoller.IsPolling;
257        OnRefreshAutomaticallyChanged();
258      }
259    }
260    private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightTask>> e) {
261      foreach (LightweightTask lightweightTask in e.Value) {
262        HiveTask hiveTask = GetHiveTaskById(lightweightTask.Id);
263        if (hiveTask != null) {
264          // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
265          if (hiveTask.Task.State == TaskState.Offline && lightweightTask.State != TaskState.Finished && lightweightTask.State != TaskState.Failed && lightweightTask.State != TaskState.Aborted) {
266            hiveTask.Task.LastTaskDataUpdate = lightweightTask.LastTaskDataUpdate;
267          }
268
269          hiveTask.UpdateFromLightweightJob(lightweightTask);
270
271          if (!hiveTask.IsFinishedTaskDownloaded && !hiveTask.IsDownloading && hiveTask.Task.LastTaskDataUpdate < lightweightTask.LastTaskDataUpdate) {
272            log.LogMessage(string.Format("Downloading task {0}", lightweightTask.Id));
273            hiveTask.IsDownloading = true;
274            jobDownloader.DownloadTaskData(hiveTask.Task, (localJob, itemJob) => {
275              log.LogMessage(string.Format("Finished downloading task {0}", localJob.Id));
276              HiveTask localHiveTask = GetHiveTaskById(localJob.Id);
277
278              if (itemJob == null) {
279                localHiveTask.IsDownloading = false;
280              }
281
282              if (itemJob == null) {
283                // something bad happened to this task. bad task, BAAAD task!
284              } else {
285                // if the task is paused, download but don't integrate into parent optimizer (to avoid Prepare)
286
287                if (localJob.State == TaskState.Paused) {
288                  localHiveTask.ItemTask = itemJob;
289                } else {
290                  if (localJob.ParentTaskId.HasValue) {
291                    HiveTask parentHiveTask = GetHiveTaskById(localJob.ParentTaskId.Value);
292                    parentHiveTask.IntegrateChild(itemJob, localJob.Id);
293                  } else {
294                    localHiveTask.ItemTask = itemJob;
295                  }
296                }
297                localHiveTask.IsDownloading = false;
298                localHiveTask.Task.LastTaskDataUpdate = localJob.LastTaskDataUpdate;
299              }
300            });
301          }
302        }
303      }
304      GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
305      if (AllJobsFinished()) {
306        this.ExecutionState = Core.ExecutionState.Stopped;
307        StopResultPolling();
308      }
309      UpdateTotalExecutionTime();
310      UpdateStatistics();
311      OnStateLogListChanged();
312      OnTaskReceived();
313    }
314
315    public HiveTask GetHiveTaskById(Guid jobId) {
316      foreach (HiveTask t in this.HiveTasks) {
317        var hj = t.GetHiveTaskByTaskId(jobId);
318        if (hj != null)
319          return hj;
320      }
321      return null;
322    }
323
324    private void UpdateStatistics() {
325      var jobs = this.GetAllHiveTasks();
326      job.JobCount = jobs.Count();
327      job.CalculatingCount = jobs.Count(j => j.Task.State == TaskState.Calculating);
328      job.FinishedCount = jobs.Count(j => j.Task.State == TaskState.Finished);
329      OnJobStatisticsChanged();
330    }
331
332    public bool AllJobsFinished() {
333      return this.GetAllHiveTasks().All(j => (j.Task.State == TaskState.Finished
334                                                   || j.Task.State == TaskState.Aborted
335                                                   || j.Task.State == TaskState.Failed)
336                                                   && j.IsFinishedTaskDownloaded);
337    }
338
339    private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
340      OnExceptionOccured(sender, e.Value);
341    }
342    private void jobDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
343      OnExceptionOccured(sender, e.Value);
344    }
345    public void UpdateTotalExecutionTime() {
346      this.ExecutionTime = TimeSpan.FromMilliseconds(this.GetAllHiveTasks().Sum(x => x.Task.ExecutionTime.TotalMilliseconds));
347    }
348    #endregion
349
350    #region Job Events
351    private void RegisterJobEvents() {
352      job.ToStringChanged += new EventHandler(OnToStringChanged);
353      job.PropertyChanged += new PropertyChangedEventHandler(job_PropertyChanged);
354      job.ItemImageChanged += new EventHandler(job_ItemImageChanged);
355      job.ModifiedChanged += new EventHandler(job_ModifiedChanged);
356    }
357
358    private void DergisterJobEvents() {
359      job.ToStringChanged -= new EventHandler(OnToStringChanged);
360      job.PropertyChanged -= new PropertyChangedEventHandler(job_PropertyChanged);
361      job.ItemImageChanged -= new EventHandler(job_ItemImageChanged);
362      job.ModifiedChanged -= new EventHandler(job_ModifiedChanged);
363    }
364    #endregion
365
366    #region Event Handler
367    public event EventHandler RefreshAutomaticallyChanged;
368    private void OnRefreshAutomaticallyChanged() {
369      var handler = RefreshAutomaticallyChanged;
370      if (handler != null) handler(this, EventArgs.Empty);
371    }
372
373    public event EventHandler JobChanged;
374    private void OnJobChanged() {
375      var handler = JobChanged;
376      if (handler != null) handler(this, EventArgs.Empty);
377    }
378
379    public event EventHandler ModifiedChanged;
380    private void job_ModifiedChanged(object sender, EventArgs e) {
381      var handler = ModifiedChanged;
382      if (handler != null) handler(sender, e);
383    }
384
385    public event EventHandler ItemImageChanged;
386    private void job_ItemImageChanged(object sender, EventArgs e) {
387      var handler = ItemImageChanged;
388      if (handler != null) handler(this, e);
389    }
390
391    public event PropertyChangedEventHandler PropertyChanged;
392    private void job_PropertyChanged(object sender, PropertyChangedEventArgs e) {
393      this.IsSharable = job.Permission == Permission.Full;
394      this.IsControllable = job.Permission == Permission.Full;
395
396      var handler = PropertyChanged;
397      if (handler != null) handler(sender, e);
398    }
399
400    public event EventHandler ToStringChanged;
401    private void OnToStringChanged(object sender, EventArgs e) {
402      var handler = ToStringChanged;
403      if (handler != null) handler(this, e);
404    }
405
406    public event EventHandler IsDownloadableChanged;
407    private void OnIsDownloadableChanged() {
408      var handler = IsDownloadableChanged;
409      if (handler != null) handler(this, EventArgs.Empty);
410    }
411
412    public event EventHandler IsControllableChanged;
413    private void OnIsControllableChanged() {
414      var handler = IsControllableChanged;
415      if (handler != null) handler(this, EventArgs.Empty);
416    }
417
418    public event EventHandler IsSharableChanged;
419    private void OnIsSharableChanged() {
420      var handler = IsSharableChanged;
421      if (handler != null) handler(this, EventArgs.Empty);
422    }
423
424    public event EventHandler IsAllowedPrivilegedChanged;
425    private void OnIsAllowedPrivilegedChanged() {
426      var handler = IsAllowedPrivilegedChanged;
427      if (handler != null) handler(this, EventArgs.Empty);
428    }
429
430    public event EventHandler JobStatisticsChanged;
431    private void OnJobStatisticsChanged() {
432      var handler = JobStatisticsChanged;
433      if (handler != null) handler(this, EventArgs.Empty);
434    }
435
436    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
437    private void OnExceptionOccured(object sender, Exception exception) {
438      log.LogException(exception);
439      var handler = ExceptionOccured;
440      if (handler != null) handler(sender, new EventArgs<Exception>(exception));
441    }
442
443    public event EventHandler StateLogListChanged;
444    private void OnStateLogListChanged() {
445      var handler = StateLogListChanged;
446      if (handler != null) handler(this, EventArgs.Empty);
447    }
448
449    public event EventHandler ExecutionTimeChanged;
450    protected virtual void OnExecutionTimeChanged() {
451      var handler = ExecutionTimeChanged;
452      if (handler != null) handler(this, EventArgs.Empty);
453    }
454
455    public event EventHandler ExecutionStateChanged;
456    protected virtual void OnExecutionStateChanged() {
457      var handler = ExecutionStateChanged;
458      if (handler != null) handler(this, EventArgs.Empty);
459    }
460    public event EventHandler TaskReceived;
461    protected virtual void OnTaskReceived() {
462      var handler = TaskReceived;
463      if (handler != null) handler(this, EventArgs.Empty);
464    }
465    public event EventHandler IsProgressingChanged;
466    private void OnIsProgressingChanged() {
467      var handler = IsProgressingChanged;
468      if (handler != null) handler(this, EventArgs.Empty);
469    }
470    #endregion
471
472    #region HiveTasks Events
473    private void RegisterHiveJobsEvents() {
474      this.hiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
475      this.hiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
476      this.hiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_CollectionReset);
477    }
478
479    private void DeregisterHiveJobsEvents() {
480      this.hiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
481      this.hiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
482      this.hiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_CollectionReset);
483    }
484
485    private void hiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
486      foreach (var item in e.Items) {
487        item.StateLogChanged -= new EventHandler(item_StateLogChanged);
488      }
489      OnHiveTasksReset(e);
490    }
491
492    private void hiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
493      foreach (var item in e.Items) {
494        item.StateLogChanged -= new EventHandler(item_StateLogChanged);
495      }
496      OnHiveTasksRemoved(e);
497    }
498
499    private void hivetasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
500      foreach (var item in e.Items) {
501        item.StateLogChanged += new EventHandler(item_StateLogChanged);
502        item.IsControllable = this.IsControllable;
503      }
504      OnHiveTasksAdded(e);
505    }
506
507    private void item_StateLogChanged(object sender, EventArgs e) {
508      OnStateLogListChanged();
509    }
510    #endregion
511
512    public event EventHandler HiveTasksChanged;
513    protected virtual void OnHiveTasksChanged() {
514      StopResultPolling();
515      if (this.HiveTasks != null && this.HiveTasks.Count > 0 && this.GetAllHiveTasks().All(x => x.Task.Id != Guid.Empty)) {
516        if (IsFinished()) {
517          this.ExecutionState = Core.ExecutionState.Stopped;
518          this.RefreshAutomatically = false;
519          if (jobResultPoller != null) DeregisterResultPollingEvents();
520        } else {
521          this.RefreshAutomatically = true;
522        }
523      }
524
525      var handler = HiveTasksChanged;
526      if (handler != null) handler(this, EventArgs.Empty);
527    }
528
529    public event EventHandler Loaded;
530    public virtual void OnLoaded() {
531      this.UpdateTotalExecutionTime();
532      this.OnStateLogListChanged();
533
534      if (this.ExecutionState != ExecutionState.Stopped) {
535        this.RefreshAutomatically = true;
536      }
537
538      var handler = Loaded;
539      if (handler != null) handler(this, EventArgs.Empty);
540    }
541
542    public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksAdded;
543    private void OnHiveTasksAdded(CollectionItemsChangedEventArgs<HiveTask> e) {
544      var handler = HiveTasksAdded;
545      if (handler != null) handler(this, e);
546    }
547
548    public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksRemoved;
549    private void OnHiveTasksRemoved(CollectionItemsChangedEventArgs<HiveTask> e) {
550      var handler = HiveTasksRemoved;
551      if (handler != null) handler(this, e);
552    }
553
554    public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksReset;
555    private void OnHiveTasksReset(CollectionItemsChangedEventArgs<HiveTask> e) {
556      var handler = HiveTasksReset;
557      if (handler != null) handler(this, e);
558    }
559
560    public Guid Id {
561      get { return job.Id; }
562      set { job.Id = value; }
563    }
564    public bool Modified {
565      get { return job.Modified; }
566    }
567    public void Store() {
568      job.Store();
569    }
570    public string ItemDescription {
571      get { return job.ItemDescription; }
572    }
573    public Image ItemImage {
574      get { return job.ItemImage; }
575    }
576    public string ItemName {
577      get { return job.ItemName; }
578    }
579    public Version ItemVersion {
580      get { return job.ItemVersion; }
581    }
582
583    public override string ToString() {
584      return string.Format("{0} {1}", Job.DateCreated.ToString("MM.dd.yyyy HH:mm"), Job.ToString());
585    }
586
587    public bool IsFinished() {
588      return HiveTasks != null
589        && HiveTasks.All(x => x.Task.DateFinished.HasValue && x.Task.DateCreated.HasValue);
590    }
591
592    public IEnumerable<HiveTask> GetAllHiveTasks() {
593      if (hiveTasks == null) return Enumerable.Empty<HiveTask>();
594
595      var tasks = new List<HiveTask>();
596      foreach (HiveTask task in HiveTasks) {
597        tasks.AddRange(task.GetAllHiveTasks());
598      }
599      return tasks;
600    }
601
602    public int CompareTo(RefreshableJob other) {
603      return this.ToString().CompareTo(other.ToString());
604    }
605  }
606}
Note: See TracBrowser for help on using the repository browser.