Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/13 13:13:41 (12 years ago)
Author:
spimming
Message:

#1888:

  • Merged revisions from trunk
Location:
branches/OaaS
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/OaaS

  • branches/OaaS/HeuristicLab.Clients.Hive/3.3/RefreshableJob.cs

    r8165 r9363  
    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;
    36     private static object locker = new object();
     36    private object locker = new object();
     37    private object downloadFinishedLocker = new object();
     38    object jobResultReceivedLocker = new object();
    3739
    3840    public bool IsProgressing { get; set; }
     
    4648            throw new ArgumentNullException();
    4749
    48           if (job != null) DergisterJobEvents();
     50          if (job != null) DeregisterJobEvents();
    4951          job = value;
    5052          if (job != null) {
     
    6466      set {
    6567        if (hiveTasks != value) {
    66           if (hiveTasks != null) DeregisterHiveJobsEvents();
     68          if (hiveTasks != null) DeregisterHiveTasksEvents();
    6769          hiveTasks = value;
    68           if (hiveTasks != null) RegisterHiveJobsEvents();
     70          if (hiveTasks != null) RegisterHiveTasksEvents();
    6971          OnHiveTasksChanged();
    7072        }
     
    156158
    157159    // may execute jobs with privileged permissions on slaves
    158     private bool isAllowedPrivileged = true;
    159160    public bool IsAllowedPrivileged {
    160       get { return isAllowedPrivileged; }
     161      get { return Job.IsPrivileged; }
    161162      set {
    162         if (value != isAllowedPrivileged) {
    163           isAllowedPrivileged = value;
     163        if (value != Job.IsPrivileged) {
     164          Job.IsPrivileged = value;
    164165          OnIsAllowedPrivilegedChanged();
    165166        }
     
    188189    #region Constructors and Cloning
    189190    public RefreshableJob() {
    190       this.refreshAutomatically = true;
     191      this.refreshAutomatically = false;
    191192      this.Job = new Job();
    192193      this.log = new ThreadSafeLog();
     
    228229        jobResultPoller = new JobResultPoller(job.Id, Settings.Default.ResultPollingInterval);
    229230        RegisterResultPollingEvents();
    230         jobResultPoller.AutoResumeOnException = true;
     231        jobResultPoller.AutoResumeOnException = false;
    231232      }
    232233
     
    239240      if (jobResultPoller != null && jobResultPoller.IsPolling) {
    240241        jobResultPoller.Stop();
     242        DeregisterResultPollingEvents();
    241243      }
    242244    }
     
    258260      }
    259261    }
     262
    260263    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);
     264      lock (jobResultReceivedLocker) {
     265        foreach (LightweightTask lightweightTask in e.Value) {
     266          HiveTask hiveTask = GetHiveTaskById(lightweightTask.Id);
     267          if (hiveTask != null) {
     268            // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
     269            if (hiveTask.Task.State == TaskState.Offline && lightweightTask.State == TaskState.Waiting) {
     270              hiveTask.Task.LastTaskDataUpdate = lightweightTask.LastTaskDataUpdate;
     271            }
     272
     273            hiveTask.UpdateFromLightweightJob(lightweightTask);
     274
     275            if (!hiveTask.IsFinishedTaskDownloaded && !hiveTask.IsDownloading && hiveTask.Task.LastTaskDataUpdate < lightweightTask.LastTaskDataUpdate && (lightweightTask.State == TaskState.Finished || lightweightTask.State == TaskState.Aborted || lightweightTask.State == TaskState.Failed || lightweightTask.State == TaskState.Paused)) {
     276              log.LogMessage(string.Format("Downloading task {0}", lightweightTask.Id));
     277              hiveTask.IsDownloading = true;
     278              jobDownloader.DownloadTaskData(hiveTask.Task, (localJob, itemJob) => {
     279                lock (downloadFinishedLocker) {
     280                  log.LogMessage(string.Format("Finished downloading task {0}", localJob.Id));
     281                  HiveTask localHiveTask = GetHiveTaskById(localJob.Id);
     282
     283                  if (itemJob == null) {
     284                    // something bad happened to this task. bad task, BAAAD task!
     285                    localHiveTask.IsDownloading = false;
    293286                  } else {
    294                     localHiveTask.ItemTask = itemJob;
     287                    // if the task is paused, download but don't integrate into parent optimizer (to avoid Prepare)
     288                    if (localJob.State == TaskState.Paused) {
     289                      localHiveTask.ItemTask = itemJob;
     290                    } else {
     291                      if (localJob.ParentTaskId.HasValue) {
     292                        HiveTask parentHiveTask = GetHiveTaskById(localJob.ParentTaskId.Value);
     293                        parentHiveTask.IntegrateChild(itemJob, localJob.Id);
     294                      } else {
     295                        localHiveTask.ItemTask = itemJob;
     296                      }
     297                    }
     298                    localHiveTask.IsDownloading = false;
     299                    localHiveTask.Task.LastTaskDataUpdate = lightweightTask.LastTaskDataUpdate;
    295300                  }
    296301                }
    297                 localHiveTask.IsDownloading = false;
    298                 localHiveTask.Task.LastTaskDataUpdate = localJob.LastTaskDataUpdate;
    299               }
    300             });
     302              });
     303            }
    301304          }
    302305        }
    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();
     306        GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
     307        if (AllJobsFinished()) {
     308          this.ExecutionState = Core.ExecutionState.Stopped;
     309          StopResultPolling();
     310        }
     311        UpdateTotalExecutionTime();
     312        UpdateStatistics();
     313        OnStateLogListChanged();
     314        OnTaskReceived();
     315      }
    313316    }
    314317
     
    334337                                                   || j.Task.State == TaskState.Aborted
    335338                                                   || j.Task.State == TaskState.Failed)
    336                                                    && j.IsFinishedTaskDownloaded);
     339                                                   && !j.IsDownloading);
    337340    }
    338341
     
    356359    }
    357360
    358     private void DergisterJobEvents() {
     361    private void DeregisterJobEvents() {
    359362      job.ToStringChanged -= new EventHandler(OnToStringChanged);
    360363      job.PropertyChanged -= new PropertyChangedEventHandler(job_PropertyChanged);
     
    471474
    472475    #region HiveTasks Events
    473     private void RegisterHiveJobsEvents() {
     476    private void RegisterHiveTasksEvents() {
    474477      this.hiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
    475478      this.hiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
     
    477480    }
    478481
    479     private void DeregisterHiveJobsEvents() {
     482    private void DeregisterHiveTasksEvents() {
    480483      this.hiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
    481484      this.hiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
     
    559562
    560563    public Guid Id {
    561       get { return job.Id; }
     564      get {
     565        if (job == null) return Guid.Empty;
     566        return job.Id;
     567      }
    562568      set { job.Id = value; }
    563569    }
     
    603609      return this.ToString().CompareTo(other.ToString());
    604610    }
     611
     612    public void Unload() {
     613      // stop result polling
     614      if (refreshAutomatically)
     615        RefreshAutomatically = false;
     616      DisposeTasks();
     617      hiveTasks = new ItemCollection<HiveTask>();
     618    }
     619
     620    #region IDisposable Members
     621    public void Dispose() {
     622      if (jobDownloader != null) {
     623        jobDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
     624        jobDownloader.Dispose();
     625        jobDownloader = null;
     626      }
     627      if (jobResultPoller != null) {
     628        DeregisterResultPollingEvents();
     629        jobResultPoller = null;
     630      }
     631      if (hiveTasks != null) {
     632        DisposeTasks();
     633      }
     634      if (job != null) {
     635        DeregisterJobEvents();
     636        job = null;
     637      }
     638    }
     639
     640    private void DisposeTasks() {
     641      DeregisterHiveTasksEvents();
     642      foreach (var task in hiveTasks) {
     643        task.Dispose();
     644      }
     645      hiveTasks.Clear(); // this should remove the item_StateLogChanged event handlers
     646      hiveTasks = null;
     647    }
     648    #endregion
    605649  }
    606650}
Note: See TracChangeset for help on using the changeset viewer.