Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: sync job running state
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-wff committed Dec 20, 2023
1 parent f5872db commit e018243
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
7 changes: 4 additions & 3 deletions EasyLib/Api/JobManagerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
19 changes: 12 additions & 7 deletions EasyLib/Api/JobManagerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ public JobManagerServer(LocalJobManager localJobManager)
}
}

public CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource();
public CancellationTokenSource CancellationTokenSource { get; } = new();

private void _waitForConnection()
{
try
{
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();
}
Expand All @@ -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
});
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion EasyLib/Api/JobManagerServerWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ public void SendAllJobs(List<Job.Job> 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
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion EasyLib/Job/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// </summary>
public bool CurrentlyRunning { get; protected set; }
public bool CurrentlyRunning { get; set; }

/// <summary>
/// Subscribers to the job-related events
Expand Down
4 changes: 2 additions & 2 deletions EasyLib/JobManager/LocalJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions EasyLib/Json/JsonApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e018243

Please sign in to comment.