-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathProgressReportManager.cs
More file actions
294 lines (255 loc) · 10.6 KB
/
Copy pathProgressReportManager.cs
File metadata and controls
294 lines (255 loc) · 10.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.EnterpriseServices.Internal;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using MySqlConnector;
namespace System
{
public static class ProgressReportManager
{
public static int CreateNewTask(ProgressReportInfo pr)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic["operation"] = pr.operation;
dic["start_time"] = pr.start_time;
dic["end_time"] = pr.end_time;
dic["is_completed"] = pr.is_completed ? 1 : 0;
dic["has_error"] = pr.has_error ? 1 : 0;
dic["is_cancelled"] = pr.is_cancelled ? 1 : 0;
dic["filename"] = pr.filename ?? "";
dic["total_tables"] = pr.total_tables;
dic["total_rows"] = pr.total_rows;
dic["total_rows_current_table"] = pr.total_rows_current_table;
dic["current_table"] = pr.current_table ?? "";
dic["current_table_index"] = pr.current_table_index;
dic["current_row"] = pr.current_row;
dic["current_row_in_current_table"] = pr.current_row_in_current_table;
dic["total_bytes"] = pr.total_bytes;
dic["current_bytes"] = pr.current_bytes;
dic["percent_complete"] = pr.percent_complete;
dic["remarks"] = pr.remarks;
dic["last_update_time"] = DateTime.Now;
using (var connection = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(connection))
{
SQLiteHelper h = new SQLiteHelper(cmd);
h.Insert("progress_report", dic);
return Convert.ToInt32(connection.LastInsertRowId);
}
}
}
public static void CancelTask(int id)
{
using (SQLiteConnection conn = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
using (SQLiteCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"update progress_report set client_request_cancel_task=1 where id={id};";
cmd.ExecuteNonQuery();
}
}
}
public static bool TaskIsBeingRequestedToCancel(int id)
{
using (SQLiteConnection conn = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = $"select client_request_cancel_task from progress_report where id={id};";
return cmd.ExecuteScalar() + "" == "1";
}
}
}
public static void DeleteTask(int id)
{
using (var conn = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = $"delete from progress_report where id={id};";
cmd.ExecuteNonQuery();
}
}
}
public static ProgressReportInfo GetProgressReport(int id)
{
var lst = GetProgressReportList(id, false, false);
if (lst.Count == 0)
return null;
return lst[0];
}
public static List<ProgressReportInfo> GetProgressReportList(int id, bool getAll, bool inProgress)
{
StringBuilder sb = new StringBuilder();
sb.Append("select * from progress_report");
if (id != 0)
{
sb.Append($" where id={id}");
}
else
{
if (!getAll)
{
sb.Append(" where is_completed=");
if (inProgress)
{
sb.Append("0");
}
else
{
sb.Append("1");
}
}
sb.Append(" order by start_time desc, id desc;");
}
string sql = sb.ToString();
List<ProgressReportInfo> lst = new List<ProgressReportInfo>();
using (var connection = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(connection))
{
SQLiteHelper h = new SQLiteHelper(cmd);
lst = h.GetObjectList<ProgressReportInfo>(sql);
foreach (var pr in lst)
{
if ((DateTime.Now - pr.last_update_time).TotalSeconds > 10 && !pr.is_completed)
{
// some error has occur, the update is scheduled to be executed every 500ms
// if after 10000ms (10 seconds), there isn't any update and the task is not completed
// this indicates that the process (ASP.NET) has been terminated by IIS App Pool
// the task will never complete
h.Execute($"update progress_report set is_completed=1, is_cancelled=1, has_error=1, remarks='Terminated by system. Unknown error.' where id={pr.id};");
pr.is_completed = true;
pr.has_error = true;
pr.remarks = "Terminated by system. Unknown error.";
}
}
}
}
return lst;
}
public static void UpdateProgress(ProgressReportInfo pr)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
if (pr.has_error)
{
dic["has_error"] = 1;
dic["is_completed"] = 1;
dic["is_cancelled"] = 1;
dic["remarks"] = pr.remarks;
}
else
{
dic["total_tables"] = pr.total_tables;
dic["total_rows"] = pr.total_rows;
dic["total_rows_current_table"] = pr.total_rows_current_table;
dic["current_table"] = pr.current_table ?? "";
dic["current_table_index"] = pr.current_table_index;
dic["current_row"] = pr.current_row;
dic["current_row_in_current_table"] = pr.current_row_in_current_table;
dic["percent_complete"] = pr.percent_complete;
dic["last_update_time"] = DateTime.Now;
dic["total_bytes"] = pr.total_bytes;
dic["current_bytes"] = pr.current_bytes;
}
using (var connection = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(connection))
{
SQLiteHelper h = new SQLiteHelper(cmd);
h.Update("progress_report", dic, "id", pr.id);
}
}
}
public static void CompleteProgress(ProgressReportInfo pr)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic["end_time"] = pr.end_time;
dic["is_completed"] = 1;
dic["has_error"] = pr.has_error ? 1 : 0;
dic["is_cancelled"] = pr.is_cancelled ? 1 : 0;
dic["last_update_time"] = DateTime.Now;
dic["has_file"] = true;
if (pr.has_error)
dic["remarks"] = pr.remarks;
string filename = "";
using (var connection = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(connection))
{
SQLiteHelper h = new SQLiteHelper(cmd);
// First, get the filename BEFORE updating
filename = h.ExecuteScalar<string>($"select filename from progress_report where id={pr.id}");
h.Update("progress_report", dic, "id", pr.id);
cmd.ExecuteNonQuery();
}
}
string filePath = Path.Combine(BackupFilesManager.folder, filename);
// the process is cancelled
if (pr.is_cancelled)
{
// delete the incomplete file
if (File.Exists(filePath))
{
File.Delete(filePath);
return;
}
}
// File doesn't exist, possibly because backup was cancelled before file was created
if (!File.Exists(filePath))
{
return;
}
var fileInfo = new FileInfo(filePath);
DatabaseFileRecord dbFile = new DatabaseFileRecord();
dbFile.Operation = "Progress Backup";
dbFile.Filename = filename;
dbFile.OriginalFilename = filename;
dbFile.LogFilename = "";
dbFile.Filesize = fileInfo.Length;
dbFile.DatabaseName = config.GetCurrentDatabaseName();
dbFile.DateCreated = fileInfo.CreationTime;
dbFile.Remarks = "";
dbFile.Sha256 = BackupFilesManager.ComputeSha256File(filePath);
dbFile.TaskId = pr.id;
int dbfileid = BackupFilesManager.SaveRecord(dbFile);
using (var conn = new SQLiteConnection(BackupFilesManager.sqliteConnectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = $"update progress_report set dbfile_id={dbfileid} where id={pr.id};";
cmd.ExecuteNonQuery();
}
}
// Pre-generate the zip file in background if backup was successful
if (!pr.has_error && !pr.is_cancelled && File.Exists(filePath))
{
Task.Run(() =>
{
try
{
BackupFilesManager.PreGenerateZipFile(filePath);
}
catch
{
// Ignore errors - pre-generation is optional
}
});
}
}
}
}