Changeset 7

User picture

Author: marisic.net

(2008/09/25 18:33) Over 3 years ago


  

Affected files

Updated TaskScheduler/TaskScheduler/CacheManager/TaskList.cs Download diff

67
11
11
12
namespace CacheManager
12
namespace CacheManager
13
{
13
{
14
    public class TaskList : IList<Task>
14
    public class TaskList : IList<ITask>
15
    {
15
    {
16
        //How long for jobs to wait before waking.
16
        //How long for jobs to wait before waking.
17
        private const double sleepLength = 1;
17
        private const double sleepLength = 1;
18
        private readonly Cache _cache = HttpRuntime.Cache;
18
        private readonly Cache _cache = HttpRuntime.Cache;
19
        private readonly List<Task> _tasks = new List<Task>();
19
        private readonly List<ITask> _tasks = new List<ITask>();
20
20
21
        public List<Task> Tasks
21
        public List<ITask> Tasks
22
        {
22
        {
23
            get
23
            get
24
            {
24
            {
...
...
27
                    IDictionaryEnumerator enumerator = _cache.GetEnumerator();
27
                    IDictionaryEnumerator enumerator = _cache.GetEnumerator();
28
                    do
28
                    do
29
                    {
29
                    {
30
                        if (enumerator.Current is Task)
30
                        if (enumerator.Current is ITask)
31
                            _tasks.Add(enumerator.Current as Task);
31
                            _tasks.Add(enumerator.Current as ITask);
32
                    } while (enumerator.MoveNext());
32
                    } while (enumerator.MoveNext());
33
                }
33
                }
34
                return _tasks;
34
                return _tasks;
...
...
39
39
40
        #region Implementation of IEnumerable
40
        #region Implementation of IEnumerable
41
41
42
        public IEnumerator<Task> GetEnumerator()
42
        public IEnumerator<ITask> GetEnumerator()
43
        {
43
        {
44
            return _tasks.GetEnumerator();
44
            return _tasks.GetEnumerator();
45
        }
45
        }
...
...
58
        /// </summary>
58
        /// </summary>
59
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
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>
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)
61
        public void Add(ITask item)
62
        {
62
        {
63
            Task task = _tasks.Find(t => t.Name == item.Name);
63
            ITask task = _tasks.Find(t => t.Name == item.Name);
64
            if (task == null)
64
            if (task == null)
65
            {
65
            {
66
                _tasks.Add(item);
66
                _tasks.Add(item);
...
...
81
            throw new NotImplementedException();
81
            throw new NotImplementedException();
82
        }
82
        }
83
83
84
        public bool Contains(Task item)
84
        public bool Contains(ITask item)
85
        {
85
        {
86
            return _tasks.Contains(item);
86
            return _tasks.Contains(item);
87
        }
87
        }
88
88
89
        public void CopyTo(Task[] array, int arrayIndex)
89
        public void CopyTo(ITask[] array, int arrayIndex)
90
        {
90
        {
91
            throw new NotImplementedException();
91
            throw new NotImplementedException();
92
        }
92
        }
93
93
94
        public bool Remove(Task item)
94
        public bool Remove(ITask item)
95
        {
95
        {
96
            throw new NotImplementedException();
96
            throw new NotImplementedException();
97
        }
97
        }
...
...
106
            get { return true; }
106
            get { return true; }
107
        }
107
        }
108
108
109
        public Task Find(Predicate<Task> match)
109
        public ITask Find(Predicate<ITask> match)
110
        {
110
        {
111
            return _tasks.Find(match);
111
            return _tasks.Find(match);
112
        }
112
        }
...
...
115
115
116
        #region Implementation of IList<Task>
116
        #region Implementation of IList<Task>
117
117
118
        public int IndexOf(Task item)
118
        public int IndexOf(ITask item)
119
        {
119
        {
120
            return _tasks.IndexOf(item);
120
            return _tasks.IndexOf(item);
121
        }
121
        }
122
122
123
        public void Insert(int index, Task item)
123
        public void Insert(int index, ITask item)
124
        {
124
        {
125
            throw new NotImplementedException();
125
            throw new NotImplementedException();
126
        }
126
        }
...
...
130
            throw new NotImplementedException();
130
            throw new NotImplementedException();
131
        }
131
        }
132
132
133
        public Task this[int index]
133
        public ITask this[int index]
134
        {
134
        {
135
            get { return _tasks[index]; }
135
            get { return _tasks[index]; }
136
            set { throw new NotImplementedException(); }
136
            set { throw new NotImplementedException(); }
...
...
142
        /// Caches the update.
142
        /// Caches the update.
143
        /// </summary>
143
        /// </summary>
144
        /// <param name="item">The item.</param>
144
        /// <param name="item">The item.</param>
145
        private void CacheUpdate(Task item)
145
        private void CacheUpdate(ITask item)
146
        {
146
        {
147
            _cache.Remove(item.Name);
147
            _cache.Remove(item.Name);
148
148
...
...
153
        /// Caches the add.
153
        /// Caches the add.
154
        /// </summary>
154
        /// </summary>
155
        /// <param name="item">The item.</param>
155
        /// <param name="item">The item.</param>
156
        private void CacheAdd(Task item)
156
        private void CacheAdd(ITask item)
157
        {
157
        {
158
            if (_cache[item.Name] == null)
158
            if (_cache[item.Name] == null)
159
            {
159
            {

Updated TaskScheduler/TaskScheduler/TaskManager/TaskManager.cs Download diff

67
20
20
21
        public static void TaskExecuting(string key, object value, CacheItemRemovedReason reason)
21
        public static void TaskExecuting(string key, object value, CacheItemRemovedReason reason)
22
        {
22
        {
23
            var task = value as Task;
23
            var task = value as ITask;
24
24
25
            if (task != null && task.LastRan.GetValueOrDefault() < DateTime.Now)
25
            if (task != null && task.LastRan.GetValueOrDefault() < DateTime.Now)
26
            {
26
            {

Updated TaskScheduler/TaskScheduler/Tasks.TestProject/PrintTaskTest.cs Download diff

67
21
        [TestMethod]
21
        [TestMethod]
22
        public void PrintTaskTest_RunTest()
22
        public void PrintTaskTest_RunTest()
23
        {
23
        {
24
            var target = new PrintTask { Active = true, Id = 1, Name = "UnitTestPrintTask" };
24
            var target = new PrintTask {Active = true, Id = 1, Name = "UnitTestPrintTask"};
25
            target.Run();
25
            target.Run();
26
        }
26
        }
27
    }
27
    }

Updated TaskScheduler/TaskScheduler/Tasks.TestProject/ServiceUpTaskTest.cs Download diff

67
22
        /// <summary>
22
        /// <summary>
23
        ///A test for Run
23
        ///A test for Run
24
        ///</summary>
24
        ///</summary>
25
        [TestMethod]        
25
        [TestMethod]
26
        public void ServiceUpTaskTest_RunTest()
26
        public void ServiceUpTaskTest_RunTest()
27
        {
27
        {
28
            var target = new ServiceUpTask {Active = true, Id = 1, Name = "UnitTestServiceUpTask", ServiceName = "Telnet"};
28
            var target = new ServiceUpTask {Active = true, Id = 1, Name = "UnitTestServiceUpTask", ServiceName = "Telnet"};

Updated TaskScheduler/TaskScheduler/Tasks/ITask.cs Download diff

67
1
#region Using Statements
1
#region Using Statements
2
2
3
using System;
3
4
4
5
#endregion
5
#endregion
6
6
7
namespace Tasks
7
namespace Tasks
8
{
8
{
9
    public interface ITask
9
    public interface ITask
10
    {
10
    {
11
        /// <summary>
12
        /// Gets or sets the name.
13
        /// </summary>
14
        /// <value>The name.</value>
15
        string Name { get; set; }
16
17
        /// <summary>
18
        /// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
19
        /// </summary>
20
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
21
        bool Active { get; set; }
22
23
        /// <summary>
24
        /// Gets or sets the last ran.
25
        /// </summary>
26
        /// <value>The last ran.</value>
27
        DateTime? LastRan { get; set; }
28
29
        /// <summary>
30
        /// Gets or sets the id.
31
        /// </summary>
32
        /// <value>The id.</value>
33
        decimal? Id { get; set; }
34
35
        /// <summary>
36
        /// Runs this instance.
37
        /// </summary>
11
        void Run();
38
        void Run();
12
    }
39
    }
13
}
40
}

Updated TaskScheduler/TaskScheduler/Tasks/PrintTask.cs Download diff

67
1
#region Using Statements
1
#region Using Statements
2
2
3
using System;
3
using System.Diagnostics;
4
using System.Diagnostics;
4
5
5
#endregion
6
#endregion
6
7
7
namespace Tasks
8
namespace Tasks
8
{
9
{
9
    public class PrintTask : Task
10
    [DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]
11
    public class PrintTask : ITask
10
    {
12
    {
11
        public override void Run()
13
        #region ITask Members
14
15
        public void Run()
12
        {
16
        {
13
            Debug.Print(string.Format("Printing line for Task: {0} last ran at {1}", Name, LastRan));
17
            Debug.Print(string.Format("Printing line for Task: {0} last ran at {1}", Name, LastRan));
14
        }
18
        }
19
20
        /// <summary>
21
        /// Gets or sets the name.
22
        /// </summary>
23
        /// <value>The name.</value>
24
        public string Name { get; set; }
25
26
        /// <summary>
27
        /// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
28
        /// </summary>
29
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
30
        public bool Active { get; set; }
31
32
        /// <summary>
33
        /// Gets or sets the last ran.
34
        /// </summary>
35
        /// <value>The last ran.</value>
36
        public DateTime? LastRan { get; set; }
37
38
        /// <summary>
39
        /// Gets or sets the id.
40
        /// </summary>
41
        /// <value>The id.</value>
42
        public decimal? Id { get; set; }
43
44
        #endregion
15
    }
45
    }
16
}
46
}

Updated TaskScheduler/TaskScheduler/Tasks/PrintTaskOther.cs Download diff

67
1
#region Using Statements
1
#region Using Statements
2
2
3
using System;
3
using System.Diagnostics;
4
using System.Diagnostics;
4
5
5
#endregion
6
#endregion
6
7
7
namespace Tasks
8
namespace Tasks
8
{
9
{
9
    public class PrintTaskOther : Task
10
    [DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]
11
    public class PrintTaskOther : ITask
10
    {
12
    {
11
        public override void Run()
13
        #region ITask Members
14
15
        public void Run()
12
        {
16
        {
13
            Debug.Print(string.Format("Printing other line for Task: {0} last ran at {1}", Name, LastRan));
17
            Debug.Print(string.Format("Printing other line for Task: {0} last ran at {1}", Name, LastRan));
14
        }
18
        }
19
20
        /// <summary>
21
        /// Gets or sets the name.
22
        /// </summary>
23
        /// <value>The name.</value>
24
        public string Name { get; set; }
25
26
        /// <summary>
27
        /// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
28
        /// </summary>
29
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
30
        public bool Active { get; set; }
31
32
        /// <summary>
33
        /// Gets or sets the last ran.
34
        /// </summary>
35
        /// <value>The last ran.</value>
36
        public DateTime? LastRan { get; set; }
37
38
        /// <summary>
39
        /// Gets or sets the id.
40
        /// </summary>
41
        /// <value>The id.</value>
42
        public decimal? Id { get; set; }
43
44
        #endregion
15
    }
45
    }
16
}
46
}

Updated TaskScheduler/TaskScheduler/Tasks/ServiceUpTask.cs Download diff

67
13
{
13
{
14
    [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
14
    [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]
15
    [assembly: PermissionSet(SecurityAction.RequestMinimum, Name = "FullTrust")]
15
    [assembly: PermissionSet(SecurityAction.RequestMinimum, Name = "FullTrust")]
16
    public class ServiceUpTask : Task
16
    [DebuggerDisplay("Name = {Name}, Id = {Id}, LastRan = {LastRan}")]
17
    public class ServiceUpTask : ITask
17
    {
18
    {
18
        public string ServiceName { get; set; }
19
        public string ServiceName { get; set; }
19
        public string UserName { get; set; }
20
        public string UserName { get; set; }
...
...
23
24
24
        #region Overrides of Task
25
        #region Overrides of Task
25
26
26
        public override void Run()
27
        public void Run()
27
        {
28
        {
28
            if (!string.IsNullOrEmpty(ServiceName))
29
            if (!string.IsNullOrEmpty(ServiceName))
29
            {
30
            {
...
...
33
                {
34
                {
34
                    impersonationContext = AssumeIdentity(UserName, DomainName, Password);
35
                    impersonationContext = AssumeIdentity(UserName, DomainName, Password);
35
36
36
                    using (var service = ServiceBuilder(ServiceName, MachineName))
37
                    using (ServiceController service = ServiceBuilder(ServiceName, MachineName))
37
                    {
38
                    {
38
                        Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));
39
                        Debug.Print(string.Format("Service {0}, Status {1}", service.ServiceName, service.Status));
39
40
...
...
44
                        }
45
                        }
45
46
46
                        service.Refresh();
47
                        service.Refresh();
47
                
48
48
                        if (service.Status == ServiceControllerStatus.Stopped)
49
                        if (service.Status == ServiceControllerStatus.Stopped)
49
                        {
50
                        {
50
                            service.Start();
51
                            service.Start();
...
...
60
        }
61
        }
61
62
62
        /// <summary>
63
        /// <summary>
64
        /// Gets or sets the name.
65
        /// </summary>
66
        /// <value>The name.</value>
67
        public string Name { get; set; }
68
69
        /// <summary>
70
        /// Gets or sets a value indicating whether this <see cref="ITask"/> is active.
71
        /// </summary>
72
        /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
73
        public bool Active { get; set; }
74
75
        /// <summary>
76
        /// Gets or sets the last ran.
77
        /// </summary>
78
        /// <value>The last ran.</value>
79
        public DateTime? LastRan { get; set; }
80
81
        /// <summary>
82
        /// Gets or sets the id.
83
        /// </summary>
84
        /// <value>The id.</value>
85
        public decimal? Id { get; set; }
86
87
        /// <summary>
63
        /// Builds the service.
88
        /// Builds the service.
64
        /// </summary>
89
        /// </summary>
65
        /// <param name="serviceName">Name of the service.</param>
90
        /// <param name="serviceName">Name of the service.</param>
...
...
67
        /// <returns></returns>
92
        /// <returns></returns>
68
        private static ServiceController ServiceBuilder(string serviceName, string machineName)
93
        private static ServiceController ServiceBuilder(string serviceName, string machineName)
69
        {
94
        {
70
            var service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
95
            ServiceController service = string.IsNullOrEmpty(machineName) ? new ServiceController(serviceName) : new ServiceController(serviceName, machineName);
71
            return service;
96
            return service;
72
        }
97
        }
73
98
...
...
91
                if (phToken != IntPtr.Zero)
116
                if (phToken != IntPtr.Zero)
92
                {
117
                {
93
                    var windowsIdentity = new WindowsIdentity(phToken);
118
                    var windowsIdentity = new WindowsIdentity(phToken);
94
                    var impersonationContext = windowsIdentity.Impersonate();
119
                    WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
95
                    CloseHandle(phToken);
120
                    CloseHandle(phToken);
96
                    return impersonationContext;
121
                    return impersonationContext;
97
                }
122
                }

Updated TaskScheduler/TaskScheduler/Tasks/Tasks.csproj Download diff

67
48
  <ItemGroup>
48
  <ItemGroup>
49
    <Compile Include="PrintTaskOther.cs" />
49
    <Compile Include="PrintTaskOther.cs" />
50
    <Compile Include="ServiceUpTask.cs" />
50
    <Compile Include="ServiceUpTask.cs" />
51
    <Compile Include="Task.cs" />
52
    <Compile Include="PrintTask.cs" />
51
    <Compile Include="PrintTask.cs" />
53
    <Compile Include="ITask.cs" />
52
    <Compile Include="ITask.cs" />
54
    <Compile Include="Properties\AssemblyInfo.cs" />
53
    <Compile Include="Properties\AssemblyInfo.cs" />

Updated TaskScheduler/TaskScheduler/TaskScheduler/Default.aspx.cs Download diff

67
15
15
16
        protected void Page_Load(object sender, EventArgs e)
16
        protected void Page_Load(object sender, EventArgs e)
17
        {
17
        {
18
            Task printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);
18
            ITask printTask = taskManager.TaskList.Find(t => t.Id.GetValueOrDefault() == 1);
19
19
20
            lblStatus.Text = printTask != null ? (printTask.Active ? "Active" : "InActive") : "Task not found";
20
            lblStatus.Text = printTask != null ? (printTask.Active ? "Active" : "InActive") : "Task not found";
21
        }
21
        }