Bladeren bron

implemented timer service and refactored background worker

Bryan Allred 10 jaren geleden
bovenliggende
commit
30c6e86f61
3 gewijzigde bestanden met toevoegingen van 169 en 104 verwijderingen
  1. 142 0
      PollingService.cs
  2. 24 104
      Program.cs
  3. 3 0
      pulse.csproj

+ 142 - 0
PollingService.cs

@ -0,0 +1,142 @@
1
using System;
2
using System.ComponentModel;
3
using System.Diagnostics;
4
using System.Linq;
5
using Microsoft.TeamFoundation.Client;
6
using Microsoft.TeamFoundation.Framework.Common;
7
using Microsoft.TeamFoundation.VersionControl.Client;
8
using Microsoft.TeamFoundation.WorkItemTracking.Client;
9
10
namespace pulse
11
{
12
	/// <summary>
13
	/// The polling service.
14
	/// </summary>
15
	public class PollingService : BackgroundWorker
16
	{
17
		/// <summary>
18
		/// Gets or sets the team foundation server.
19
		/// </summary>
20
		/// <value>
21
		/// The team foundation server.
22
		/// </value>
23
		public TfsConfigurationServer TeamFoundationServer { get; set; }
24
		
25
		/// <summary>
26
		/// Gets or sets the dashboard.
27
		/// </summary>
28
		/// <value>
29
		/// The dashboard.
30
		/// </value>
31
		public Dashboard Dashboard { get; set; }
32
33
		/// <summary>
34
		/// Initializes a new instance of the <see cref="PollingService"/> class.
35
		/// </summary>
36
		/// <param name="tfs">The TFS.</param>
37
		/// <param name="dashboard">The dashboard.</param>
38
		public PollingService(TfsConfigurationServer tfs, Dashboard dashboard)
39
		{
40
			this.TeamFoundationServer = tfs;
41
			this.Dashboard = dashboard;
42
		}
43
44
		/// <summary>
45
		/// Raises the <see cref="E:System.ComponentModel.BackgroundWorker.DoWork" /> event.
46
		/// </summary>
47
		/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
48
		protected override void OnDoWork(DoWorkEventArgs e)
49
		{
50
			base.OnDoWork(e);
51
52
			var now = DateTime.Now;
53
			this.Dashboard.ClearHighlights();
54
			Debug.WriteLine("Worker started at " + DateTime.Now);
55
56
			if (e.Cancel)
57
			{
58
				return;
59
			}
60
61
			// Get all the project collections.
62
			var collections = this.TeamFoundationServer.CatalogNode.QueryChildren(
63
				new[] { CatalogResourceTypes.ProjectCollection },
64
				false,
65
				CatalogQueryOptions.None);
66
67
			foreach (var collection in collections)
68
			{
69
				if (e.Cancel)
70
				{
71
					return;
72
				}
73
74
				// Find the project collection.
75
				var projectCollectionId = new Guid(collection.Resource.Properties["InstanceId"]);
76
				var projectCollection = this.TeamFoundationServer.GetTeamProjectCollection(projectCollectionId);
77
78
				if (e.Cancel)
79
				{
80
					return;
81
				}
82
83
				// Get the projects.
84
				var projects = projectCollection.CatalogNode.QueryChildren(
85
					new[] { CatalogResourceTypes.TeamProject },
86
					false,
87
					CatalogQueryOptions.None);
88
				this.Dashboard.AddProjects(projects.ToArray());
89
90
				if (e.Cancel)
91
				{
92
					return;
93
				}
94
95
				// Get the work history.
96
				var workItemStore = projectCollection.GetService<WorkItemStore>();
97
				var workItemCollection = workItemStore.Query("select * from workitems order by [changed date] asc");
98
				if (this.Dashboard.DateRefresh.HasValue)
99
				{
100
					this.Dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>().Where(x => x.ChangedDate > this.Dashboard.DateRefresh.Value));
101
				}
102
				else
103
				{
104
					this.Dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>());
105
				}
106
107
				if (e.Cancel)
108
				{
109
					return;
110
				}
111
112
				// Get the commit log.
113
				var versionControlServer = projectCollection.GetService<VersionControlServer>();
114
				var commits = versionControlServer.QueryHistory("$/", RecursionType.Full);
115
				if (this.Dashboard.DateRefresh.HasValue)
116
				{
117
					this.Dashboard.AddCommits(commits.Where(x => x.CreationDate > this.Dashboard.DateRefresh.Value));
118
				}
119
				else
120
				{
121
					this.Dashboard.AddCommits(commits);
122
				}
123
			}
124
125
			e.Result = now;
126
		}
127
128
		/// <summary>
129
		/// Raises the <see cref="E:System.ComponentModel.BackgroundWorker.RunWorkerCompleted" /> event.
130
		/// </summary>
131
		/// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
132
		protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
133
		{
134
			base.OnRunWorkerCompleted(e);
135
136
			Debug.WriteLine("Worker completed at " + DateTime.Now);
137
138
			this.Dashboard.DateRefresh = (DateTime?)e.Result;
139
			this.Dashboard.Cache();
140
		}
141
	}
142
}

+ 24 - 104
Program.cs

