-
Notifications
You must be signed in to change notification settings - Fork 119
/
ChoTaskQManager.cs
143 lines (127 loc) · 4.82 KB
/
ChoTaskQManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Cinchoo.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ChoEazyCopy
{
internal class ChoTaskQManager
{
private readonly ICollection<ChoTaskQueueItem> _taskQItems;
private readonly object _padLock;
private Thread _roboCopyThread;
public ChoTaskQManager(ICollection<ChoTaskQueueItem> taskQItems, object padLock)
{
_taskQItems = taskQItems;
_padLock = padLock;
}
public void Start()
{
#if TEST_MODE
return;
#endif
if (_roboCopyThread != null)
return;
ChoTaskQueueItem firstTaskQueueItem = null;
_roboCopyThread = new Thread(() =>
{
int count = 0;
while (true)
{
lock (_padLock)
{
count = _taskQItems.Count;
if (count > 0)
{
firstTaskQueueItem = GetFirstTaskQueueItem();
if (firstTaskQueueItem != null)
{
RunRoboCopyOperation(firstTaskQueueItem);
}
}
}
if (firstTaskQueueItem == null)
Thread.Sleep(10 * 1000);
}
});
_roboCopyThread.Start();
}
private void RunRoboCopyOperation(ChoTaskQueueItem taskQueueItem)
{
try
{
taskQueueItem.StartTime = DateTime.Now;
taskQueueItem.ErrorMessage = null;
if (taskQueueItem.TaskFilePath.IsNullOrWhiteSpace())
throw new ApplicationException("Missing task file path.");
if (taskQueueItem.TaskFilePath.IsNullOrWhiteSpace())
throw new ApplicationException($"'{taskQueueItem.TaskFilePath}' task file path does not exists.");
ChoAppSettings appSettings = new ChoAppSettings();
appSettings.LoadXml(File.ReadAllText(taskQueueItem.TaskFilePath));
using (var log = new StreamWriter(taskQueueItem.LogFilePath))
{
ChoRoboCopyManager _roboCopyManager = new ChoRoboCopyManager();
_roboCopyManager.Status += (sender, e) => log.Write(e.Message);
//_roboCopyManager.AppStatus += (sender, e) => UpdateStatus(e.Message, e.Tag.ToNString());
_roboCopyManager.Process(appSettings);
}
taskQueueItem.Status = TaskStatus.Completed;
}
catch (ThreadAbortException)
{
taskQueueItem.Status = TaskStatus.Stopped;
Thread.ResetAbort();
}
catch (Exception ex)
{
taskQueueItem.Status = TaskStatus.Stopped;
taskQueueItem.ErrorMessage = ex.Message;
}
finally
{
taskQueueItem.EndTime = DateTime.Now;
}
}
private ChoTaskQueueItem GetFirstTaskQueueItem()
{
return _taskQItems.FirstOrDefault(t => t.Status == TaskStatus.Queued);
}
public void Stop()
{
var thread = _roboCopyThread;
if (thread != null)
thread.AbortThread();
}
public void Add(string taskName, DateTime? startTime = null, DateTime? endTime = null, string taskFilePath = null,
TaskStatus? status = TaskStatus.Queued, string errorMsg = null, Action<ChoTaskQueueItem> onSuccess = null,
Action<string, string> onFailure = null)
{
lock (_padLock)
{
try
{
long index = _taskQItems.Count == 0 ? 0 : _taskQItems.Max(i => i.Id);
var task = new ChoTaskQueueItem(index + 1, taskName)
{
StartTime = startTime == null ? DateTime.Now : startTime.Value,
EndTime = endTime == null ? DateTime.Now.AddDays(1) : endTime.Value,
TaskFilePath = taskFilePath,
Status = status == null ? TaskStatus.Queued : status.Value,
ErrorMessage = errorMsg,
};
_taskQItems.Add(task);
if (onSuccess != null)
onSuccess(task);
}
catch (Exception ex)
{
if (onFailure != null)
onFailure(taskName, ex.Message);
}
}
}
}
}