Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Clients.Hive/3.4/HiveExperiment/JobResultPoller.cs @ 5041

Last change on this file since 5041 was 4629, checked in by cneumuel, 14 years ago
  • worked on new hive structure
  • created IIS hostable website for hive (old structure)

(#1233)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Services.Hive.Common;
26using HeuristicLab.Services.Hive.Common.ServiceContracts;
27using HeuristicLab.Services.Hive.Common.DataTransfer;
28using System.Collections.Generic;
29
30namespace HeuristicLab.Clients.Hive {
31  internal class JobResultPoller {
32    private bool stopRequested { get; set; }
33    private AutoResetEvent waitHandle;
34    private Thread thread;
35
36    private HiveJob hiveJob;
37    public HiveJob HiveJob {
38      get { return hiveJob; }
39      set { hiveJob = value; }
40    }
41
42    private TimeSpan interval;
43    public TimeSpan Interval {
44      get { return interval; }
45      set { interval = value; }
46    }
47
48    private bool isPolling;
49    public bool IsPolling {
50      get { return isPolling; }
51      set {
52        if (isPolling != value) {
53          isPolling = value;
54          OnIsPollingChanged();
55        }
56      }
57    }
58
59    public JobResultPoller(HiveJob hiveJob, TimeSpan interval) {
60      this.isPolling = false;
61      this.hiveJob = hiveJob;
62      this.interval = interval;
63    }
64
65    public void Start() {
66      stopRequested = false;
67      thread = new Thread(RunPolling);
68      thread.Start();
69      IsPolling = true;
70    }
71
72    public void Stop() {
73      // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
74      stopRequested = true;
75      waitHandle.Set();
76      IsPolling = false;
77      thread = null;
78    }
79
80    public void RunPolling() {
81      try {
82        waitHandle = new AutoResetEvent(false);
83        while (!stopRequested) {
84          OnPollingStarted();
85          FetchJobResults();
86          OnPollingFinished();
87          waitHandle.WaitOne(Interval);
88        }
89        waitHandle.Close();
90      }
91      catch (Exception e) {
92        OnExceptionOccured(e);
93      }
94    }
95
96    private void FetchJobResults() {
97      using (Disposable<IHiveService> service = ServiceLocator.Instance.ServicePool.GetService()) {
98        IEnumerable<LightweightJob> response = service.Obj.GetLightweightChildJobs(hiveJob.Job.Id, true, true);
99        OnJobResultsReceived(response);
100      }
101    }
102
103    public event EventHandler<EventArgs<IEnumerable<LightweightJob>>> JobResultsReceived;
104    private void OnJobResultsReceived(IEnumerable<LightweightJob> lightweightJobs) {
105      var handler = JobResultsReceived;
106      if (handler != null) handler(this, new EventArgs<IEnumerable<LightweightJob>>(lightweightJobs));
107    }
108
109    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
110    private void OnExceptionOccured(Exception e) {
111      var handler = ExceptionOccured;
112      if (handler != null) handler(this, new EventArgs<Exception>(e));
113    }
114
115    public event EventHandler IsPollingChanged;
116    private void OnIsPollingChanged() {
117      var handler = IsPollingChanged;
118      if (handler != null) handler(this, EventArgs.Empty);
119    }
120    public event EventHandler PollingStarted;
121    private void OnPollingStarted() {
122      var handler = PollingStarted;
123      if (handler != null) handler(this, EventArgs.Empty);
124    }
125    public event EventHandler PollingFinished;
126    private void OnPollingFinished() {
127      var handler = PollingFinished;
128      if (handler != null) handler(this, EventArgs.Empty);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.