[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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 {
|
---|
[9219] | 33 | public class RefreshableJob : IHiveItem, IDeepCloneable, IContent, IComparable<RefreshableJob>, IDisposable {
|
---|
[6976] | 34 | private JobResultPoller jobResultPoller;
|
---|
| 35 | private ConcurrentTaskDownloader<ItemTask> jobDownloader;
|
---|
[8871] | 36 | private object locker = new object();
|
---|
| 37 | private object downloadFinishedLocker = new object();
|
---|
[8939] | 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 |
|
---|
[9219] | 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) {
|
---|
[9219] | 68 | if (hiveTasks != null) DeregisterHiveTasksEvents();
|
---|
[6976] | 69 | hiveTasks = value;
|
---|
[9219] | 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 {
|
---|
[9390] | 112 | PauseResultPolling();
|
---|
[6976] | 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 |
|
---|
[11083] | 159 | private IProgress progress;
|
---|
| 160 | public IProgress Progress {
|
---|
[6976] | 161 | get { return progress; }
|
---|
[8156] | 162 | set {
|
---|
| 163 | this.progress = value;
|
---|
| 164 | OnIsProgressingChanged();
|
---|
| 165 | }
|
---|
[6976] | 166 | }
|
---|
| 167 |
|
---|
[8156] | 168 |
|
---|
[6976] | 169 | private ThreadSafeLog log;
|
---|
| 170 | public ILog Log {
|
---|
| 171 | get { return log; }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public StateLogListList StateLogList {
|
---|
| 175 | get { return new StateLogListList(this.GetAllHiveTasks().Select(x => x.StateLog)); }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | #region Constructors and Cloning
|
---|
| 179 | public RefreshableJob() {
|
---|
[9933] | 180 | this.progress = new Progress();
|
---|
[8914] | 181 | this.refreshAutomatically = false;
|
---|
[6976] | 182 | this.Job = new Job();
|
---|
| 183 | this.log = new ThreadSafeLog();
|
---|
[7020] | 184 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
[6976] | 185 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
| 186 | this.HiveTasks = new ItemCollection<HiveTask>();
|
---|
| 187 | }
|
---|
[9436] | 188 | public RefreshableJob(Job hiveJob) {
|
---|
[9933] | 189 | this.progress = new Progress();
|
---|
[6976] | 190 | this.refreshAutomatically = true;
|
---|
[9436] | 191 | this.Job = hiveJob;
|
---|
[6976] | 192 | this.log = new ThreadSafeLog();
|
---|
[7020] | 193 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
[6976] | 194 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
| 195 | this.HiveTasks = new ItemCollection<HiveTask>();
|
---|
| 196 | }
|
---|
| 197 | protected RefreshableJob(RefreshableJob original, Cloner cloner) {
|
---|
| 198 | cloner.RegisterClonedObject(original, this);
|
---|
[6994] | 199 | this.Job = cloner.Clone(original.Job);
|
---|
[6976] | 200 | this.IsControllable = original.IsControllable;
|
---|
| 201 | this.log = cloner.Clone(original.log);
|
---|
| 202 | this.RefreshAutomatically = false; // do not start results polling automatically
|
---|
[7020] | 203 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
[6976] | 204 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
| 205 | this.HiveTasks = cloner.Clone(original.HiveTasks);
|
---|
| 206 | this.ExecutionTime = original.ExecutionTime;
|
---|
| 207 | this.ExecutionState = original.ExecutionState;
|
---|
| 208 | }
|
---|
| 209 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
| 210 | return new RefreshableJob(this, cloner);
|
---|
| 211 | }
|
---|
| 212 | public object Clone() {
|
---|
| 213 | return this.Clone(new Cloner());
|
---|
| 214 | }
|
---|
| 215 | #endregion
|
---|
| 216 |
|
---|
| 217 | #region JobResultPoller Events
|
---|
| 218 | public void StartResultPolling() {
|
---|
| 219 | if (jobResultPoller == null) {
|
---|
[7020] | 220 | jobResultPoller = new JobResultPoller(job.Id, Settings.Default.ResultPollingInterval);
|
---|
[6976] | 221 | RegisterResultPollingEvents();
|
---|
[8869] | 222 | jobResultPoller.AutoResumeOnException = false;
|
---|
[6976] | 223 | }
|
---|
| 224 |
|
---|
| 225 | if (!jobResultPoller.IsPolling) {
|
---|
| 226 | jobResultPoller.Start();
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | public void StopResultPolling() {
|
---|
| 231 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
[9390] | 232 | refreshAutomatically = false;
|
---|
[6976] | 233 | jobResultPoller.Stop();
|
---|
[9219] | 234 | DeregisterResultPollingEvents();
|
---|
[9390] | 235 | jobResultPoller = null;
|
---|
[6976] | 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[9390] | 239 | public void PauseResultPolling() {
|
---|
| 240 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
| 241 | jobResultPoller.Stop();
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[6976] | 245 | private void RegisterResultPollingEvents() {
|
---|
| 246 | jobResultPoller.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobResultPoller_ExceptionOccured);
|
---|
| 247 | jobResultPoller.JobResultsReceived += new EventHandler<EventArgs<IEnumerable<LightweightTask>>>(jobResultPoller_JobResultReceived);
|
---|
| 248 | jobResultPoller.IsPollingChanged += new EventHandler(jobResultPoller_IsPollingChanged);
|
---|
| 249 | }
|
---|
| 250 | private void DeregisterResultPollingEvents() {
|
---|
| 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 jobResultPoller_IsPollingChanged(object sender, EventArgs e) {
|
---|
| 256 | if (this.refreshAutomatically != jobResultPoller.IsPolling) {
|
---|
| 257 | this.refreshAutomatically = jobResultPoller.IsPolling;
|
---|
| 258 | OnRefreshAutomaticallyChanged();
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
[8871] | 261 |
|
---|
[6976] | 262 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightTask>> e) {
|
---|
[8939] | 263 | lock (jobResultReceivedLocker) {
|
---|
| 264 | foreach (LightweightTask lightweightTask in e.Value) {
|
---|
| 265 | HiveTask hiveTask = GetHiveTaskById(lightweightTask.Id);
|
---|
| 266 | if (hiveTask != null) {
|
---|
| 267 | // lastJobDataUpdate equals DateTime.MinValue right after it was uploaded. When the first results are polled, this value is updated
|
---|
[8993] | 268 | if (hiveTask.Task.State == TaskState.Offline && lightweightTask.State == TaskState.Waiting) {
|
---|
[8939] | 269 | hiveTask.Task.LastTaskDataUpdate = lightweightTask.LastTaskDataUpdate;
|
---|
| 270 | }
|
---|
[6976] | 271 |
|
---|
[8939] | 272 | hiveTask.UpdateFromLightweightJob(lightweightTask);
|
---|
[6976] | 273 |
|
---|
[8993] | 274 | 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)) {
|
---|
[8939] | 275 | log.LogMessage(string.Format("Downloading task {0}", lightweightTask.Id));
|
---|
| 276 | hiveTask.IsDownloading = true;
|
---|
| 277 | jobDownloader.DownloadTaskData(hiveTask.Task, (localJob, itemJob) => {
|
---|
| 278 | lock (downloadFinishedLocker) {
|
---|
| 279 | log.LogMessage(string.Format("Finished downloading task {0}", localJob.Id));
|
---|
| 280 | HiveTask localHiveTask = GetHiveTaskById(localJob.Id);
|
---|
[6976] | 281 |
|
---|
[8939] | 282 | if (itemJob == null) {
|
---|
| 283 | // something bad happened to this task. bad task, BAAAD task!
|
---|
| 284 | localHiveTask.IsDownloading = false;
|
---|
[6976] | 285 | } else {
|
---|
[8939] | 286 | // if the task is paused, download but don't integrate into parent optimizer (to avoid Prepare)
|
---|
| 287 | if (localJob.State == TaskState.Paused) {
|
---|
| 288 | localHiveTask.ItemTask = itemJob;
|
---|
[8871] | 289 | } else {
|
---|
[8939] | 290 | if (localJob.ParentTaskId.HasValue) {
|
---|
| 291 | HiveTask parentHiveTask = GetHiveTaskById(localJob.ParentTaskId.Value);
|
---|
| 292 | parentHiveTask.IntegrateChild(itemJob, localJob.Id);
|
---|
| 293 | } else {
|
---|
| 294 | localHiveTask.ItemTask = itemJob;
|
---|
| 295 | }
|
---|
[8871] | 296 | }
|
---|
[8939] | 297 | localHiveTask.IsDownloading = false;
|
---|
| 298 | localHiveTask.Task.LastTaskDataUpdate = lightweightTask.LastTaskDataUpdate;
|
---|
[6976] | 299 | }
|
---|
| 300 | }
|
---|
[8939] | 301 | });
|
---|
| 302 | }
|
---|
[8994] | 303 | }
|
---|
[6976] | 304 | }
|
---|
[8939] | 305 | GC.Collect(); // force GC, because .NET is too lazy here (deserialization takes a lot of memory)
|
---|
[15262] | 306 | if (IsFinished()) {
|
---|
| 307 | ExecutionState = ExecutionState.Stopped;
|
---|
[8939] | 308 | StopResultPolling();
|
---|
[15262] | 309 | } else if (IsPaused()) {
|
---|
| 310 | ExecutionState = ExecutionState.Paused;
|
---|
| 311 | StopResultPolling();
|
---|
| 312 | } else {
|
---|
| 313 | ExecutionState = ExecutionState.Started;
|
---|
[8939] | 314 | }
|
---|
| 315 | UpdateTotalExecutionTime();
|
---|
| 316 | UpdateStatistics();
|
---|
| 317 | OnStateLogListChanged();
|
---|
| 318 | OnTaskReceived();
|
---|
[6976] | 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[7218] | 322 | public HiveTask GetHiveTaskById(Guid jobId) {
|
---|
| 323 | foreach (HiveTask t in this.HiveTasks) {
|
---|
| 324 | var hj = t.GetHiveTaskByTaskId(jobId);
|
---|
[6976] | 325 | if (hj != null)
|
---|
| 326 | return hj;
|
---|
| 327 | }
|
---|
| 328 | return null;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | private void UpdateStatistics() {
|
---|
| 332 | var jobs = this.GetAllHiveTasks();
|
---|
| 333 | job.JobCount = jobs.Count();
|
---|
| 334 | job.CalculatingCount = jobs.Count(j => j.Task.State == TaskState.Calculating);
|
---|
| 335 | job.FinishedCount = jobs.Count(j => j.Task.State == TaskState.Finished);
|
---|
| 336 | OnJobStatisticsChanged();
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
[7409] | 340 | OnExceptionOccured(sender, e.Value);
|
---|
[6976] | 341 | }
|
---|
| 342 | private void jobDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
[7409] | 343 | OnExceptionOccured(sender, e.Value);
|
---|
[6976] | 344 | }
|
---|
| 345 | public void UpdateTotalExecutionTime() {
|
---|
| 346 | this.ExecutionTime = TimeSpan.FromMilliseconds(this.GetAllHiveTasks().Sum(x => x.Task.ExecutionTime.TotalMilliseconds));
|
---|
| 347 | }
|
---|
| 348 | #endregion
|
---|
| 349 |
|
---|
| 350 | #region Job Events
|
---|
| 351 | private void RegisterJobEvents() {
|
---|
| 352 | job.ToStringChanged += new EventHandler(OnToStringChanged);
|
---|
| 353 | job.PropertyChanged += new PropertyChangedEventHandler(job_PropertyChanged);
|
---|
| 354 | job.ItemImageChanged += new EventHandler(job_ItemImageChanged);
|
---|
| 355 | job.ModifiedChanged += new EventHandler(job_ModifiedChanged);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[9219] | 358 | private void DeregisterJobEvents() {
|
---|
[6976] | 359 | job.ToStringChanged -= new EventHandler(OnToStringChanged);
|
---|
| 360 | job.PropertyChanged -= new PropertyChangedEventHandler(job_PropertyChanged);
|
---|
| 361 | job.ItemImageChanged -= new EventHandler(job_ItemImageChanged);
|
---|
| 362 | job.ModifiedChanged -= new EventHandler(job_ModifiedChanged);
|
---|
| 363 | }
|
---|
| 364 | #endregion
|
---|
| 365 |
|
---|
| 366 | #region Event Handler
|
---|
| 367 | public event EventHandler RefreshAutomaticallyChanged;
|
---|
| 368 | private void OnRefreshAutomaticallyChanged() {
|
---|
| 369 | var handler = RefreshAutomaticallyChanged;
|
---|
| 370 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | public event EventHandler JobChanged;
|
---|
| 374 | private void OnJobChanged() {
|
---|
| 375 | var handler = JobChanged;
|
---|
| 376 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | public event EventHandler ModifiedChanged;
|
---|
| 380 | private void job_ModifiedChanged(object sender, EventArgs e) {
|
---|
| 381 | var handler = ModifiedChanged;
|
---|
| 382 | if (handler != null) handler(sender, e);
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | public event EventHandler ItemImageChanged;
|
---|
| 386 | private void job_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 387 | var handler = ItemImageChanged;
|
---|
| 388 | if (handler != null) handler(this, e);
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 392 | private void job_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 393 | this.IsSharable = job.Permission == Permission.Full;
|
---|
| 394 | this.IsControllable = job.Permission == Permission.Full;
|
---|
| 395 |
|
---|
| 396 | var handler = PropertyChanged;
|
---|
| 397 | if (handler != null) handler(sender, e);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | public event EventHandler ToStringChanged;
|
---|
| 401 | private void OnToStringChanged(object sender, EventArgs e) {
|
---|
| 402 | var handler = ToStringChanged;
|
---|
| 403 | if (handler != null) handler(this, e);
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | public event EventHandler IsDownloadableChanged;
|
---|
| 407 | private void OnIsDownloadableChanged() {
|
---|
| 408 | var handler = IsDownloadableChanged;
|
---|
| 409 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | public event EventHandler IsControllableChanged;
|
---|
| 413 | private void OnIsControllableChanged() {
|
---|
| 414 | var handler = IsControllableChanged;
|
---|
| 415 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | public event EventHandler IsSharableChanged;
|
---|
| 419 | private void OnIsSharableChanged() {
|
---|
| 420 | var handler = IsSharableChanged;
|
---|
| 421 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | public event EventHandler JobStatisticsChanged;
|
---|
| 425 | private void OnJobStatisticsChanged() {
|
---|
| 426 | var handler = JobStatisticsChanged;
|
---|
| 427 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
[7409] | 431 | private void OnExceptionOccured(object sender, Exception exception) {
|
---|
[6976] | 432 | log.LogException(exception);
|
---|
| 433 | var handler = ExceptionOccured;
|
---|
[7409] | 434 | if (handler != null) handler(sender, new EventArgs<Exception>(exception));
|
---|
[6976] | 435 | }
|
---|
| 436 |
|
---|
| 437 | public event EventHandler StateLogListChanged;
|
---|
| 438 | private void OnStateLogListChanged() {
|
---|
| 439 | var handler = StateLogListChanged;
|
---|
| 440 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | public event EventHandler ExecutionTimeChanged;
|
---|
| 444 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 445 | var handler = ExecutionTimeChanged;
|
---|
| 446 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | public event EventHandler ExecutionStateChanged;
|
---|
| 450 | protected virtual void OnExecutionStateChanged() {
|
---|
| 451 | var handler = ExecutionStateChanged;
|
---|
| 452 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 453 | }
|
---|
[7782] | 454 | public event EventHandler TaskReceived;
|
---|
| 455 | protected virtual void OnTaskReceived() {
|
---|
| 456 | var handler = TaskReceived;
|
---|
| 457 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 458 | }
|
---|
[8156] | 459 | public event EventHandler IsProgressingChanged;
|
---|
| 460 | private void OnIsProgressingChanged() {
|
---|
| 461 | var handler = IsProgressingChanged;
|
---|
| 462 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 463 | }
|
---|
[6976] | 464 | #endregion
|
---|
| 465 |
|
---|
| 466 | #region HiveTasks Events
|
---|
[9219] | 467 | private void RegisterHiveTasksEvents() {
|
---|
[6976] | 468 | this.hiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
|
---|
| 469 | this.hiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
|
---|
| 470 | this.hiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_CollectionReset);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[9219] | 473 | private void DeregisterHiveTasksEvents() {
|
---|
[6976] | 474 | this.hiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(hivetasks_ItemsAdded);
|
---|
| 475 | this.hiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_ItemsRemoved);
|
---|
| 476 | this.hiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<HiveTask>(hiveTasks_CollectionReset);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | private void hiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 480 | foreach (var item in e.Items) {
|
---|
| 481 | item.StateLogChanged -= new EventHandler(item_StateLogChanged);
|
---|
| 482 | }
|
---|
| 483 | OnHiveTasksReset(e);
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | private void hiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 487 | foreach (var item in e.Items) {
|
---|
| 488 | item.StateLogChanged -= new EventHandler(item_StateLogChanged);
|
---|
| 489 | }
|
---|
| 490 | OnHiveTasksRemoved(e);
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | private void hivetasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 494 | foreach (var item in e.Items) {
|
---|
| 495 | item.StateLogChanged += new EventHandler(item_StateLogChanged);
|
---|
| 496 | item.IsControllable = this.IsControllable;
|
---|
| 497 | }
|
---|
| 498 | OnHiveTasksAdded(e);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | private void item_StateLogChanged(object sender, EventArgs e) {
|
---|
| 502 | OnStateLogListChanged();
|
---|
| 503 | }
|
---|
| 504 | #endregion
|
---|
| 505 |
|
---|
| 506 | public event EventHandler HiveTasksChanged;
|
---|
| 507 | protected virtual void OnHiveTasksChanged() {
|
---|
| 508 | if (this.HiveTasks != null && this.HiveTasks.Count > 0 && this.GetAllHiveTasks().All(x => x.Task.Id != Guid.Empty)) {
|
---|
| 509 | if (IsFinished()) {
|
---|
| 510 | this.ExecutionState = Core.ExecutionState.Stopped;
|
---|
| 511 | this.RefreshAutomatically = false;
|
---|
[9390] | 512 | StopResultPolling();
|
---|
[15262] | 513 | } else if (IsPaused()) {
|
---|
| 514 | this.executionState = Core.ExecutionState.Paused;
|
---|
| 515 | this.RefreshAutomatically = false;
|
---|
| 516 | StopResultPolling();
|
---|
[8090] | 517 | } else {
|
---|
| 518 | this.RefreshAutomatically = true;
|
---|
[6976] | 519 | }
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | var handler = HiveTasksChanged;
|
---|
| 523 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | public event EventHandler Loaded;
|
---|
| 527 | public virtual void OnLoaded() {
|
---|
| 528 | this.UpdateTotalExecutionTime();
|
---|
[7056] | 529 | this.OnStateLogListChanged();
|
---|
[6976] | 530 |
|
---|
[15262] | 531 | if (this.ExecutionState != ExecutionState.Stopped && this.ExecutionState != ExecutionState.Paused) {
|
---|
[6976] | 532 | this.RefreshAutomatically = true;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | var handler = Loaded;
|
---|
| 536 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksAdded;
|
---|
| 540 | private void OnHiveTasksAdded(CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 541 | var handler = HiveTasksAdded;
|
---|
| 542 | if (handler != null) handler(this, e);
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksRemoved;
|
---|
| 546 | private void OnHiveTasksRemoved(CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 547 | var handler = HiveTasksRemoved;
|
---|
| 548 | if (handler != null) handler(this, e);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | public event EventHandler<CollectionItemsChangedEventArgs<HiveTask>> HiveTasksReset;
|
---|
| 552 | private void OnHiveTasksReset(CollectionItemsChangedEventArgs<HiveTask> e) {
|
---|
| 553 | var handler = HiveTasksReset;
|
---|
| 554 | if (handler != null) handler(this, e);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | public Guid Id {
|
---|
[9219] | 558 | get {
|
---|
| 559 | if (job == null) return Guid.Empty;
|
---|
| 560 | return job.Id;
|
---|
| 561 | }
|
---|
[6976] | 562 | set { job.Id = value; }
|
---|
| 563 | }
|
---|
| 564 | public bool Modified {
|
---|
| 565 | get { return job.Modified; }
|
---|
| 566 | }
|
---|
| 567 | public void Store() {
|
---|
| 568 | job.Store();
|
---|
| 569 | }
|
---|
| 570 | public string ItemDescription {
|
---|
| 571 | get { return job.ItemDescription; }
|
---|
| 572 | }
|
---|
| 573 | public Image ItemImage {
|
---|
| 574 | get { return job.ItemImage; }
|
---|
| 575 | }
|
---|
| 576 | public string ItemName {
|
---|
| 577 | get { return job.ItemName; }
|
---|
| 578 | }
|
---|
| 579 | public Version ItemVersion {
|
---|
| 580 | get { return job.ItemVersion; }
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | public override string ToString() {
|
---|
| 584 | return string.Format("{0} {1}", Job.DateCreated.ToString("MM.dd.yyyy HH:mm"), Job.ToString());
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | public bool IsFinished() {
|
---|
[15262] | 588 | var tasks = GetAllHiveTasks();
|
---|
| 589 | return tasks.All(x => x.Task.State == TaskState.Finished
|
---|
| 590 | || x.Task.State == TaskState.Aborted
|
---|
| 591 | || x.Task.State == TaskState.Failed);
|
---|
[6976] | 592 | }
|
---|
| 593 |
|
---|
[15262] | 594 | public bool IsPaused() {
|
---|
| 595 | var tasks = GetAllHiveTasks().Where(x => !x.Task.IsParentTask);
|
---|
| 596 | return tasks.All(x => x.Task.State != TaskState.Waiting
|
---|
| 597 | && x.Task.State != TaskState.Transferring
|
---|
| 598 | && x.Task.State != TaskState.Calculating)
|
---|
| 599 | && tasks.Any(x => x.Task.State == TaskState.Paused);
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[6976] | 602 | public IEnumerable<HiveTask> GetAllHiveTasks() {
|
---|
| 603 | if (hiveTasks == null) return Enumerable.Empty<HiveTask>();
|
---|
| 604 |
|
---|
| 605 | var tasks = new List<HiveTask>();
|
---|
| 606 | foreach (HiveTask task in HiveTasks) {
|
---|
| 607 | tasks.AddRange(task.GetAllHiveTasks());
|
---|
| 608 | }
|
---|
| 609 | return tasks;
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | public int CompareTo(RefreshableJob other) {
|
---|
| 613 | return this.ToString().CompareTo(other.ToString());
|
---|
| 614 | }
|
---|
[9219] | 615 |
|
---|
| 616 | public void Unload() {
|
---|
| 617 | // stop result polling
|
---|
| 618 | if (refreshAutomatically)
|
---|
| 619 | RefreshAutomatically = false;
|
---|
| 620 | DisposeTasks();
|
---|
| 621 | hiveTasks = new ItemCollection<HiveTask>();
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | #region IDisposable Members
|
---|
| 625 | public void Dispose() {
|
---|
| 626 | if (jobDownloader != null) {
|
---|
| 627 | jobDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
| 628 | jobDownloader.Dispose();
|
---|
| 629 | jobDownloader = null;
|
---|
| 630 | }
|
---|
| 631 | if (jobResultPoller != null) {
|
---|
| 632 | DeregisterResultPollingEvents();
|
---|
| 633 | jobResultPoller = null;
|
---|
| 634 | }
|
---|
| 635 | if (hiveTasks != null) {
|
---|
| 636 | DisposeTasks();
|
---|
| 637 | }
|
---|
| 638 | if (job != null) {
|
---|
| 639 | DeregisterJobEvents();
|
---|
| 640 | job = null;
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | private void DisposeTasks() {
|
---|
| 645 | DeregisterHiveTasksEvents();
|
---|
| 646 | foreach (var task in hiveTasks) {
|
---|
| 647 | task.Dispose();
|
---|
| 648 | }
|
---|
| 649 | hiveTasks.Clear(); // this should remove the item_StateLogChanged event handlers
|
---|
| 650 | hiveTasks = null;
|
---|
| 651 | }
|
---|
| 652 | #endregion
|
---|
[6976] | 653 | }
|
---|
| 654 | }
|
---|