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