11 Commits

Author SHA1 Message Date
e063cf1fd9 Debug: MatchJobsRunningAndWaiting
Some checks failed
Docker Image CI / build (push) Has been cancelled
UpdateCoverJobs not starting.
2025-06-28 23:15:51 +02:00
8170e1d762 JobCycle Info-Debug list jobs started/running
Some checks failed
Docker Image CI / build (push) Has been cancelled
2025-06-28 20:35:10 +02:00
254383b006 Include Description in ComicInfo.xml 2025-06-28 20:28:28 +02:00
df431e533a Add POST Jobs/Cleaup Endpoint:
Removes failed and completed Jobs (that are not recurring)
2025-06-28 20:18:28 +02:00
9a4cc0cbaf Only log Error on image-processing if we dont know what Exception was thrown 2025-06-28 20:13:09 +02:00
861cf7e166 Fix Image-Processing:
Format is not supported by Imagesharp, throwing exception causing Job to fail.
2025-06-28 20:00:01 +02:00
7e34b3b91e Update readme to contain information on how to test locally 2025-06-28 19:48:47 +02:00
29d36484f9 include logging driver in docker-compose
Remove parameters from start-CMD in Dockerfile
2025-06-28 19:39:19 +02:00
2c6e8e4d16 Default startNewJobTimeoutMs set to 20s
Some checks failed
Docker Image CI / build (push) Has been cancelled
2025-06-18 02:11:03 +02:00
fab2886684 ComickIo Stop double work for retrieving chapters:
We can build the canonical url from the hids
2025-06-18 01:55:19 +02:00
d9ccf71b21 DownloadSingleChapterJob add check if chapter is already downloaded before re-downloading 2025-06-18 01:18:06 +02:00
10 changed files with 131 additions and 53 deletions

View File

@ -374,4 +374,25 @@ public class JobController(PgsqlContext context, ILog Log) : Controller
{ {
return StatusCode(Status501NotImplemented); return StatusCode(Status501NotImplemented);
} }
/// <summary>
/// Removes failed and completed Jobs (that are not recurring)
/// </summary>
/// <response code="202">Job started</response>
/// <response code="500">Error during Database Operation</response>
[HttpPost("Cleanup")]
public IActionResult CleanupJobs()
{
try
{
context.Jobs.RemoveRange(context.Jobs.Where(j => j.state == JobState.Failed || j.state == JobState.Completed));
context.SaveChanges();
return Ok();
}
catch (Exception e)
{
Log.Error(e);
return StatusCode(500, e.Message);
}
}
} }

View File

@ -187,6 +187,8 @@ public class Chapter : IComparable<Chapter>
comicInfo.Add(new XElement("Writer", string.Join(',', ParentManga.Authors.Select(author => author.AuthorName)))); comicInfo.Add(new XElement("Writer", string.Join(',', ParentManga.Authors.Select(author => author.AuthorName))));
if(ParentManga.OriginalLanguage is not null) if(ParentManga.OriginalLanguage is not null)
comicInfo.Add(new XElement("LanguageISO", ParentManga.OriginalLanguage)); comicInfo.Add(new XElement("LanguageISO", ParentManga.OriginalLanguage));
if(ParentManga.Description != string.Empty)
comicInfo.Add(new XElement("Summary", ParentManga.Description));
return comicInfo.ToString(); return comicInfo.ToString();
} }

View File

