Author: marisic.net
(2008/09/25 18:33) Over 3 years ago
11
12
namespace CacheManager
13
{
14
public class TaskList : IList<Task>
public class TaskList : IList<ITask>
15
16
//How long for jobs to wait before waking.
17
private const double sleepLength = 1;
18
private readonly Cache _cache = HttpRuntime.Cache;
19
private readonly List<Task> _tasks = new List<Task>();
private readonly List<ITask> _tasks = new List<ITask>();
20
21
public List<Task> Tasks
public List<ITask> Tasks
22
23
get
24
...
27
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
28
do
29
30
if (enumerator.Current is Task)
if (enumerator.Current is ITask)
31
_tasks.Add(enumerator.Current as Task);
_tasks.Add(enumerator.Current as ITask);
32
} while (enumerator.MoveNext());
33
}
34
return _tasks;
39
40
#region Implementation of IEnumerable
41
42
public IEnumerator<Task> GetEnumerator()
public IEnumerator<ITask> GetEnumerator()
43
44
return _tasks.GetEnumerator();
45
58
/// </summary>
59
/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
60
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
61
public void Add(Task item)
public void Add(ITask item)
62
63
Task task = _tasks.Find(t => t.Name == item.Name);
ITask task = _tasks.Find(t => t.Name == item.Name);
64
if (task == null)
65
66
_tasks.Add(item);
81
throw new NotImplementedException();
82
83
84
public bool Contains(Task item)
public bool Contains(ITask item)
85
86
return _tasks.Contains(item);
87
88
89
public void CopyTo(Task[] array, int arrayIndex)
public void CopyTo(ITask[] array, int arrayIndex)
90
91
92
93
94
public bool Remove(Task item)
public bool Remove(ITask item)
95
96
97
106
get { return true; }
107
108
109
public Task Find(Predicate<Task> match)
public ITask Find(Predicate<ITask> match)
110
111
return _tasks.Find(match);
112
115
116
#region Implementation of IList<Task>
117
118
public int IndexOf(Task item)
public int IndexOf(ITask item)
119
120
return _tasks.IndexOf(item);
121
122
123
public void Insert(int index, Task item)
public void Insert(int index, ITask item)
124
125
126
130
131
132
133
public Task this[int index]
public ITask this[int index]
134
135
get { return _tasks[index]; }
136
set { throw new NotImplementedException(); }
142
/// Caches the update.
143
144
/// <param name="item">The item.</param>
145
private void CacheUpdate(Task item)
private void CacheUpdate(ITask item)
146
147
_cache.Remove(item.Name);
148
153
/// Caches the add.
154
155
156
private void CacheAdd(Task item)
private void CacheAdd(ITask item)
157
158
if (_cache[item.Name] == null)
159
public static void TaskExecuting(string key, object value, CacheItemRemovedReason reason)
var task = value as Task;
var task = value as ITask;
25
if (task != null && task.LastRan.GetValueOrDefault() < DateTime.Now)
26
[TestMethod]
public void PrintTaskTest_RunTest()
var target = new PrintTask { Active = true, Id = 1, Name = "UnitTestPrintTask" };
var target = new PrintTask {Active = true, Id = 1, Name = "UnitTestPrintTask"};
target.Run();
/// <summary>
///A test for Run
///</summary>
public void ServiceUpTaskTest_RunTest()
var target = new ServiceUpTask {Active = true, Id = 1, Name = "UnitTestServiceUpTask", ServiceName = "Telnet"};
1
#region Using Statements
2
3
using System;
4
5
#endregion
6
7
namespace Tasks
8
9
public interface ITask
10
/// Gets or sets the name.
/// <value>The name.</value>
string Name { get; set; }
/// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
/// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
bool Active { get; set; }
/// Gets or sets the last ran.
/// <value>The last ran.</value>
DateTime? LastRan { get; set; }
/// Gets or sets the id.
/// <value>The id.</value>
decimal? Id { get; set; }
35
36
/// Runs this instance.
37
void Run();
38
using System.Diagnostics;
public class PrintTask : Task
[DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]
public class PrintTask : ITask
public override void Run()
#region ITask Members
public void Run()
Debug.Print(string.Format("Printing line for Task: {0} last ran at {1}", Name, LastRan));
public string Name { get; set; }
public bool Active { get; set; }
public DateTime? LastRan { get; set; }
public decimal? Id { get; set; }
46
public class PrintTaskOther : Task
public class PrintTaskOther : ITask
Debug.Print(string.Format("Printing other line for Task: {0} last ran at {1}", Name, LastRan));
[assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
[assembly: PermissionSet(SecurityAction.RequestMinimum, Name = "FullTrust")]
public class ServiceUpTask : Task
public class ServiceUpTask : ITask
public string ServiceName { get; set; }
public string UserName { get; set; }
#region Overrides of Task
if (!string.IsNullOrEmpty(ServiceName))
impersonationContext = AssumeIdentity(UserName, DomainName, Password);
using (var service = ServiceBuilder(ServiceName, MachineName))
using (ServiceController service = ServiceBuilder(ServiceName, MachineName))
Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));
service.Refresh();
47
48
if (service.Status == ServiceControllerStatus.Stopped)
49
50
service.Start();
51
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/// Builds the service.
/// <param name="serviceName">Name of the service.</param>
/// <returns></returns>
private static ServiceController ServiceBuilder(string serviceName, string machineName)
var service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
ServiceController service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
return service;
98
if (phToken != IntPtr.Zero)
var windowsIdentity = new WindowsIdentity(phToken);
var impersonationContext = windowsIdentity.Impersonate();
WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
CloseHandle(phToken);
return impersonationContext;
<ItemGroup>
<Compile Include="PrintTaskOther.cs" />
<Compile Include="ServiceUpTask.cs" />
<Compile Include="Task.cs" />
52
<Compile Include="PrintTask.cs" />
53
<Compile Include="ITask.cs" />
54
<Compile Include="Properties\AssemblyInfo.cs" />
protected void Page_Load(object sender, EventArgs e)
Task printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);
ITask printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);
lblStatus.Text = printTask != null ? (printTask.Active ? "Active" : "InActive") : "Task not found";
{{public class TaskList : IList<ITask>{{private readonly List<Task> _tasks = new List<Task>();private readonly List<ITask> _tasks = new List<ITask>();public List<ITask> Tasks{{{{{{if (enumerator.Current is ITask)_tasks.Add(enumerator.Current as ITask);public IEnumerator<ITask> GetEnumerator(){{public void Add(ITask item){{ITask task = _tasks.Find(t => t.Name == item.Name);{{public bool Contains(ITask item){{public void CopyTo(ITask[] array, int arrayIndex){{public bool Remove(ITask item){{get { return true; }get { return true; }public Task Find(Predicate<Task> match)public ITask Find(Predicate<ITask> match){{public int IndexOf(ITask item){{public void Insert(int index, ITask item){{public ITask this[int index]{{get { return _tasks[index]; }get { return _tasks[index]; }set { throw new NotImplementedException(); }set { throw new NotImplementedException(); }private void CacheUpdate(ITask item){{private void CacheAdd(ITask item){{{{{{var task = value as ITask;{{{{var target = new PrintTask { Active = true, Id = 1, Name = "UnitTestPrintTask" };var target = new PrintTask {Active = true, Id = 1, Name = "UnitTestPrintTask"};[TestMethod]{{var target = new ServiceUpTask {Active = true, Id = 1, Name = "UnitTestServiceUpTask", ServiceName = "Telnet"};var target = new ServiceUpTask {Active = true, Id = 1, Name = "UnitTestServiceUpTask", ServiceName = "Telnet"};{{{{string Name { get; set; }bool Active { get; set; }DateTime? LastRan { get; set; }decimal? Id { get; set; }{{public class PrintTask : Task[DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]public class PrintTask : ITask{{public override void Run()#region ITask Memberspublic void Run(){{Debug.Print(string.Format("Printing line for Task: {0} last ran at {1}", Name, LastRan));Debug.Print(string.Format("Printing line for Task: {0} last ran at {1}", Name, LastRan));public string Name { get; set; }public bool Active { get; set; }public DateTime? LastRan { get; set; }public decimal? Id { get; set; }{{public class PrintTaskOther : Task[DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]public class PrintTaskOther : ITask{{public override void Run()#region ITask Memberspublic void Run(){{Debug.Print(string.Format("Printing other line for Task: {0} last ran at {1}", Name, LastRan));Debug.Print(string.Format("Printing other line for Task: {0} last ran at {1}", Name, LastRan));public string Name { get; set; }public bool Active { get; set; }public DateTime? LastRan { get; set; }public decimal? Id { get; set; }{{public class ServiceUpTask : Task[DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]public class ServiceUpTask : ITask{{public string ServiceName { get; set; }public string ServiceName { get; set; }public string UserName { get; set; }public string UserName { get; set; }public override void Run(){{{{{{using (var service = ServiceBuilder(ServiceName, MachineName))using (ServiceController service = ServiceBuilder(ServiceName, MachineName)){{Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));{{public string Name { get; set; }public bool Active { get; set; }public DateTime? LastRan { get; set; }public decimal? Id { get; set; }{{var service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);ServiceController service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);{{var impersonationContext = windowsIdentity.Impersonate();WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();<Compile Include="Task.cs" />{{ITask printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);