Changeset 9169 for branches/UnloadJobs


Ignore:
Timestamp:
01/16/13 11:13:18 (7 years ago)
Author:
ascheibe
Message:

#2005

  • renamed some events and methods
  • fixed some memory leaks
Location:
branches/UnloadJobs
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/UnloadJobs/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveJobManagerView.cs

    r7259 r9169  
    5454      Content.Refreshing += new EventHandler(Content_Refreshing);
    5555      Content.Refreshed += new EventHandler(Content_Refreshed);
    56       Content.HiveExperimentsChanged += new EventHandler(Content_HiveExperimentsChanged);
     56      Content.HiveJobsChanged += new EventHandler(Content_HiveJobsChanged);
    5757
    5858    }
     
    6161      Content.Refreshing -= new EventHandler(Content_Refreshing);
    6262      Content.Refreshed -= new EventHandler(Content_Refreshed);
    63       Content.HiveExperimentsChanged -= new EventHandler(Content_HiveExperimentsChanged);
     63      Content.HiveJobsChanged -= new EventHandler(Content_HiveJobsChanged);
    6464      base.DeregisterContentEvents();
    6565    }
     
    123123
    124124    protected override void OnClosing(FormClosingEventArgs e) {
    125       base.OnClosing(e);
    126       if (Content != null && Content.Jobs != null) {
    127         foreach (var exp in Content.Jobs.OfType<RefreshableJob>()) {
    128           if (exp.RefreshAutomatically) {
    129             exp.RefreshAutomatically = false; // stop result polling
    130           }
     125      if (Content.Jobs.Any(x => x.IsProgressing)) {
     126        MessageBox.Show("The Hive Job Manager can only be closed after all down/uploads are finished. ", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
     127        e.Cancel = true;
     128      } else {
     129        base.OnClosing(e);
     130        if (Content != null && Content.Jobs != null) {
     131          Content.Jobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<RefreshableJob>(HiveExperiments_ItemsRemoved);
     132          Content.ClearHiveClient();
    131133        }
    132134      }
     
    139141    }
    140142
    141     private void Content_HiveExperimentsChanged(object sender, EventArgs e) {
    142       Content.Jobs.ItemsRemoved += new CollectionItemsChangedEventHandler<RefreshableJob>(HiveExperiments_ItemsRemoved);
     143    private void Content_HiveJobsChanged(object sender, EventArgs e) {
     144      if (Content.Jobs != null) {
     145        Content.Jobs.ItemsRemoved += new CollectionItemsChangedEventHandler<RefreshableJob>(HiveExperiments_ItemsRemoved);
     146      }
    143147    }
    144148  }
  • branches/UnloadJobs/HeuristicLab.Clients.Hive/3.3/ConcurrentTaskDownloader.cs

    r7259 r9169  
    3030  /// Downloads and deserializes jobs. It avoids too many jobs beeing downloaded or deserialized at the same time to avoid memory problems
    3131  /// </summary>
    32   public class ConcurrentTaskDownloader<T> where T : class, ITask {
     32  public class ConcurrentTaskDownloader<T> : IDisposable where T : class, ITask {
    3333    private bool abort = false;
    3434    // use semaphore to ensure only few concurrenct connections and few SerializedJob objects in memory
     
    8282        if (abort) return null;
    8383        result = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(task.Id));
    84       } finally {
     84      }
     85      finally {
    8586        downloadSemaphore.Release();
    8687      }
     
    9596        taskData.Item2.Data = null; // reduce memory consumption.
    9697        return new Tuple<Task, T>(taskData.Item1, deserializedJob);
    97       } finally {
     98      }
     99      finally {
    98100        deserializeSemaphore.Release();
    99101      }
     
    110112      if (handler != null) handler(this, new EventArgs<Exception>(exception));
    111113    }
     114
     115    #region IDisposable Members
     116    public void Dispose() {
     117      TaskScheduler.UnobservedTaskException -= new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);
     118      deserializeSemaphore.Dispose();
     119      downloadSemaphore.Dispose();
     120    }
     121    #endregion
    112122  }
    113123}
  • branches/UnloadJobs/HeuristicLab.Clients.Hive/3.3/HiveClient.cs

    r9107 r9169  
    5252        if (value != jobs) {
    5353          jobs = value;
    54           OnHiveExperimentsChanged();
     54          OnHiveJobsChanged();
    5555        }
    5656      }
     
    7777
    7878    private HiveClient() { }
     79
     80    public void ClearHiveClient() {
     81      foreach (var j in Jobs) {
     82        if (j.RefreshAutomatically) {
     83          j.RefreshAutomatically = false; // stop result polling
     84          j.Dispose();
     85        }
     86      }
     87      Jobs = null;
     88      if (onlinePlugins != null)
     89        onlinePlugins.Clear();
     90      if (alreadyUploadedPlugins != null)
     91        alreadyUploadedPlugins.Clear();
     92    }
    7993
    8094    #region Refresh
     
    205219      if (handler != null) handler(this, EventArgs.Empty);
    206220    }
    207     public event EventHandler HiveExperimentsChanged;
    208     private void OnHiveExperimentsChanged() {
    209       var handler = HiveExperimentsChanged;
     221    public event EventHandler HiveJobsChanged;
     222    private void OnHiveJobsChanged() {
     223      var handler = HiveJobsChanged;
    210224      if (handler != null) handler(this, EventArgs.Empty);
    211225    }
     
    308322      }
    309323      finally {
    310         refreshableJob.RefreshAutomatically = true;
    311         refreshableJob.StartResultPolling();
     324        //refreshableJob.RefreshAutomatically = true;       
    312325        refreshableJob.Job.Modified = false;
    313326        refreshableJob.IsProgressing = false;
     
    421434      refreshableJob.IsProgressing = true;
    422435      refreshableJob.Progress = new Progress();
     436      TaskDownloader downloader = null;
    423437
    424438      try {
     
    433447
    434448        refreshableJob.Progress.Status = "Downloading tasks...";
    435         TaskDownloader downloader = new TaskDownloader(allTasks.Select(x => x.Id));
     449        downloader = new TaskDownloader(allTasks.Select(x => x.Id));
    436450        downloader.StartAsync();
    437451
     
    465479        refreshableJob.IsProgressing = false;
    466480        refreshableJob.Progress.Finish();
     481        if (downloader != null) {
     482          downloader.Dispose();
     483        }
    467484      }
    468485    }
  • branches/UnloadJobs/HeuristicLab.Clients.Hive/3.3/RefreshableJob.cs

    r9107 r9169  
    3131
    3232namespace HeuristicLab.Clients.Hive {
    33   public class RefreshableJob : IHiveItem, IDeepCloneable, IContent, IComparable<RefreshableJob> {
     33  public class RefreshableJob : IHiveItem, IDeepCloneable, IContent, IComparable<RefreshableJob>, IDisposable {
    3434    private JobResultPoller jobResultPoller;
    3535    private ConcurrentTaskDownloader<ItemTask> jobDownloader;
     
    4848            throw new ArgumentNullException();
    4949
    50           if (job != null) DergisterJobEvents();
     50          if (job != null) DeregisterJobEvents();
    5151          job = value;
    5252          if (job != null) {
     
    6666      set {
    6767        if (hiveTasks != value) {
    68           if (hiveTasks != null) DeregisterHiveJobsEvents();
     68          if (hiveTasks != null) DeregisterHiveTasksEvents();
    6969          hiveTasks = value;
    70           if (hiveTasks != null) RegisterHiveJobsEvents();
     70          if (hiveTasks != null) RegisterHiveTasksEvents();
    7171          OnHiveTasksChanged();
    7272        }
     
    240240      if (jobResultPoller != null && jobResultPoller.IsPolling) {
    241241        jobResultPoller.Stop();
     242        DeregisterResultPollingEvents();
    242243      }
    243244    }
     
    358359    }
    359360
    360     private void DergisterJobEvents() {
     361    private void DeregisterJobEvents() {
    361362      job.ToStringChanged -= new EventHandler(OnToStringChanged);
    362363      job.PropertyChanged -= new PropertyChangedEventHandler(job_PropertyChanged);
     
    473474
    474475    #region HiveTasks Events
    475     private void RegisterHiveJobsEvents() {
     476    private void RegisterHiveTasksEvents() {
    476477      this.hiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
    477478      this.hiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
     
    479480    }
    480481
    481     private void DeregisterHiveJobsEvents() {
     482    private void DeregisterHiveTasksEvents() {
    482483      this.hiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
    483484      this.hiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
     
    605606      return this.ToString().CompareTo(other.ToString());
    606607    }
     608
     609    #region IDisposable Members
     610    public void Dispose() {
     611      if (jobDownloader != null) {
     612        jobDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
     613        jobDownloader.Dispose();
     614        jobDownloader = null;
     615      }
     616      if (job != null) {
     617        DeregisterJobEvents();
     618        job = null;
     619      }
     620      if (hiveTasks != null) {
     621        DeregisterHiveTasksEvents();
     622        hiveTasks.Clear(); // this should remove the item_StateLogChanged event handlers
     623        hiveTasks = null;
     624      }
     625      if (jobResultPoller != null) {
     626        DeregisterResultPollingEvents();
     627        jobResultPoller = null;
     628      }
     629    }
     630    #endregion
    607631  }
    608632}
  • branches/UnloadJobs/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs

    r7259 r9169  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using HeuristicLab.Clients.Hive.Jobs;
    2627using HeuristicLab.Common;
    27 using System.Threading;
    2828
    2929namespace HeuristicLab.Clients.Hive {
    30   public class TaskDownloader {
     30  public class TaskDownloader : IDisposable {
    3131    private IEnumerable<Guid> taskIds;
    3232    private ConcurrentTaskDownloader<ItemTask> taskDownloader;
     
    3838    public bool IsFinished {
    3939      get {
    40           try {       
    41               resultsLock.EnterReadLock();
    42               return results.Count == taskIds.Count();
    43           } finally { resultsLock.ExitReadLock(); }
     40        try {
     41          resultsLock.EnterReadLock();
     42          return results.Count == taskIds.Count();
     43        }
     44        finally { resultsLock.ExitReadLock(); }
    4445      }
    4546    }
     
    5960    public int FinishedCount {
    6061      get {
    61             try {
    62               resultsLock.EnterReadLock();
    63               return results.Count;
    64              } finally { resultsLock.ExitReadLock(); }
     62        try {
     63          resultsLock.EnterReadLock();
     64          return results.Count;
     65        }
     66        finally { resultsLock.ExitReadLock(); }
    6567      }
    6668    }
     
    6870    public IDictionary<Guid, HiveTask> Results {
    6971      get {
    70             try {
    71               resultsLock.EnterReadLock();
    72               return results;
    73             } finally { resultsLock.ExitReadLock(); }
     72        try {
     73          resultsLock.EnterReadLock();
     74          return results;
     75        }
     76        finally { resultsLock.ExitReadLock(); }
    7477      }
    7578    }
     
    97100                resultsLock.EnterWriteLock();
    98101                this.results.Add(localJob.Id, hiveTask);
    99               } finally { resultsLock.ExitWriteLock(); }
     102              }
     103              finally { resultsLock.ExitWriteLock(); }
    100104            }
    101105          });
     
    114118      if (handler != null) handler(this, new EventArgs<Exception>(exception));
    115119    }
     120
     121    #region IDisposable Members
     122    public void Dispose() {
     123      taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
     124      resultsLock.Dispose();
     125    }
     126    #endregion
    116127  }
    117128}
Note: See TracChangeset for help on using the changeset viewer.