Skip to content

Commit ffe8651

Browse files
authored
Merge pull request #21 from cjmanca/in-progress
Handle invalid library section availability #20
2 parents 0f5bec9 + a9f7851 commit ffe8651

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

plex-credits-detect/Database/InMemoryFingerprintDatabase.cs

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ internal class InMemoryFingerprintDatabase : IFingerprintDatabase
1414
InMemoryModelService modelService = null;
1515
SQLiteConnection sqlite_conn = null;
1616

17+
string databasePath = "";
18+
1719

1820
public DateTime lastPlexIntroAdded
1921
{
@@ -66,6 +68,8 @@ public void LoadDatabase(string path)
6668

6769
SetupNewScan();
6870

71+
databasePath = path;
72+
6973
SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
7074
sb.DataSource = Program.PathCombine(path, "fingerprintMedia.db");
7175
sb.Version = 3;
@@ -239,14 +243,25 @@ public int ExecuteDBCommand(string cmd, Dictionary<string, object> p = null)
239243
}
240244
}
241245

246+
int count = 0;
247+
242248
while (true)
243249
{
244250
try
245251
{
252+
count++;
246253
return sqlite_cmd.ExecuteNonQuery();
247254
}
248255
catch (SQLiteException ex)
249256
{
257+
if (count >= 2)
258+
{
259+
Console.WriteLine($"PlexDB ExecuteDBCommand Database has been locked for a long time. Attempting to re-connect.");
260+
CloseDatabase();
261+
LoadDatabase(databasePath);
262+
count = 0;
263+
}
264+
250265
if ((SQLiteErrorCode)ex.ErrorCode != SQLiteErrorCode.Busy)
251266
{
252267
Console.WriteLine("InMemoryFingerprintDatabase.ExecuteDBCommand exception: " + ex.Message + "" +
@@ -260,6 +275,7 @@ public int ExecuteDBCommand(string cmd, Dictionary<string, object> p = null)
260275
Console.WriteLine($"{x.Key} = {x.Value}");
261276
}
262277
}
278+
Program.Exit();
263279
return -1;
264280
}
265281
Thread.Sleep(10);
@@ -283,10 +299,13 @@ public SQLResultInfo ExecuteDBQuery(string cmd, Dictionary<string, object> p = n
283299
}
284300
}
285301

302+
int count = 0;
303+
286304
while (true)
287305
{
288306
try
289307
{
308+
count++;
290309
//var reader = sqlite_cmd.ExecuteReader(System.Data.CommandBehavior.KeyInfo);
291310
ret.reader = sqlite_cmd.ExecuteReader();
292311

@@ -299,6 +318,14 @@ public SQLResultInfo ExecuteDBQuery(string cmd, Dictionary<string, object> p = n
299318
}
300319
catch (SQLiteException ex)
301320
{
321+
if (count >= 2)
322+
{
323+
Console.WriteLine($"PlexDB ExecuteDBCommand Database has been locked for a long time. Attempting to re-connect.");
324+
CloseDatabase();
325+
LoadDatabase(databasePath);
326+
count = 0;
327+
}
328+
302329
if ((SQLiteErrorCode)ex.ErrorCode != SQLiteErrorCode.Busy)
303330
{
304331
Console.WriteLine("InMemoryFingerprintDatabase.ExecuteDBCommand exception: " + ex.Message + "" +
@@ -312,6 +339,7 @@ public SQLResultInfo ExecuteDBQuery(string cmd, Dictionary<string, object> p = n
312339
Console.WriteLine($"{x.Key} = {x.Value}");
313340
}
314341
}
342+
Program.Exit();
315343
return null;
316344
}
317345
Thread.Sleep(10);

plex-credits-detect/Database/PlexDB.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public SQLResultInfo ExecuteDBQuery(string cmd, Dictionary<string, object> p = n
159159

160160
int count = 0;
161161

162-
163162
while (true)
164163
{
165164
try
@@ -188,6 +187,8 @@ public SQLResultInfo ExecuteDBQuery(string cmd, Dictionary<string, object> p = n
188187
if ((SQLiteErrorCode)e.ErrorCode != SQLiteErrorCode.Busy)
189188
{
190189
Console.WriteLine($"PlexDB ExecuteDBQuery SQLite error code {e.ErrorCode}: {e.Message}");
190+
Program.Exit();
191+
return null;
191192
}
192193
Thread.Sleep(10);
193194
}
@@ -627,12 +628,19 @@ private Dictionary<long, RootDirectory> GetRootDirectories()
627628

628629
while (result.Read())
629630
{
630-
if (result.Get<int>("available") == 1)
631+
try
632+
{
633+
if (result.Get<bool>("available"))
634+
{
635+
var root = new RootDirectory();
636+
root.path = result.Get<string>("root_path");
637+
root.library_section_id = result.Get<long>("library_section_id");
638+
ret[root.library_section_id] = root;
639+
}
640+
}
641+
catch (InvalidCastException ce)
631642
{
632-
var root = new RootDirectory();
633-
root.path = result.Get<string>("root_path");
634-
root.library_section_id = result.Get<long>("library_section_id");
635-
ret[root.library_section_id] = root;
643+
Console.WriteLine($"A library section has invalid information: {result.Get<string>("root_path")}");
636644
}
637645
}
638646

plex-credits-detect/Scanner.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,11 @@ public void CleanTemp()
11761176
var files = Directory.EnumerateFiles(Program.PathCombine(Settings.TempDirectoryPath, "plex-credits-detect-temp"));
11771177
foreach (var file in files)
11781178
{
1179-
File.Delete(file);
1179+
try
1180+
{
1181+
File.Delete(file);
1182+
}
1183+
catch { }
11801184
}
11811185
}
11821186

0 commit comments

Comments
 (0)