Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/11 18:58:53 (13 years ago)
Author:
cneumuel
Message:

#1233

  • created baseclass for jobs (ItemJob) which derives OperatorJobs and EngineJobs
  • created special view for OptimizerJobs which derives from a more general view
  • removed logic from domain class HiveExperiment and moved it into RefreshableHiveExperiment
  • improved ItemTreeView
  • corrected plugin dependencies
  • fixed bug in database trigger when deleting HiveExperiments
  • added delete cascade for Plugin and PluginData
  • lots of fixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ServiceClients/HiveExperiment.cs

    r6006 r6033  
    2424using System.ComponentModel;
    2525using System.Linq;
    26 using HeuristicLab.Clients.Hive.Jobs;
    2726using HeuristicLab.Common;
    2827using HeuristicLab.Core;
    29 using HeuristicLab.Optimization;
    3028using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3129
     
    3331  [StorableClass]
    3432  public partial class HiveExperiment : IDeepCloneable, IContent, IProgressReporter {
    35     private JobResultPoller jobResultPoller;
    36 
    3733    [Storable]
    3834    private bool useLocalPlugins;
     
    7167      get { return hiveJobs; }
    7268      set {
    73         DeregisterHiveJobsEvents();
    7469        if (hiveJobs != value) {
    7570          hiveJobs = value;
    76           RegisterHiveJobsEvents();
    7771          OnHiveJobsChanged();
    7872        }
     
    9286    }
    9387
    94     /** include jobs when refreshing **/
    95     [Storable]
    96     private bool includeJobs;
    97     public bool IncludeJobs {
    98       get { return includeJobs; }
    99       set { includeJobs = value; }
    100     }
    101 
    102     [Storable]
    103     private bool refreshAutomatically;
    104     public bool RefreshAutomatically {
    105       get { return refreshAutomatically; }
    106       set {
    107         if (refreshAutomatically != value) {
    108           refreshAutomatically = value;
    109           OnRefreshAutomaticallyChanged();
    110           if (RefreshAutomatically) {
    111             StartResultPolling();
    112           } else {
    113             StopResultPolling();
    114           }
    115         }
    116       }
    117     }
    118 
    11988    [Storable]
    12089    private IProgress progress;
     
    12796    public HiveExperiment() {
    12897      this.ResourceNames = "HEAL";
    129       this.includeJobs = true;
    130       this.refreshAutomatically = true;
     98      this.HiveJobs = new ItemCollection<HiveJob>();
    13199    }
    132100
     
    140108      this.Description = original.Description;
    141109      this.Id = original.Id;
    142 
     110      this.HiveJobs = cloner.Clone(original.HiveJobs);
    143111      this.UseLocalPlugins = original.UseLocalPlugins;
    144112      this.ExecutionTime = original.ExecutionTime;
     
    148116    }
    149117    #endregion
    150 
    151     public override string ToString() {
    152       return Name;
    153     }
    154118
    155119    #region Events
     
    167131
    168132    public event EventHandler HiveJobsChanged;
    169     private void OnHiveJobsChanged() {
    170       if (jobResultPoller != null && jobResultPoller.IsPolling) {
    171         jobResultPoller.Stop();
    172         DeregisterResultPollingEvents();
    173       }
    174       if (HiveJobs != null && HiveJobs.Count > 0 && GetAllHiveJobs().All(x => x.Job.Id != Guid.Empty)) {
    175         if (this.RefreshAutomatically)
    176           StartResultPolling();
    177       }
     133    protected virtual void OnHiveJobsChanged() {
    178134      EventHandler handler = HiveJobsChanged;
    179135      if (handler != null) handler(this, EventArgs.Empty);
     
    185141      if (handler != null) handler(this, EventArgs.Empty);
    186142    }
    187 
    188     public event EventHandler RefreshAutomaticallyChanged;
    189     private void OnRefreshAutomaticallyChanged() {
    190       EventHandler handler = RefreshAutomaticallyChanged;
    191       if (handler != null) handler(this, EventArgs.Empty);
    192     }
    193143    #endregion
    194 
    195     private void RegisterHiveJobsEvents() {
    196       //if (HiveJobs != null) {
    197       //  HiveJobs.JobStateChanged += new EventHandler(HiveJob_JobStateChanged);
    198       //}
    199     }
    200 
    201     private void DeregisterHiveJobsEvents() {
    202       //if (HiveJobs != null) {
    203       //  HiveJobs.JobStateChanged -= new EventHandler(HiveJob_JobStateChanged);
    204       //}
    205     }
    206 
    207     private void HiveJob_JobStateChanged(object sender, EventArgs e) {
    208       //if (this.HiveJobs != null) {
    209       //  this.RootJobId = HiveJobs.Job.Id;
    210       //}
    211     }
    212 
    213     public Experiment GetExperiment(int idx) {
    214       if (this.HiveJobs != null) {
    215         var hj = HiveJobs.ElementAtOrDefault(idx);
    216         if (hj != null)
    217           return hj.OptimizerJob.OptimizerAsExperiment;
    218       }
    219       return null;
    220     }
    221 
    222     public void AddExperiment(Experiment experiment) {
    223       if (this.HiveJobs == null)
    224         this.HiveJobs = new ItemCollection<HiveJob>();
    225       this.HiveJobs.Add(new HiveJob(experiment));
    226     }
    227 
    228     public void SetExperiment(Experiment experiment) {
    229       if (this.HiveJobs == null)
    230         this.HiveJobs = new ItemCollection<HiveJob>();
    231       else
    232         this.HiveJobs.Clear();
    233       this.HiveJobs.Add(new HiveJob(experiment));
    234     }
    235 
     144   
    236145    protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
    237146      base.OnPropertyChanged(e);
     
    240149      }
    241150    }
    242 
    243     #region JobResultPoller Events
    244 
    245     public void StartResultPolling() {
    246       if (jobResultPoller == null) {
    247         jobResultPoller = new JobResultPoller(HiveJobs, /*ApplicationConstants.ResultPollingInterval*/new TimeSpan(0, 0, 5)); //TODO: find a better place for ApplicationConstants
    248         RegisterResultPollingEvents();
    249       }
    250 
    251       if (!jobResultPoller.IsPolling) {
    252         jobResultPoller.Start();
    253       }
    254     }
    255 
    256     public void StopResultPolling() {
    257       if (jobResultPoller != null && jobResultPoller.IsPolling) {
    258         jobResultPoller.Stop();
    259       }
    260     }
    261 
    262     private void RegisterResultPollingEvents() {
    263       jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
    264       jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
    265       jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
    266     }
    267     private void DeregisterResultPollingEvents() {
    268       jobResultPoller.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
    269       jobResultPoller.JobResultsReceived -= new EventHandler<EventArgs<IEnumerable<LightweightJob>>>(jobResultPoller_JobResultReceived);
    270       jobResultPoller.IsPollingChanged -= new EventHandler(jobResultPoller_IsPollingChanged);
    271     }
    272     private void jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
    273       this.refreshAutomatically = jobResultPoller.IsPolling;
    274       OnRefreshAutomaticallyChanged();
    275     }
    276     private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightJob>> e) {
    277       foreach (LightweightJob lightweightJob in e.Value) {
    278         HiveJob hj = GetHiveJobById(lightweightJob.Id);
    279         if (hj != null) {
    280           DateTime lastJobDataUpdate = hj.Job.LastJobDataUpdate;
    281           hj.UpdateFromLightweightJob(lightweightJob);
    282 
    283           // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
    284           if (lastJobDataUpdate != DateTime.MinValue && lastJobDataUpdate < hj.Job.LastJobDataUpdate) {
    285             OptimizerJob optimizerJob = ExperimentManagerClient.LoadOptimizerJob(hj.Job.Id);
    286             if (optimizerJob == null) {
    287               // something bad happened to this job. bad job, BAAAD job!
    288             } else {
    289               // if the job is paused, download but don't integrate into parent optimizer (to avoid Prepare)
    290               if (hj.Job.State == JobState.Paused) {
    291                 hj.OptimizerJob = optimizerJob;
    292               } else {
    293                 if (lightweightJob.ParentJobId.HasValue) {
    294                   HiveJob parentHiveJob = GetHiveJobById(lightweightJob.ParentJobId.Value);
    295                   parentHiveJob.UpdateChildOptimizer(optimizerJob, hj.Job.Id);
    296                 }
    297               }
    298             }
    299           }
    300         }
    301       }
    302       GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
    303       if (AllJobsFinished()) {
    304         this.ExecutionState = Core.ExecutionState.Stopped;
    305         StopResultPolling();
    306         //OnStopped();
    307       }
    308       UpdateTotalExecutionTime();
    309       UpdateStats();
    310     }
    311 
    312     public HiveJob GetHiveJobById(Guid jobId) {
    313       foreach (HiveJob job in HiveJobs) {
    314         HiveJob hj = job.GetHiveJobByJobId(jobId);
    315         if (hj != null)
    316           return hj;
    317       }
    318       return null;
    319     }
    320 
    321     private void UpdateStats() {
    322       var jobs = GetAllHiveJobs();
    323       this.JobCount = jobs.Count();
    324       this.CalculatingCount = jobs.Count(j => j.Job.State == JobState.Calculating);
    325       this.FinishedCount = jobs.Count(j => j.Job.State == JobState.Finished);
    326     }
    327 
    328     public IEnumerable<HiveJob> GetAllHiveJobs() {
    329       var jobs = new List<HiveJob>();
    330       foreach (HiveJob job in HiveJobs) {
    331         jobs.AddRange(job.GetAllHiveJobs());
    332       }
    333       return jobs;
    334     }
    335 
    336     public bool AllJobsFinished() {
    337       return GetAllHiveJobs().All(j => j.Job.State == JobState.Finished
    338                                             || j.Job.State == JobState.Aborted
    339                                             || j.Job.State == JobState.Failed);
    340     }
    341 
    342     private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
    343       //OnExceptionOccured(e.Value);
    344     }
    345 
    346     public void UpdateTotalExecutionTime() {
    347       this.ExecutionTime = TimeSpan.FromMilliseconds(GetAllHiveJobs().Sum(x => x.Job.ExecutionTime.HasValue ? x.Job.ExecutionTime.Value.TotalMilliseconds : 0));
    348     }
    349     #endregion
    350 
     151   
    351152    protected override void RaisePropertyChanged(string propertyName) {
    352153      if (!(propertyName == "ExecutionTime")
     
    362163        && HiveJobs.All(x => x.Job.DateFinished.HasValue && x.Job.DateCreated.HasValue);
    363164    }
     165
     166    public IEnumerable<HiveJob> GetAllHiveJobs() {
     167      var jobs = new List<HiveJob>();
     168      foreach (HiveJob job in HiveJobs) {
     169        jobs.AddRange(job.GetAllHiveJobs());
     170      }
     171      return jobs;
     172    }
     173
     174    public override string ToString() {
     175      return Name;
     176    }
     177
     178    public virtual void OnLoaded() { }
    364179  }
    365180}
Note: See TracChangeset for help on using the changeset viewer.