From e0182437706cc7639e8da8455bc456e0c10a7b4f Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 17:25:56 +0100 Subject: [PATCH] feat: sync job running state --- EasyLib/Api/JobManagerClient.cs | 7 ++++--- EasyLib/Api/JobManagerServer.cs | 19 ++++++++++++------- EasyLib/Api/JobManagerServerWorker.cs | 7 ++++++- EasyLib/Job/Job.cs | 2 +- EasyLib/JobManager/LocalJobManager.cs | 4 ++-- EasyLib/Json/JsonApiRequest.cs | 5 +++-- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/EasyLib/Api/JobManagerClient.cs b/EasyLib/Api/JobManagerClient.cs index 8206301..48e58ee 100644 --- a/EasyLib/Api/JobManagerClient.cs +++ b/EasyLib/Api/JobManagerClient.cs @@ -37,7 +37,7 @@ public bool SendAction(ApiAction action, Job.Job job) { try { - var json = JsonConvert.SerializeObject(new JsonApiRequest(action, job)) + "\n\r"; + var json = JsonConvert.SerializeObject(new JsonApiRequest(action, job, job.CurrentlyRunning)) + "\n\r"; var data = Encoding.ASCII.GetBytes(json); serverSocket.Send(data); return true; @@ -60,7 +60,7 @@ private void _handleAction(string jsonAction) return; } - var job = _createOrUpdateJob(request.Job); + var job = _createOrUpdateJob(request.Job, request.JobRunning); switch (request.Action) { @@ -93,7 +93,7 @@ private void _handleAction(string jsonAction) } } - private Job.Job _createOrUpdateJob(JsonJob jsonJob) + private Job.Job _createOrUpdateJob(JsonJob jsonJob, bool running) { var job = remoteJobManager.GetJobs().Find(j => j.Id == jsonJob.id) ?? new RemoteJob(jsonJob, this); @@ -108,6 +108,7 @@ private Job.Job _createOrUpdateJob(JsonJob jsonJob) job.FilesBytesCopied = jsonJob.active_job_info?.bytes_copied ?? 0; job.CurrentFileSource = jsonJob.active_job_info?.current_file_source ?? string.Empty; job.CurrentFileDestination = jsonJob.active_job_info?.current_file_destination ?? string.Empty; + job.CurrentlyRunning = running; return job; } } diff --git a/EasyLib/Api/JobManagerServer.cs b/EasyLib/Api/JobManagerServer.cs index e8fa548..42acc90 100644 --- a/EasyLib/Api/JobManagerServer.cs +++ b/EasyLib/Api/JobManagerServer.cs @@ -26,7 +26,7 @@ public JobManagerServer(LocalJobManager localJobManager) } } - public CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource(); + public CancellationTokenSource CancellationTokenSource { get; } = new(); private void _waitForConnection() { @@ -34,16 +34,16 @@ private void _waitForConnection() { while (true) { - TcpClient socket = _serverSocket.AcceptTcpClient(); + var socket = _serverSocket.AcceptTcpClient(); - Worker worker = new Worker(socket, this); + var worker = new Worker(socket, this); AddWorker(worker); worker.SendAllJobs(_localJobManager.GetJobs()); if (CancellationTokenSource.IsCancellationRequested) break; } } - catch (SocketException e) + catch (SocketException) { CleanInstance(); } @@ -67,13 +67,18 @@ public void RemoveWorker(Worker worker) } } - public void Broadcast(ApiAction action, JsonJob jsonJob) + public void Broadcast(ApiAction action, JsonJob jsonJob, bool jobRunning) { lock (ServerLockObject) { - foreach (Worker worker in _workers) + foreach (var worker in _workers) { - worker.Send(new JsonApiRequest() { Action = action, Job = jsonJob }); + worker.Send(new JsonApiRequest + { + Action = action, + Job = jsonJob, + JobRunning = jobRunning + }); } } } diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index 6164542..cff203b 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -60,7 +60,12 @@ public void SendAllJobs(List jobs) { foreach (var job in jobs) { - Send(new JsonApiRequest() { Action = ApiAction.Create, Job = job.ToJsonJob() }); + Send(new JsonApiRequest + { + Action = ApiAction.Create, + Job = job.ToJsonJob(), + JobRunning = job.CurrentlyRunning + }); } } diff --git a/EasyLib/Job/Job.cs b/EasyLib/Job/Job.cs index 32fd96a..95e2f26 100644 --- a/EasyLib/Job/Job.cs +++ b/EasyLib/Job/Job.cs @@ -75,7 +75,7 @@ public abstract class Job( /// True if the job is currently running, false otherwise /// If it's false but the state is not End, it means that the job is paused /// - public bool CurrentlyRunning { get; protected set; } + public bool CurrentlyRunning { get; set; } /// /// Subscribers to the job-related events diff --git a/EasyLib/JobManager/LocalJobManager.cs b/EasyLib/JobManager/LocalJobManager.cs index 222d154..a7a6a10 100644 --- a/EasyLib/JobManager/LocalJobManager.cs +++ b/EasyLib/JobManager/LocalJobManager.cs @@ -50,7 +50,7 @@ public override void OnJobProgress(Job.Job job) StateManager.Instance.WriteJobs(Jobs); } - _server?.Broadcast(ApiAction.Progress, job.ToJsonJob()); + _server?.Broadcast(ApiAction.Progress, job.ToJsonJob(), job.CurrentlyRunning); } public override void OnJobStateChange(JobState state, Job.Job job) @@ -62,7 +62,7 @@ public override void OnJobStateChange(JobState state, Job.Job job) StateManager.Instance.WriteJobs(Jobs); - _server?.Broadcast(ApiAction.State, job.ToJsonJob()); + _server?.Broadcast(ApiAction.State, job.ToJsonJob(), job.CurrentlyRunning); } public override void CleanStop() diff --git a/EasyLib/Json/JsonApiRequest.cs b/EasyLib/Json/JsonApiRequest.cs index d7a9522..53da938 100644 --- a/EasyLib/Json/JsonApiRequest.cs +++ b/EasyLib/Json/JsonApiRequest.cs @@ -2,12 +2,13 @@ namespace EasyLib.Json; -public readonly struct JsonApiRequest(ApiAction action, JsonJob job) +public readonly struct JsonApiRequest(ApiAction action, JsonJob job, bool running) { - public JsonApiRequest(ApiAction action, Job.Job job) : this(action, job.ToJsonJob()) + public JsonApiRequest(ApiAction action, Job.Job job, bool running) : this(action, job.ToJsonJob(), running) { } public ApiAction Action { get; init; } = action; public JsonJob Job { get; init; } = job; + public bool JobRunning { get; init; } = running; }