This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: base structure of jobs library
- Loading branch information
1 parent
b67bbed
commit 521fe40
Showing
6 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace EasyLib.Enums; | ||
|
||
public enum JobState | ||
{ | ||
End, | ||
SourceScan, | ||
DifferenceCalculation, | ||
Copy, | ||
Paused | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EasyLib.Enums; | ||
|
||
public enum JobType | ||
{ | ||
Full, | ||
Differential, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace EasyLib.Events; | ||
|
||
public interface IJobStatusPublisher | ||
{ | ||
/// <summary> | ||
/// Subscribe to the job-related events | ||
/// </summary> | ||
/// <param name="subscriber">Class that will be notified of the events</param> | ||
public void Subscribe(IJobStatusSubscriber subscriber); | ||
|
||
/// <summary> | ||
/// Unsubscribe from the job-related events | ||
/// </summary> | ||
/// <param name="subscriber">Class that will stop to be notified</param> | ||
public void Unsubscribe(IJobStatusSubscriber subscriber); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using EasyLib.Enums; | ||
|
||
namespace EasyLib.Events; | ||
|
||
public interface IJobStatusSubscriber | ||
{ | ||
/// <summary> | ||
/// Event triggered when the job state changes (running, paused, steps...) | ||
/// </summary> | ||
/// <param name="state">New step of the job</param> | ||
/// <param name="job">Concerned job</param> | ||
public void OnJobStateChange(JobState state, Job.Job job) | ||
{ | ||
// Implementation optional | ||
} | ||
|
||
/// <summary> | ||
/// Event triggered when an error occurs during the job | ||
/// </summary> | ||
/// <param name="error"></param> | ||
public void OnJobError(string error) | ||
{ | ||
// Implementation optional | ||
} | ||
|
||
/// <summary> | ||
/// Event triggered when the job progress changes (number of files, bytes, etc.) | ||
/// </summary> | ||
/// <param name="job">Concerned job</param> | ||
public void OnJobProgress(Job.Job job) | ||
{ | ||
// Implementation optional | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using EasyLib.Enums; | ||
using EasyLib.Events; | ||
|
||
namespace EasyLib.Job; | ||
|
||
/// <summary> | ||
/// A job is a backup task that can be run by the application. It can be a copy, a move, a delete, etc. | ||
/// </summary> | ||
/// <param name="name">Name of the task</param> | ||
/// <param name="sourceFolder">Folder to backup</param> | ||
/// <param name="destinationFolder">Backup destination</param> | ||
/// <param name="type">Backup type (full, differential)</param> | ||
/// <param name="state">Current state</param> | ||
public class Job(string name, string sourceFolder, string destinationFolder, JobType type, | ||
JobState state = JobState.End) : IJobStatusPublisher | ||
{ | ||
private readonly List<IJobStatusSubscriber> _observers = new(); | ||
public string DestinationFolder = destinationFolder; | ||
public ulong FilesBytesCopied = 0; | ||
public uint FilesCopied = 0; | ||
public uint FilesCount = 0; | ||
public ulong FilesSizeBytes = 0; | ||
public uint Id; | ||
public string Name = name; | ||
public string SourceFolder = sourceFolder; | ||
public JobState State = state; | ||
public JobType Type = type; | ||
|
||
public void Subscribe(IJobStatusSubscriber subscriber) | ||
{ | ||
_observers.Add(subscriber); | ||
} | ||
|
||
public void Unsubscribe(IJobStatusSubscriber subscriber) | ||
{ | ||
_observers.Remove(subscriber); | ||
} | ||
|
||
/// <summary> | ||
/// Check if the job is valid (name, source and destination reachable, etc.) | ||
/// </summary> | ||
/// <returns>True if the job is valid</returns> | ||
public bool Check() | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Run the backup job | ||
/// </summary> | ||
/// <returns>True when the job is complete</returns> | ||
public bool Run() | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Pause a job execution | ||
/// </summary> | ||
/// <returns>True when the job is paused</returns> | ||
public bool Pause() | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Cancel a running or a paused job, make its status to "End" | ||
/// </summary> | ||
/// <returns></returns> | ||
public bool Cancel() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using EasyLib.Enums; | ||
using EasyLib.Events; | ||
|
||
namespace EasyLib; | ||
|
||
/// <summary> | ||
/// Facade to all the job-related work | ||
/// </summary> | ||
public class JobManager : IJobStatusSubscriber, IJobStatusPublisher | ||
{ | ||
/// <summary> | ||
/// List of all available jobs | ||
/// </summary> | ||
private readonly List<Job.Job> _jobs = new(); | ||
|
||
/// <summary> | ||
/// List of all the subscribers to the job-related events | ||
/// </summary> | ||
private readonly List<IJobStatusSubscriber> _subscribers = new(); | ||
|
||
public void Subscribe(IJobStatusSubscriber subscriber) | ||
{ | ||
_subscribers.Add(subscriber); | ||
} | ||
|
||
public void Unsubscribe(IJobStatusSubscriber subscriber) | ||
{ | ||
_subscribers.Remove(subscriber); | ||
} | ||
|
||
/// <summary> | ||
/// Get all the jobs | ||
/// </summary> | ||
/// <returns>Stored jobs</returns> | ||
public List<Job.Job> GetJobs() | ||
{ | ||
return _jobs; | ||
} | ||
|
||
/// <summary> | ||
/// Populate the list of jobs from the state.json file | ||
/// </summary> | ||
/// <returns>True if the operation is successful</returns> | ||
public bool FetchJobs() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Parse a job string into a list of jobs. IDs and names are accepted. | ||
/// </summary> | ||
/// <param name="jobsIString">Job string, for instance: 1-3,5,job2</param> | ||
/// <returns>The list of found jobs</returns> | ||
public List<Job.Job> GetJobsFromString(string jobsIString) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Get the jobs with the specified IDs | ||
/// </summary> | ||
/// <param name="jobIds">List of job IDs</param> | ||
/// <returns></returns> | ||
public List<Job.Job> GetJobsFromIds(IEnumerable<int> jobIds) | ||
{ | ||
var ids = jobIds.ToList().Select(id => (uint)id); | ||
return _jobs.Where(job => ids.Contains(job.Id)).ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// Start the execution of the specified jobs. If the jobs are paused, they are resumed. | ||
/// </summary> | ||
/// <param name="jobIds">IDs of the jobs to start</param> | ||
/// <returns>True if the execution is complete</returns> | ||
public bool ExecuteJobs(IEnumerable<int> jobIds) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Start the execution of the specified jobs. If the jobs are paused, they are resumed. | ||
/// </summary> | ||
/// <param name="jobs">List of obs to start</param> | ||
/// <returns></returns> | ||
public bool ExecuteJobs(IEnumerable<Job.Job> jobs) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Create a new job | ||
/// </summary> | ||
/// <param name="name">Job name</param> | ||
/// <param name="src">Backup source UNC path</param> | ||
/// <param name="dest">Backup destination UNC path</param> | ||
/// <param name="type">Backup type</param> | ||
/// <returns>Newly created job</returns> | ||
public Job.Job CreateJob(string name, string src, string dest, JobType type) | ||
{ | ||
var newJob = new Job.Job(name, src, dest, type); | ||
_jobs.Add(newJob); | ||
return newJob; | ||
} | ||
|
||
/// <summary> | ||
/// Delete a job | ||
/// </summary> | ||
/// <param name="job">Job to delete</param> | ||
public void DeleteJob(Job.Job job) | ||
{ | ||
_jobs.Remove(job); | ||
} | ||
|
||
/// <summary> | ||
/// Cancel a paused job, make its status to "End" | ||
/// </summary> | ||
/// <param name="job">Job to cancel</param> | ||
public void CancelJob(Job.Job job) | ||
{ | ||
job.Cancel(); | ||
} | ||
} |