mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-04 09:54:16 +02:00
Tranga WorkerCycle
This commit is contained in:
@ -21,7 +21,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
||||||
public IActionResult GetAllWorkers()
|
public IActionResult GetAllWorkers()
|
||||||
{
|
{
|
||||||
return Ok(Tranga.Workers.ToArray());
|
return Ok(Tranga.AllWorkers.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -33,7 +33,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
||||||
public IActionResult GetJobs([FromBody]string[] WorkerIds)
|
public IActionResult GetJobs([FromBody]string[] WorkerIds)
|
||||||
{
|
{
|
||||||
return Ok(Tranga.Workers.Where(worker => WorkerIds.Contains(worker.Key)).ToArray());
|
return Ok(Tranga.AllWorkers.Where(worker => WorkerIds.Contains(worker.Key)).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -45,7 +45,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
[ProducesResponseType<BaseWorker[]>(Status200OK, "application/json")]
|
||||||
public IActionResult GetJobsInState(WorkerExecutionState State)
|
public IActionResult GetJobsInState(WorkerExecutionState State)
|
||||||
{
|
{
|
||||||
return Ok(Tranga.Workers.Where(worker => worker.State == State).ToArray());
|
return Ok(Tranga.AllWorkers.Where(worker => worker.State == State).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -59,7 +59,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType(Status404NotFound)]
|
[ProducesResponseType(Status404NotFound)]
|
||||||
public IActionResult GetJob(string WorkerId)
|
public IActionResult GetJob(string WorkerId)
|
||||||
{
|
{
|
||||||
if(Tranga.Workers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
if(Tranga.AllWorkers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
||||||
return NotFound(nameof(WorkerId));
|
return NotFound(nameof(WorkerId));
|
||||||
return Ok(worker);
|
return Ok(worker);
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType(Status404NotFound)]
|
[ProducesResponseType(Status404NotFound)]
|
||||||
public IActionResult DeleteJob(string WorkerId)
|
public IActionResult DeleteJob(string WorkerId)
|
||||||
{
|
{
|
||||||
if(Tranga.Workers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
if(Tranga.AllWorkers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
||||||
return NotFound(nameof(WorkerId));
|
return NotFound(nameof(WorkerId));
|
||||||
Tranga.RemoveWorker(worker);
|
Tranga.RemoveWorker(worker);
|
||||||
return Ok();
|
return Ok();
|
||||||
@ -97,7 +97,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType<string>(Status409Conflict, "text/plain")]
|
[ProducesResponseType<string>(Status409Conflict, "text/plain")]
|
||||||
public IActionResult ModifyJob(string WorkerId, [FromBody]ModifyWorkerRecord modifyWorkerRecord)
|
public IActionResult ModifyJob(string WorkerId, [FromBody]ModifyWorkerRecord modifyWorkerRecord)
|
||||||
{
|
{
|
||||||
if(Tranga.Workers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
if(Tranga.AllWorkers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
||||||
return NotFound(nameof(WorkerId));
|
return NotFound(nameof(WorkerId));
|
||||||
|
|
||||||
if(modifyWorkerRecord.IntervalMs is not null && worker is not IPeriodic)
|
if(modifyWorkerRecord.IntervalMs is not null && worker is not IPeriodic)
|
||||||
@ -121,7 +121,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType<string>(Status412PreconditionFailed, "text/plain")]
|
[ProducesResponseType<string>(Status412PreconditionFailed, "text/plain")]
|
||||||
public IActionResult StartJob(string WorkerId)
|
public IActionResult StartJob(string WorkerId)
|
||||||
{
|
{
|
||||||
if(Tranga.Workers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
if(Tranga.AllWorkers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
||||||
return NotFound(nameof(WorkerId));
|
return NotFound(nameof(WorkerId));
|
||||||
|
|
||||||
if (worker.State >= WorkerExecutionState.Waiting)
|
if (worker.State >= WorkerExecutionState.Waiting)
|
||||||
@ -142,7 +142,7 @@ public class WorkerController(ILog Log) : Controller
|
|||||||
[ProducesResponseType(Status501NotImplemented)]
|
[ProducesResponseType(Status501NotImplemented)]
|
||||||
public IActionResult StopJob(string WorkerId)
|
public IActionResult StopJob(string WorkerId)
|
||||||
{
|
{
|
||||||
if(Tranga.Workers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
if(Tranga.AllWorkers.FirstOrDefault(w => w.Key == WorkerId) is not { } worker)
|
||||||
return NotFound(nameof(WorkerId));
|
return NotFound(nameof(WorkerId));
|
||||||
|
|
||||||
if(worker.State is < WorkerExecutionState.Running or >= WorkerExecutionState.Completed)
|
if(worker.State is < WorkerExecutionState.Running or >= WorkerExecutionState.Completed)
|
||||||
|
@ -32,8 +32,8 @@ public static class Tranga
|
|||||||
Log.Info(TRANGA);
|
Log.Info(TRANGA);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static HashSet<BaseWorker> Workers { get; private set; } = new ();
|
internal static HashSet<BaseWorker> AllWorkers { get; private set; } = new ();
|
||||||
public static void AddWorker(BaseWorker worker) => Workers.Add(worker);
|
public static void AddWorker(BaseWorker worker) => AllWorkers.Add(worker);
|
||||||
public static void AddWorkers(IEnumerable<BaseWorker> workers)
|
public static void AddWorkers(IEnumerable<BaseWorker> workers)
|
||||||
{
|
{
|
||||||
foreach (BaseWorker baseWorker in workers)
|
foreach (BaseWorker baseWorker in workers)
|
||||||
@ -42,21 +42,14 @@ public static class Tranga
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void StopWorker(BaseWorker worker) => RemoveWorker(worker);
|
|
||||||
|
|
||||||
public static void RemoveWorker(BaseWorker removeWorker)
|
public static void RemoveWorker(BaseWorker removeWorker)
|
||||||
{
|
{
|
||||||
IEnumerable<BaseWorker> baseWorkers = Workers.Where(w => w.DependenciesAndSelf.Any(worker => worker == removeWorker));
|
IEnumerable<BaseWorker> baseWorkers = AllWorkers.Where(w => w.DependenciesAndSelf.Any(worker => worker == removeWorker));
|
||||||
|
|
||||||
foreach (BaseWorker worker in baseWorkers)
|
foreach (BaseWorker worker in baseWorkers)
|
||||||
{
|
{
|
||||||
worker.Cancel();
|
StopWorker(worker);
|
||||||
Workers.Remove(worker);
|
AllWorkers.Remove(worker);
|
||||||
if (RunningWorkers.ContainsKey(worker))
|
|
||||||
{
|
|
||||||
worker.Cancel();
|
|
||||||
RunningWorkers.Remove(worker);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,6 +69,9 @@ public static class Tranga
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
CheckRunningWorkers();
|
CheckRunningWorkers();
|
||||||
|
|
||||||
|
foreach (BaseWorker baseWorker in AllWorkers.DueWorkers())
|
||||||
|
StartWorkers.Add(baseWorker);
|
||||||
|
|
||||||
foreach (BaseWorker worker in StartWorkers)
|
foreach (BaseWorker worker in StartWorkers)
|
||||||
{
|
{
|
||||||
@ -96,12 +92,31 @@ public static class Tranga
|
|||||||
{
|
{
|
||||||
RunningWorkers.Remove(worker);
|
RunningWorkers.Remove(worker);
|
||||||
foreach (BaseWorker newWorker in task.Result)
|
foreach (BaseWorker newWorker in task.Result)
|
||||||
StartWorkers.Add(newWorker);
|
AllWorkers.Add(newWorker);
|
||||||
task.Dispose();
|
task.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<BaseWorker> DueWorkers(this IEnumerable<BaseWorker> workers)
|
||||||
|
{
|
||||||
|
return workers.Where(w =>
|
||||||
|
{
|
||||||
|
if (w.State is >= WorkerExecutionState.Running and < WorkerExecutionState.Completed)
|
||||||
|
return false;
|
||||||
|
if (w is IPeriodic periodicWorker)
|
||||||
|
return periodicWorker.IsDue;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
internal static void MarkWorkerForStart(BaseWorker worker) => StartWorkers.Add(worker);
|
internal static void MarkWorkerForStart(BaseWorker worker) => StartWorkers.Add(worker);
|
||||||
|
|
||||||
|
internal static void StopWorker(BaseWorker worker)
|
||||||
|
{
|
||||||
|
StartWorkers.Remove(worker);
|
||||||
|
worker.Cancel();
|
||||||
|
RunningWorkers.Remove(worker);
|
||||||
|
}
|
||||||
|
|
||||||
internal static bool AddMangaToContext((Manga, MangaConnectorId<Manga>) addManga, MangaContext context, [NotNullWhen(true)]out Manga? manga) => AddMangaToContext(addManga.Item1, addManga.Item2, context, out manga);
|
internal static bool AddMangaToContext((Manga, MangaConnectorId<Manga>) addManga, MangaContext context, [NotNullWhen(true)]out Manga? manga) => AddMangaToContext(addManga.Item1, addManga.Item2, context, out manga);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user