Skip to content

Commit eed89fe

Browse files
committed
fix(LocalDatabase): log errors for LocalDatabase
1 parent 2a4d404 commit eed89fe

File tree

2 files changed

+158
-91
lines changed

2 files changed

+158
-91
lines changed

src/PollinationSDK/Wrapper/JobInfo.cs

+45-30
Original file line numberDiff line numberDiff line change
@@ -248,42 +248,57 @@ public async Task<ScheduledJobInfo> RunJobAsync(Action<string> progressReporting
248248

249249
private async Task<ScheduledJobInfo> RunJobOnLocalAsync()
250250
{
251-
if (string.IsNullOrEmpty(this.LocalRunFolder) || !this.IsLocalJob)
252-
throw new ArgumentException($"Please call SetLocalJob() before running a job");
253-
254-
var workDir = this.LocalRunOutputFolder;
255-
var cpuNum = this.LocalCPUNumber;
256-
var isSilentMode = this.LocalSilentMode;
257-
var runner = new JobRunner(this);
258-
var runout = await Task.Run(() => runner.RunOnLocalMachine(workDir, cpuNum, isSilentMode)).ConfigureAwait(false);
259-
// check local job status
260-
var status = JobRunner.CheckLocalJobStatus(runout);
261-
this.LocalJobStatus = status.ToString();
262-
263-
var jobInfo = new ScheduledJobInfo(this, workDir);
264-
265-
//save jobinfo to folder
266-
var jobPath = Path.Combine(jobInfo.SavedLocalPath, "job.json");
267-
File.WriteAllText(jobPath, jobInfo.ToJson());
268-
269-
// add the record to local database
270-
LocalDatabase.Instance.Add(jobInfo);
271-
return jobInfo;
251+
try
252+
{
253+
if (string.IsNullOrEmpty(this.LocalRunFolder) || !this.IsLocalJob)
254+
throw new ArgumentException($"Please call SetLocalJob() before running a job");
255+
256+
var workDir = this.LocalRunOutputFolder;
257+
var cpuNum = this.LocalCPUNumber;
258+
var isSilentMode = this.LocalSilentMode;
259+
var runner = new JobRunner(this);
260+
var runout = await Task.Run(() => runner.RunOnLocalMachine(workDir, cpuNum, isSilentMode)).ConfigureAwait(false);
261+
// check local job status
262+
var status = JobRunner.CheckLocalJobStatus(runout);
263+
this.LocalJobStatus = status.ToString();
264+
265+
var jobInfo = new ScheduledJobInfo(this, workDir);
266+
267+
//save jobinfo to folder
268+
var jobPath = Path.Combine(jobInfo.SavedLocalPath, "job.json");
269+
File.WriteAllText(jobPath, jobInfo.ToJson());
270+
271+
// add the record to local database
272+
LocalDatabase.Instance.Add(jobInfo);
273+
return jobInfo;
274+
}
275+
catch (Exception e)
276+
{
277+
throw LogHelper.LogReturnError(e);
278+
}
279+
272280
}
273281

274282
private async Task<ScheduledJobInfo> RunJobOnCloudAsync(Action<string> progressReporting = default, System.Threading.CancellationToken token = default)
275283
{
276-
if (string.IsNullOrEmpty(this.ProjectSlug) || this.IsLocalJob)
277-
throw new ArgumentException($"Please call SetCloudJob() before running a job");
284+
try
285+
{
286+
if (string.IsNullOrEmpty(this.ProjectSlug) || this.IsLocalJob)
287+
throw new ArgumentException($"Please call SetCloudJob() before running a job");
278288

279-
var proj = Helper.GetWritableProject(this.ProjectSlug);
280-
var runner = new JobRunner(this);
281-
var cloudJob = await runner.RunOnCloudAsync(proj, progressReporting, token);
282-
var jobInfo = new ScheduledJobInfo(proj, cloudJob);
289+
var proj = Helper.GetWritableProject(this.ProjectSlug);
290+
var runner = new JobRunner(this);
291+
var cloudJob = await runner.RunOnCloudAsync(proj, progressReporting, token);
292+
var jobInfo = new ScheduledJobInfo(proj, cloudJob);
283293

284-
// add the record to local database
285-
//LocalDatabase.Instance.Add(jobInfo);
286-
return jobInfo;
294+
// add the record to local database
295+
//LocalDatabase.Instance.Add(jobInfo);
296+
return jobInfo;
297+
}
298+
catch (Exception e)
299+
{
300+
throw LogHelper.LogReturnError(e);
301+
}
287302
}
288303

289304
public async Task<Job> UploadJobAssetsAsync(Action<string> progressReporting = default, System.Threading.CancellationToken token = default)

src/PollinationSDK/Wrapper/LocalDatabase.cs

+113-61
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ public class LocalDatabase
1010
{
1111
public string DatabaseFile { get; set; } = "pollination.db";
1212
private SqliteConnection _connection;
13-
private SqliteConnection connection
14-
{
15-
get
16-
{
17-
_connection = _connection ?? CreateConnection();
18-
if (_connection.State == System.Data.ConnectionState.Closed)
19-
_connection.Open();
20-
return _connection;
21-
}
22-
}
13+
private SqliteConnection connection => GetConnection();
2314

2415
private static LocalDatabase _instance;
2516
public static LocalDatabase Instance
@@ -31,17 +22,41 @@ public static LocalDatabase Instance
3122
}
3223
}
3324

