1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Clients.Hive {
|
---|
33 | public class RefreshableJob : IHiveItem, IDeepCloneable, IContent, IComparable<RefreshableJob> {
|
---|
34 | private JobResultPoller jobResultPoller;
|
---|
35 | private ConcurrentTaskDownloader<ItemTask> jobDownloader;
|
---|
36 | private object locker = new object();
|
---|
37 | private object downloadFinishedLocker = new object();
|
---|
38 | object jobResultReceivedLocker = new object();
|
---|
39 |
|
---|
40 | public bool IsProgressing { get; set; }
|
---|
41 |
|
---|
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 |
|
---|
50 | if (job != null) DergisterJobEvents();
|
---|
51 | job = value;
|
---|
52 | if (job != null) {
|
---|
53 | RegisterJobEvents();
|
---|
54 | job_PropertyChanged(job, new PropertyChangedEventArgs("Id"));
|
---|
55 | }
|
---|
56 | OnJobChanged();
|
---|
57 | OnToStringChanged(this, EventArgs.Empty);
|
---|
58 | job_ItemImageChanged(this, EventArgs.Empty);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private ItemCollection<HiveTask> hiveTasks;
|
---|
64 | public ItemCollection<HiveTask> HiveTasks {
|
---|
65 | get { return hiveTasks; }
|
---|
66 | set {
|
---|
67 | if (hiveTasks != value) {
|
---|
68 | if (hiveTasks != null) DeregisterHiveJobsEvents();
|
---|
69 | hiveTasks = value;
|
---|
70 | if (hiveTasks != null) RegisterHiveJobsEvents();
|
---|
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 | private bool isAllowedPrivileged = true;
|
---|
161 | public bool IsAllowedPrivileged {
|
---|
162 | get { return isAllowedPrivileged; }
|
---|
163 | set {
|
---|
164 | if (value != isAllowedPrivileged) {
|
---|
165 | isAllowedPrivileged = value;
|
---|
166 | OnIsAllowedPrivilegedChanged();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private Progress progress;
|
---|
172 | public Progress Progress {
|
---|
173 | get { return progress; }
|
---|
174 | set {
|
---|
175 | this.progress = value;
|
---|
176 | OnIsProgressingChanged();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | private ThreadSafeLog log;
|
---|
182 | public ILog Log {
|
---|
183 | get { return log; }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public StateLogListList StateLogList {
|
---|
187 | get { return new StateLogListList(this.GetAllHiveTasks().Select(x => x.StateLog)); }
|
---|
188 | }
|
---|
189 |
|
---|
190 | #region Constructors and Cloning
|
---|
191 | public RefreshableJob() {
|
---|
192 | this.refreshAutomatically = false;
|
---|
193 | this.Job = new Job();
|
---|
194 | this.log = new ThreadSafeLog();
|
---|
195 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
196 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
197 | this.HiveTasks = new ItemCollection<HiveTask>();
|
---|
198 | }
|
---|
199 | public RefreshableJob(Job hiveExperiment) {
|
---|
200 | this.refreshAutomatically = true;
|
---|
201 | this.Job = hiveExperiment;
|
---|
202 | this.log = new ThreadSafeLog();
|
---|
203 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
204 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
205 | this.HiveTasks = new ItemCollection<HiveTask>();
|
---|
206 | }
|
---|
207 | protected RefreshableJob(RefreshableJob original, Cloner cloner) {
|
---|
208 | cloner.RegisterClonedObject(original, this);
|
---|
209 | this.Job = cloner.Clone(original.Job);
|
---|
210 | this.IsControllable = original.IsControllable;
|
---|
211 | this.log = cloner.Clone(original.log);
|
---|
212 | this.RefreshAutomatically = false; // do not start results polling automatically
|
---|
213 | this.jobDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
214 | this.jobDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(jobDownloader_ExceptionOccured);
|
---|
215 | this.HiveTasks = cloner.Clone(original.HiveTasks);
|
---|
216 | this.ExecutionTime = original.ExecutionTime;
|
---|
217 | this.ExecutionState = original.ExecutionState;
|
---|
218 | }
|
---|
219 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
220 | return new RefreshableJob(this, cloner);
|
---|
221 | }
|
---|
222 | public object Clone() {
|
---|
223 | return this.Clone(new Cloner());
|
---|
224 | }
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | #region JobResultPoller Events
|
---|
228 | public void StartResultPolling() {
|
---|
229 | if (jobResultPoller == null) {
|
---|
230 | jobResultPoller = new JobResultPoller(job.Id, Settings.Default.ResultPollingInterval);
|
---|
231 | RegisterResultPollingEvents();
|
---|
232 | jobResultPoller.AutoResumeOnException = false;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (!jobResultPoller.IsPolling) {
|
---|
236 | jobResultPoller.Start();
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | public void StopResultPolling() {
|
---|
241 | if (jobResultPoller != null && jobResultPoller.IsPolling) {
|
---|
242 | jobResultPoller.Stop();
|
---|
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 | }
|
---|
262 |
|
---|
263 | private void jobResultPoller_JobResultReceived(object sender, EventArgs<IEnumerable<LightweightTask>> e) {
|
---|
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;
|
---|
286 | } else {
|
---|
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;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | });
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
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 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | public HiveTask GetHiveTaskById(Guid jobId) {
|
---|
319 | foreach (HiveTask t in this.HiveTasks) {
|
---|
320 | var hj = t.GetHiveTaskByTaskId(jobId);
|
---|
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)
|
---|
339 | && !j.IsDownloading);
|
---|
340 | }
|
---|
341 |
|
---|
342 | private void jobResultPoller_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
343 | OnExceptionOccured(sender, e.Value);
|
---|
344 | }
|
---|
345 | private void jobDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
346 | OnExceptionOccured(sender, e.Value);
|
---|
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 |
|
---|
361 | private void DergisterJobEvents() {
|
---|
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;
|
---|
440 | private void OnExceptionOccured(object sender, Exception exception) {
|
---|
441 | log.LogException(exception);
|
---|
442 | var handler = ExceptionOccured;
|
---|
443 | if (handler != null) handler(sender, new EventArgs<Exception>(exception));
|
---|
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 | }
|
---|
463 | public event EventHandler TaskReceived;
|
---|
464 | protected virtual void OnTaskReceived() {
|
---|
465 | var handler = TaskReceived;
|
---|
466 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
467 | }
|
---|
468 | public event EventHandler IsProgressingChanged;
|
---|
469 | private void OnIsProgressingChanged() {
|
---|
470 | var handler = IsProgressingChanged;
|
---|
471 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
472 | }
|
---|
473 | #endregion
|
---|
474 |
|
---|
475 | #region HiveTasks Events
|
---|
476 | private void RegisterHiveJobsEvents() {
|
---|
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 |
|
---|
482 | private void DeregisterHiveJobsEvents() {
|
---|
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() {
|
---|
517 | StopResultPolling();
|
---|
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;
|
---|
522 | if (jobResultPoller != null) DeregisterResultPollingEvents();
|
---|
523 | } else {
|
---|
524 | this.RefreshAutomatically = true;
|
---|
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();
|
---|
535 | this.OnStateLogListChanged();
|
---|
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 {
|
---|
564 | get { return job.Id; }
|
---|
565 | set { job.Id = value; }
|
---|
566 | }
|
---|
567 | public bool Modified {
|
---|
568 | get { return job.Modified; }
|
---|
569 | }
|
---|
570 | public void Store() {
|
---|
571 | job.Store();
|
---|
572 | }
|
---|
573 | public string ItemDescription {
|
---|
574 | get { return job.ItemDescription; }
|
---|
575 | }
|
---|
576 | public Image ItemImage {
|
---|
577 | get { return job.ItemImage; }
|
---|
578 | }
|
---|
579 | public string ItemName {
|
---|
580 | get { return job.ItemName; }
|
---|
581 | }
|
---|
582 | public Version ItemVersion {
|
---|
583 | get { return job.ItemVersion; }
|
---|
584 | }
|
---|
585 |
|
---|
586 | public override string ToString() {
|
---|
587 | return string.Format("{0} {1}", Job.DateCreated.ToString("MM.dd.yyyy HH:mm"), Job.ToString());
|
---|
588 | }
|
---|
589 |
|
---|
590 | public bool IsFinished() {
|
---|
591 | return HiveTasks != null
|
---|
592 | && HiveTasks.All(x => x.Task.DateFinished.HasValue && x.Task.DateCreated.HasValue);
|
---|
593 | }
|
---|
594 |
|
---|
595 | public IEnumerable<HiveTask> GetAllHiveTasks() {
|
---|
596 | if (hiveTasks == null) return Enumerable.Empty<HiveTask>();
|
---|
597 |
|
---|
598 | var tasks = new List<HiveTask>();
|
---|
599 | foreach (HiveTask task in HiveTasks) {
|
---|
600 | tasks.AddRange(task.GetAllHiveTasks());
|
---|
601 | }
|
---|
602 | return tasks;
|
---|
603 | }
|
---|
604 |
|
---|
605 | public int CompareTo(RefreshableJob other) {
|
---|
606 | return this.ToString().CompareTo(other.ToString());
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|