@ -45,6 +45,11 @@ public class DownloadSingleChapterJob : Job
protected override IEnumerable<Job> RunInternal(PgsqlContext context) protected override IEnumerable<Job> RunInternal(PgsqlContext context)
{ {
if (Chapter.Downloaded)
{
Log.Info("Chapter was already downloaded.");
return [];
}
string[] imageUrls = Chapter.ParentManga.MangaConnector.GetChapterImageUrls(Chapter); string[] imageUrls = Chapter.ParentManga.MangaConnector.GetChapterImageUrls(Chapter);
if (imageUrls.Length < 1) if (imageUrls.Length < 1)
{ {
@ -129,21 +134,39 @@ public class DownloadSingleChapterJob : Job
{ {
if (!TrangaSettings.bwImages && TrangaSettings.compression == 100) if (!TrangaSettings.bwImages && TrangaSettings.compression == 100)
{ {
Log.Debug($"No processing requested for image"); Log.Debug("No processing requested for image");
return; return;
} }
Log.Debug($"Processing image: {imagePath}"); Log.Debug($"Processing image: {imagePath}");
try
{
using Image image = Image.Load(imagePath); using Image image = Image.Load(imagePath);
File.Delete(imagePath);
if (TrangaSettings.bwImages) if (TrangaSettings.bwImages)
image.Mutate(i => i.ApplyProcessor(new AdaptiveThresholdProcessor())); image.Mutate(i => i.ApplyProcessor(new AdaptiveThresholdProcessor()));
File.Delete(imagePath);
image.SaveAsJpeg(imagePath, new JpegEncoder() image.SaveAsJpeg(imagePath, new JpegEncoder()
{ {
Quality = TrangaSettings.compression Quality = TrangaSettings.compression
}); });
} }
catch (Exception e)
{
if (e is UnknownImageFormatException or NotSupportedException)
{
//If the Image-Format is not processable by ImageSharp, we can't modify it.
Log.Debug($"Unable to process {imagePath}: Not supported image format");
}else if (e is InvalidImageContentException)
{
Log.Debug($"Unable to process {imagePath}: Invalid Content");
}
else
{
Log.Error(e);
}
}
}
private void CopyCoverFromCacheToDownloadLocation(Manga manga) private void CopyCoverFromCacheToDownloadLocation(Manga manga)
{ {

View File

@ -78,7 +78,7 @@ public class ComickIo : MangaConnector
public override Chapter[] GetChapters(Manga manga, string? language = null) public override Chapter[] GetChapters(Manga manga, string? language = null)
{ {
Log.Info($"Getting Chapters: {manga.IdOnConnectorSite}"); Log.Info($"Getting Chapters: {manga.IdOnConnectorSite}");
List<string> chapterHids = new(); List<Chapter> chapters = new();
int page = 1; int page = 1;
while(page < 50) while(page < 50)
{ {
@ -95,16 +95,13 @@ public class ComickIo : MangaConnector
JToken data = JToken.Parse(sr.ReadToEnd()); JToken data = JToken.Parse(sr.ReadToEnd());
JArray? chaptersArray = data["chapters"] as JArray; JArray? chaptersArray = data["chapters"] as JArray;
if (chaptersArray?.Count < 1) if (chaptersArray is null || chaptersArray.Count < 1)
break; break;
chapterHids.AddRange(chaptersArray?.Select(token => token.Value<string>("hid")!)!); chapters.AddRange(ParseChapters(manga, chaptersArray));
page++; page++;
} }
Log.Debug($"Getting chapters for {manga.Name} yielded {chapterHids.Count} hids. Requesting chapters now...");
List<Chapter> chapters = chapterHids.Select(hid => ChapterFromHid(manga, hid)).ToList();
return chapters.ToArray(); return chapters.ToArray();
} }
@ -219,29 +216,23 @@ public class ComickIo : MangaConnector
year: year, originalLanguage: originalLanguage); year: year, originalLanguage: originalLanguage);
} }
private Chapter ChapterFromHid(Manga parentManga, string hid) private List<Chapter> ParseChapters(Manga parentManga, JArray chaptersArray)
{ {
string requestUrl = $"https://api.comick.fun/chapter/{hid}"; List<Chapter> chapters = new ();
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.Default); foreach (JToken chapter in chaptersArray)
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
{ {
Log.Error("Request failed"); string? chapterNum = chapter.Value<string>("chap");
throw new Exception("Request failed"); string? volumeNumStr = chapter.Value<string>("vol");
}
using StreamReader sr = new (result.result);
JToken data = JToken.Parse(sr.ReadToEnd());
string? canonical = data.Value<string>("canonical");
string? chapterNum = data["chapter"]?.Value<string>("chap");
string? volumeNumStr = data["chapter"]?.Value<string>("vol");
int? volumeNum = volumeNumStr is null ? null : int.Parse(volumeNumStr); int? volumeNum = volumeNumStr is null ? null : int.Parse(volumeNumStr);
string? title = data["chapter"]?.Value<string>("title"); string? title = chapter.Value<string>("title");
string? hid = chapter.Value<string>("hid");
string url = $"https://comick.io/comic/{parentManga.IdOnConnectorSite}/{hid}";
if(chapterNum is null) if(chapterNum is null || hid is null)
throw new Exception("chapterNum is null"); continue;
string url = $"https://comick.io{canonical}"; chapters.Add(new (parentManga, url, chapterNum, volumeNum, hid, title));
return new Chapter(parentManga, url, chapterNum, volumeNum, hid, title); }
return chapters;
} }
} }