25+
SqliteConnection GetConnection()
26+
{
27+
try
28+
{
29+
_connection = _connection ?? CreateConnection();
30+
if (_connection.State == System.Data.ConnectionState.Closed)
31+
_connection.Open();
32+
return _connection;
33+
}
34+
catch (Exception e)
35+
{
36+
throw LogHelper.LogReturnError(e);
37+
}
38+
39+
}
3440

3541
SqliteConnection CreateConnection()
3642
{
37-
38-
SqliteConnection con;
39-
var file = GetDatabaseFile();
40-
var fileExist = File.Exists(file);
41-
con = new SqliteConnection($"Data Source={file}");
42-
con.Open();
43-
if (!fileExist) InitDatabase(con);
44-
return con;
43+
try
44+
{
45+
SqliteConnection con;
46+
var file = GetDatabaseFile();
47+
var fileExist = File.Exists(file);
48+
con = new SqliteConnection($"Data Source={file}");
49+
con.Open();
50+
if (!fileExist)
51+
InitDatabase(con);
52+
53+
return con;
54+
}
55+
catch (Exception e)
56+
{
57+
throw LogHelper.LogReturnError(e);
58+
}
59+
4560
}
4661

4762
public void DeleteDatabase()
@@ -51,15 +66,25 @@ public void DeleteDatabase()
5166
File.Delete(file);
5267

5368
}
69+
5470
public string GetDatabaseFile()
5571
{
56-
var file = DatabaseFile;
57-
//C:\Users\mingo\.pollination
58-
var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
59-
var dir = Path.Combine(userDir, ".pollination");
60-
Directory.CreateDirectory(dir);
61-
var filePath = Path.Combine(dir, file);
62-
return filePath;
72+
try
73+
{
74+
var file = DatabaseFile;
75+
//C:\Users\mingo\.pollination
76+
var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
77+
var dir = Path.Combine(userDir, ".pollination");
78+
Directory.CreateDirectory(dir);
79+
var filePath = Path.Combine(dir, file);
80+
LogHelper.LogInfo(filePath);
81+
return filePath;
82+
}
83+
catch (Exception e)
84+
{
85+
throw LogHelper.LogReturnError(e);
86+
}
87+
6388
}
6489

6590
static void InitDatabase(SqliteConnection con)
@@ -129,70 +154,97 @@ public bool Add(IEnumerable<ScheduledJobInfo> jobResults)
129154