@ -1,12 +1,7 @@
1 1
using System;
2
using System.ComponentModel;
3 2
using System.Diagnostics;
4
using System.Linq;
5 3
using System.Threading;
6 4
using Microsoft.TeamFoundation.Client;
7
using Microsoft.TeamFoundation.Framework.Common;
8
using Microsoft.TeamFoundation.VersionControl.Client;
9
using Microsoft.TeamFoundation.WorkItemTracking.Client;
10 5
11 6
namespace pulse
12 7
{
@ -35,109 +30,20 @@ namespace pulse
35 30
			configurationServer.EnsureAuthenticated();
36 31
37 32
			// Start the TFS worker.
38
			var worker = new BackgroundWorker();
39
			worker.DoWork += (o, a) =>
40
			{
41
				now = DateTime.Now;
42
				dashboard.ClearHighlights();
43
				Debug.WriteLine("Worker started at " + DateTime.Now);
44
45
				if (a.Cancel)
46
				{
47
					return;
48
				}
49
50
				// Get all the project collections.
51
				var collections = configurationServer.CatalogNode.QueryChildren(
52
					new[] { CatalogResourceTypes.ProjectCollection },
53
					false,
54
					CatalogQueryOptions.None);
55
56
				foreach (var collection in collections)
57
				{
58
					if (a.Cancel)
59
					{
60
						return;
61
					}
62
63
					// Find the project collection.
64
					var projectCollectionId = new Guid(collection.Resource.Properties["InstanceId"]);
65
					var projectCollection = configurationServer.GetTeamProjectCollection(projectCollectionId);
66
67
					if (a.Cancel)
68
					{
69
						return;
70
					}
71
72
					// Get the projects.
73
					var projects = projectCollection.CatalogNode.QueryChildren(
74
						new[] { CatalogResourceTypes.TeamProject },
75
						false,
76
						CatalogQueryOptions.None);
77
					dashboard.AddProjects(projects.ToArray());
78
79
					if (a.Cancel)
80
					{
81
						return;
82
					}
83
84
					// Get the work history.
85
					var workItemStore = projectCollection.GetService<WorkItemStore>();
86
					var workItemCollection = workItemStore.Query("select * from workitems order by [changed date] asc");
87
					if (dashboard.DateRefresh.HasValue)
88
					{
89
						dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>().Where(x => x.ChangedDate > dashboard.DateRefresh.Value));
90
					}
91
					else
92
					{
93
						dashboard.AddWorkItems(workItemCollection.Cast<WorkItem>());
94
					}
95
96
					if (a.Cancel)
97
					{
98
						return;
99
					}
100
101
					// Get the commit log.
102
					var versionControlServer = projectCollection.GetService<VersionControlServer>();
103
					var commits = versionControlServer.QueryHistory("$/", RecursionType.Full);
104
					if (dashboard.DateRefresh.HasValue)
105
					{
106
						dashboard.AddCommits(commits.Where(x => x.CreationDate > dashboard.DateRefresh.Value));
107
					}
108
					else
109
					{
110
						dashboard.AddCommits(commits);
111
					}
112
				}
113
			};
114
			worker.RunWorkerCompleted += (o, a) =>
115
			{
116
				Debug.WriteLine("Worker completed at " + DateTime.Now);
33
			var worker = new PollingService(configurationServer, dashboard);
117 34
118
				dashboard.DateRefresh = now;
119
				dashboard.Cache();
120
				dashboard.Update();
121
122
				// Play any queued sounds.
123
				while (Sound.Queue.Count > 0)
124
				{
125
					Sound.Queue.Dequeue()();
126
				}
127
128
				var pause = new AutoResetEvent(false);
129
				if (pause.WaitOne(new TimeSpan(0, 15, 0)))
130
				{
35
			// Create a timer to run this in an interval.
36
			var timer = new Timer(
37
				(e) => {
131 38
					worker.RunWorkerAsync();
132
				}
133
			};
134
			worker.RunWorkerAsync();
135
39
				},
40
				null,
41
				new TimeSpan(0, 0, 0, 0, 200),
42
				new TimeSpan(0, 15, 0));
43
			
136 44
			var running = true;
137 45
			while (running)
138 46
			{
139
				dashboard.Update();
140
141 47
				// Listen for key events.
142 48
				if (Console.KeyAvailable)
143 49
				{
@ -184,8 +90,17 @@ namespace pulse
184 90
					}
185 91
				}
186 92
187
				if (running && autoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 0, 100)))
93
				if (running)
188 94
				{
95
					autoResetEvent.WaitOne(new TimeSpan(0, 0, 0, 0, 100));
96
					dashboard.Update();
97
98
					// Play any queued sounds.
99
					while (Sound.Queue.Count > 0)
100
					{
101
						Sound.Queue.Dequeue()();
102
					}
103
189 104
					autoResetEvent.Reset();
190 105
				}
191 106
			}
@ -199,6 +114,11 @@ namespace pulse
199 114
				}
200 115
				catch
201 116
				{
117
					// Stub.
118
				}
119
				finally
120
				{
121
					timer.Dispose();
202 122
				}
203 123
			}
204 124
		}

+ 3 - 0
pulse.csproj

@ -43,6 +43,9 @@
43 43
  <ItemGroup>
44 44
    <Compile Include="Dashboard.cs" />
45 45
    <Compile Include="Penalties.cs" />
46
    <Compile Include="PollingService.cs">
47
      <SubType>Component</SubType>
48
    </Compile>
46 49
    <Compile Include="Program.cs" />
47 50
    <Compile Include="Properties\AssemblyInfo.cs" />
48 51
    <Compile Include="Rewards.cs" />