mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 11:58:19 +02:00
Allow requests to be cancelled.
Make workers have a CancellationTokenSource
This commit is contained in:
@@ -6,12 +6,12 @@ namespace API.Schema;
|
||||
[PrimaryKey("Key")]
|
||||
public abstract class Identifiable
|
||||
{
|
||||
public Identifiable()
|
||||
protected Identifiable()
|
||||
{
|
||||
this.Key = TokenGen.CreateToken(this.GetType());
|
||||
}
|
||||
|
||||
public Identifiable(string key)
|
||||
|
||||
protected Identifiable(string key)
|
||||
{
|
||||
this.Key = key;
|
||||
}
|
||||
|
@@ -104,15 +104,15 @@ public class MangaContext(DbContextOptions<MangaContext> options) : TrangaBaseCo
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
|
||||
public Manga? FindMangaLike(Manga other)
|
||||
public async Task<Manga?> FindMangaLike(Manga other, CancellationToken token)
|
||||
{
|
||||
if (MangaIncludeAll().FirstOrDefault(m => m.Key == other.Key) is { } f)
|
||||
if (await MangaIncludeAll().FirstOrDefaultAsync(m => m.Key == other.Key, token) is { } f)
|
||||
return f;
|
||||
|
||||
return MangaIncludeAll()
|
||||
.FirstOrDefault(m => m.Links.Any(l => l.Key == other.Key) ||
|
||||
m.AltTitles.Any(t => other.AltTitles.Select(ot => ot.Title)
|
||||
.Any(s => s.Equals(t.Title))));
|
||||
return await MangaIncludeAll()
|
||||
.FirstOrDefaultAsync(m =>
|
||||
m.Links.Any(l => l.Key == other.Key) ||
|
||||
m.AltTitles.Any(t => other.AltTitles.Select(ot => ot.Title).Any(s => s.Equals(t.Title))), token);
|
||||
}
|
||||
|
||||
public IIncludableQueryable<Manga, ICollection<MangaConnectorId<Manga>>> MangaIncludeAll() => Mangas.Include(m => m.Library)
|
||||
|
@@ -31,5 +31,5 @@ public abstract class MetadataFetcher
|
||||
/// <summary>
|
||||
/// Updates the Manga linked in the MetadataEntry
|
||||
/// </summary>
|
||||
public abstract void UpdateMetadata(MetadataEntry metadataEntry, MangaContext dbContext);
|
||||
public abstract Task UpdateMetadata(MetadataEntry metadataEntry, MangaContext dbContext, CancellationToken token);
|
||||
}
|
@@ -43,21 +43,25 @@ public class MyAnimeList : MetadataFetcher
|
||||
/// </summary>
|
||||
/// <param name="metadataEntry"></param>
|
||||
/// <param name="dbContext"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <exception cref="FormatException"></exception>
|
||||
/// <exception cref="DbUpdateException"></exception>
|
||||
public override void UpdateMetadata(MetadataEntry metadataEntry, MangaContext dbContext)
|
||||
public override async Task UpdateMetadata(MetadataEntry metadataEntry, MangaContext dbContext, CancellationToken token)
|
||||
{
|
||||
Manga dbManga = dbContext.Mangas.Find(metadataEntry.MangaId)!;
|
||||
if (await dbContext.Mangas.FirstOrDefaultAsync(m => m.Key == metadataEntry.MangaId, token) is not { } dbManga)
|
||||
throw new DbUpdateException("Manga not found");
|
||||
|
||||
foreach (CollectionEntry collectionEntry in dbContext.Entry(dbManga).Collections)
|
||||
collectionEntry.Load();
|
||||
dbContext.Entry(dbManga).Navigation(nameof(Manga.Library)).Load();
|
||||
await collectionEntry.LoadAsync(token);
|
||||
await dbContext.Entry(dbManga).Navigation(nameof(Manga.Library)).LoadAsync(token);
|
||||
|
||||
MangaFull resultData;
|
||||
try
|
||||
{
|
||||
long id = long.Parse(metadataEntry.Identifier);
|
||||
resultData = Jikan.GetMangaFullDataAsync(id).Result.Data;
|
||||
if(await Jikan.GetMangaFullDataAsync(id, token) is not { } response)
|
||||
throw new DbUpdateException("Manga Data not found");
|
||||
resultData = response.Data;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -71,7 +75,7 @@ public class MyAnimeList : MetadataFetcher
|
||||
dbManga.Authors.Clear();
|
||||
dbManga.Authors = resultData.Authors.Select(a => new Author(a.Name)).ToList();
|
||||
|
||||
dbContext.Sync();
|
||||
await dbContext.Sync(token);
|
||||
}
|
||||
|
||||
}
|
@@ -22,11 +22,11 @@ public abstract class TrangaBaseContext<T> : DbContext where T : DbContext
|
||||
}, Array.Empty<string>(), LogLevel.Warning, DbContextLoggerOptions.Level | DbContextLoggerOptions.Category | DbContextLoggerOptions.UtcTime);
|
||||
}
|
||||
|
||||
internal (bool success, string? exceptionMessage) Sync()
|
||||
internal async Task<(bool success, string? exceptionMessage)> Sync(CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.SaveChanges();
|
||||
await this.SaveChangesAsync(token);
|
||||
return (true, null);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
Reference in New Issue
Block a user