130155
static bool Add(SqliteConnection connection, ScheduledJobInfo schJob)
131156
{
132-
using (var con = connection)
157+
try
133158
{
134-
var cmd = con.CreateCommand();
135-
cmd.CommandText =
136-
@"
159+
using (var con = connection)
160+
{
161+
var cmd = con.CreateCommand();
162+
cmd.CommandText =
163+
@"
137164
INSERT INTO JobTable (ProjSlug, JobID, DateTime, JobInfo)
138165
VALUES ($ProjSlug, $JobID, $DateTime, $JobInfo)
139166
";
140-
cmd.Parameters.AddWithValue("$ProjSlug", SqliteType.Text).Value = schJob.ProjectSlug.ToLower();
141-
cmd.Parameters.AddWithValue("$JobID", SqliteType.Text).Value = schJob.JobID;
142-
cmd.Parameters.AddWithValue("$DateTime", SqliteType.Text).Value = DateTime.Now;
143-
cmd.Parameters.AddWithValue("$JobInfo", SqliteType.Blob).Value = schJob.Serialize_Binary();
144-
var done = cmd.ExecuteNonQuery();
145-
con.Close();
146-
return done == 1;
167+
cmd.Parameters.AddWithValue("$ProjSlug", SqliteType.Text).Value = schJob.ProjectSlug.ToLower();
168+
cmd.Parameters.AddWithValue("$JobID", SqliteType.Text).Value = schJob.JobID;
169+
cmd.Parameters.AddWithValue("$DateTime", SqliteType.Text).Value = DateTime.Now;
170+
cmd.Parameters.AddWithValue("$JobInfo", SqliteType.Blob).Value = schJob.Serialize_Binary();
171+
LogHelper.LogInfo($"Adding {schJob.ProjectSlug}/{schJob.JobID}");
172+
var done = cmd.ExecuteNonQuery();
173+
con.Close();
174+
return done == 1;
175+
}
176+
177+
}
178+
catch (Exception e)
179+
{
180+
throw LogHelper.LogReturnError(e, $"Failed to add job to local database:\n{schJob}");
147181
}
148-
182+
149183
}
150184

151185
static List<ScheduledJobInfo> Get(SqliteConnection connection, string condition)
152186
{
153-
var res = new List<ScheduledJobInfo>();
154-
condition = string.IsNullOrEmpty(condition) ? "1=1" : condition;
155-
using (var con = connection)
187+
188+
try
156189
{
157-
var cmd = con.CreateCommand();
158-
cmd.CommandText =
159-
$@"
190+
var res = new List<ScheduledJobInfo>();
191+
condition = string.IsNullOrEmpty(condition) ? "1=1" : condition;
192+
using (var con = connection)
193+
{
194+
var cmd = con.CreateCommand();
195+
cmd.CommandText =
196+
$@"
160197
SELECT JobInfo
161198
FROM JobTable
162199
WHERE {condition}
163200
";
164-
using (var reader = cmd.ExecuteReader())
165-
{
166-
while (reader.Read())
201+
LogHelper.LogInfo($"Getting a job {condition}");
202+
using (var reader = cmd.ExecuteReader())
167203
{
168-
var package = (byte[])reader.GetValue(0);
169-
var re = ScheduledJobInfo.Deserialize_Binary(package);
170-
res.Add(re);
204+
while (reader.Read())
205+
{
206+
var package = (byte[])reader.GetValue(0);
207+
var re = ScheduledJobInfo.Deserialize_Binary(package);
208+
res.Add(re);
209+
}
171210
}
211+
con.Close();
172212
}
173-
con.Close();
213+
return res;
174214
}
175-
return res;
215+
catch (Exception e)
216+
{
217+
throw LogHelper.LogReturnError(e, $"Failed to get a job from local database:\n{condition}");
218+
}
219+
176220
}
177221

178222
static bool Delete(SqliteConnection connection, string projID, string jobId)
179223
{
180-
using (var con = connection)
224+
try
181225
{
182-
var cmd = con.CreateCommand();
183-
cmd.CommandText =
184-
@"
226+
using (var con = connection)
227+
{
228+
var cmd = con.CreateCommand();
229+
cmd.CommandText =
230+
@"
185231
DELETE
186232
FROM JobTable
187233
WHERE ProjSlug = $ProjSlug AND JobID = $JobID
188234
";
189-
cmd.Parameters.AddWithValue("$ProjSlug", SqliteType.Blob).Value = projID;
190-
cmd.Parameters.AddWithValue("$JobID", SqliteType.Text).Value = jobId;
191-
192-
var done = cmd.ExecuteNonQuery();
193-
con.Close();
194-
return done >= 1;
235+
cmd.Parameters.AddWithValue("$ProjSlug", SqliteType.Blob).Value = projID;
236+
cmd.Parameters.AddWithValue("$JobID", SqliteType.Text).Value = jobId;
237+
LogHelper.LogInfo($"Deleting a job:\nProjSlug={projID} AND JobID={jobId}");
238+
var done = cmd.ExecuteNonQuery();
239+
con.Close();
240+
return done >= 1;
241+
}
195242
}
243+
catch (Exception e)
244+
{
245+
throw LogHelper.LogReturnError(e, $"Failed to delete a job from local database:\nProjSlug={projID} AND JobID={jobId}");
246+
}
247+
196248
}
197249
}
198250
}

0 commit comments

Comments
 (0)