View File

@ -169,11 +169,15 @@ public static class Tranga
while(!running) while(!running)
Thread.Sleep(10); Thread.Sleep(10);
} }
Log.Debug($"Running: {runningJobs.Count} Waiting: {waitingJobs.Count} Due: {dueJobs.Count} of which \n" + Log.Debug($"Running: {runningJobs.Count}\n" +
$"{jobsWithoutDependencies.Count} without missing dependencies, of which\n" + $"{string.Join("\n", runningJobs.Select(s => "\t- " + s))}\n" +
$"Waiting: {waitingJobs.Count} Due: {dueJobs.Count}\n" +
$"{string.Join("\n", dueJobs.Select(s => "\t- " + s))}\n" +
$"of which {jobsWithoutDependencies.Count} without missing dependencies, of which\n" +
$"\t{jobsWithoutDownloading.Count} without downloading\n" + $"\t{jobsWithoutDownloading.Count} without downloading\n" +
$"\t{jobsNotHeldBackByConnector.Count} not held back by Connector\n" + $"\t{jobsNotHeldBackByConnector.Count} not held back by Connector\n" +
$"{startJobs.Count} were started."); $"{startJobs.Count} were started:\n" +
$"{string.Join("\n", startJobs.Select(s => "\t- " + s))}");
if (Log.IsDebugEnabled && dueJobs.Count < 1) if (Log.IsDebugEnabled && dueJobs.Count < 1)
if(waitingJobs.MinBy(j => j.NextExecution) is { } nextJob) if(waitingJobs.MinBy(j => j.NextExecution) is { } nextJob)
@ -272,13 +276,17 @@ public static class Tranga
private static List<Job> MatchJobsRunningAndWaiting(Dictionary<string, Dictionary<JobType, List<Job>>> running, private static List<Job> MatchJobsRunningAndWaiting(Dictionary<string, Dictionary<JobType, List<Job>>> running,
Dictionary<string, Dictionary<JobType, List<Job>>> waiting) Dictionary<string, Dictionary<JobType, List<Job>>> waiting)
{ {
Log.Debug($"Matching {running.Count} running Jobs to {waiting.Count} waiting Jobs. Busy Connectors: {string.Join(", ", running.Select(r => r.Key))}");
DateTime start = DateTime.UtcNow; DateTime start = DateTime.UtcNow;
List<Job> ret = new(); List<Job> ret = new();
//Foreach MangaConnector
foreach ((string connector, Dictionary<JobType, List<Job>> jobTypeJobsWaiting) in waiting) foreach ((string connector, Dictionary<JobType, List<Job>> jobTypeJobsWaiting) in waiting)
{ {
//Check if MangaConnector has a Job running
if (running.TryGetValue(connector, out Dictionary<JobType, List<Job>>? jobTypeJobsRunning)) if (running.TryGetValue(connector, out Dictionary<JobType, List<Job>>? jobTypeJobsRunning))
{ //MangaConnector has running Jobs {
//Match per JobType //MangaConnector has running Jobs
//Match per JobType (MangaConnector can have 1 Job per Type running at the same time)
foreach ((JobType jobType, List<Job> jobsWaiting) in jobTypeJobsWaiting) foreach ((JobType jobType, List<Job> jobsWaiting) in jobTypeJobsWaiting)
{ {
if(jobTypeJobsRunning.ContainsKey(jobType)) if(jobTypeJobsRunning.ContainsKey(jobType))
@ -293,9 +301,13 @@ public static class Tranga
} }
} }
else else
{ //MangaConnector has no running Jobs {
//MangaConnector has no running Jobs
foreach ((JobType jobType, List<Job> jobsWaiting) in jobTypeJobsWaiting) foreach ((JobType jobType, List<Job> jobsWaiting) in jobTypeJobsWaiting)
{ {
if(ret.Any(j => j.JobType == jobType))
//Already a job of type to be started
continue;
if (jobType is not JobType.DownloadSingleChapterJob) if (jobType is not JobType.DownloadSingleChapterJob)
//If it is not a DownloadSingleChapterJob, just add the first //If it is not a DownloadSingleChapterJob, just add the first
ret.Add(jobsWaiting.First()); ret.Add(jobsWaiting.First());

View File

@ -36,7 +36,7 @@ public static class TrangaSettings
[JsonIgnore] [JsonIgnore]
public static string coverImageCache => Path.Join(workingDirectory, "imageCache"); public static string coverImageCache => Path.Join(workingDirectory, "imageCache");
public static bool aprilFoolsMode { get; private set; } = true; public static bool aprilFoolsMode { get; private set; } = true;
public static int startNewJobTimeoutMs { get; private set; } = 5000; public static int startNewJobTimeoutMs { get; private set; } = 20000;
[JsonIgnore] [JsonIgnore]
internal static readonly Dictionary<RequestType, int> DefaultRequestLimits = new () internal static readonly Dictionary<RequestType, int> DefaultRequestLimits = new ()
{ {

View File

@ -39,4 +39,4 @@ WORKDIR /publish
COPY --chown=1000:1000 --from=build-env /publish . COPY --chown=1000:1000 --from=build-env /publish .
USER 0 USER 0
ENTRYPOINT ["dotnet", "/publish/API.dll"] ENTRYPOINT ["dotnet", "/publish/API.dll"]
CMD ["-f", "-c", "-l", "/usr/share/tranga-api/logs"] CMD [""]

View File

@ -84,17 +84,16 @@ Endpoints are documented in Swagger. Just spin up an instance, and go to `http:/
## Built With ## Built With
- .NET
- ASP.NET - ASP.NET
- Entity Framework Core - Entity Framework Core
- [PostgreSQL](https://www.postgresql.org/about/licence/) - [PostgreSQL](https://www.postgresql.org/about/licence/)
- [Swagger](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/LICENSE)
- [Ngpsql](https://github.com/npgsql/npgsql/blob/main/LICENSE) - [Ngpsql](https://github.com/npgsql/npgsql/blob/main/LICENSE)
- [Swagger](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/LICENSE)
- [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md) - [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md)
- [Sixlabors.ImageSharp](https://docs-v2.sixlabors.com/articles/imagesharp/index.html#license)
- [PuppeteerSharp](https://github.com/hardkoded/puppeteer-sharp/blob/master/LICENSE) - [PuppeteerSharp](https://github.com/hardkoded/puppeteer-sharp/blob/master/LICENSE)
- [Html Agility Pack (HAP)](https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE) - [Html Agility Pack (HAP)](https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE)
- [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch/blob/main/LICENSE) - [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch/blob/main/LICENSE)
- [Sixlabors.ImageSharp](https://docs-v2.sixlabors.com/articles/imagesharp/index.html#license)
- 💙 Blåhaj 🦈 - 💙 Blåhaj 🦈
<p align="right">(<a href="#readme-top">back to top</a>)</p> <p align="right">(<a href="#readme-top">back to top</a>)</p>
@ -126,13 +125,13 @@ access the folder. Permission conflicts with Komga and Kavita should thus be lim
### Bare-Metal ### Bare-Metal
While not supported/currently built, Tranga will also run Bare-Metal without issue. While not supported/currently built, Tranga should also run Bare-Metal without issue.
Configuration-Files will be stored per OS: Configuration-Files will be stored per OS:
- Linux `/usr/share/tranga-api` - Linux `/usr/share/tranga-api`
- Windows `%appdata%/tranga-api` - Windows `%appdata%/tranga-api`
Downloads (default) are stored in - but this can be configured in `settings.json`: Downloads (default) are stored in - but this can be configured in `settings.json` (which will be generated on first after first launch):
- Linux `/Manga` - Linux `/Manga`
- Windows `%currentDirectory%/Downloads` - Windows `%currentDirectory%/Downloads`
@ -148,9 +147,10 @@ If you want to contribute, please feel free to fork and create a Pull-Request!
General rules: General rules:
- Strongly-type your variables. This improves readability. - Strongly-type your variables. This improves readability.
```csharp ```csharp
var xyz = Object.GetSomething(); //Do not do this. What type is xyz? var xyz = Object.GetSomething(); //Do not do this. What type is xyz (without looking at Method returns etc.)?
Manga[] zyx = Object.GetAnotherThing(); //I can now easily see that zyx is an Array. Manga[] zyx = Object.GetAnotherThing(); //I can now easily see that zyx is an Array.
``` ```
Tranga is using a code-first Entity-Framework Core approach. If you modify the db-table structure you need to create a migration.
**A broad overview of where is what:**<br /> **A broad overview of where is what:**<br />
@ -171,6 +171,10 @@ If you want to add a new Website-Connector: <br />
in the constructor). in the constructor).
4. In `Program.cs` add a new Object to the Array. 4. In `Program.cs` add a new Object to the Array.
### How to test locally
In the Project root a `docker-compose.local.yaml` file will compile the code and create the container(s).
<!-- LICENSE --> <!-- LICENSE -->
## License ## License

View File

@ -16,6 +16,11 @@ services:
environment: environment:
- POSTGRES_HOST=tranga-pg - POSTGRES_HOST=tranga-pg
restart: unless-stopped restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"
tranga-pg: tranga-pg:
image: postgres:latest image: postgres:latest
container_name: tranga-pg container_name: tranga-pg
@ -30,3 +35,8 @@ services:
retries: 5 retries: 5
start_period: 80s start_period: 80s
restart: unless-stopped restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"

View File

@ -1,7 +1,7 @@
version: '3' version: '3'
services: services:
tranga-api: tranga-api:
image: glax/tranga-api:latest image: glax/tranga-api:Server-V2
container_name: tranga-api container_name: tranga-api
volumes: volumes:
- ./Manga:/Manga - ./Manga:/Manga
@ -14,14 +14,24 @@ services:
environment: environment:
- POSTGRES_HOST=tranga-pg - POSTGRES_HOST=tranga-pg
restart: unless-stopped restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"
tranga-website: tranga-website:
image: glax/tranga-website:latest image: glax/tranga-website:Server-V2
container_name: tranga-website container_name: tranga-website
ports: ports:
- "9555:80" - "9555:80"
depends_on: depends_on:
- tranga-api - tranga-api
restart: unless-stopped restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"
tranga-pg: tranga-pg:
image: postgres:latest image: postgres:latest
container_name: tranga-pg container_name: tranga-pg
@ -36,3 +46,8 @@ services:
retries: 5 retries: 5
start_period: 80s start_period: 80s
restart: unless-stopped restart: unless-stopped
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"