Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Clients.Hive/3.3/RefreshableJob.cs @ 7615

Last change on this file since 7615 was 7615, checked in by gkronber, 13 years ago

#1081 merged r7462:7609 from trunk into time series branch

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