Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HiveExperiment/JobResultPoller.cs @ 5055

Last change on this file since 5055 was 5055, checked in by cneumuel, 13 years ago

#1233

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