-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSyncJob.cs
More file actions
124 lines (116 loc) · 4.33 KB
/
FileSyncJob.cs
File metadata and controls
124 lines (116 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using FileSyncLibNet.Commons;
using FileSyncLibNet.FileCleanJob;
using FileSyncLibNet.SyncProviders;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FileSyncLibNet.FileSyncJob
{
public class FileSyncJob : IFileJob
{
public string JobName { get { return $"Type: {options.GetType()} Destination: {options.DestinationPath} ({options.Interval})"; } }
public event EventHandler<FileSyncJobEventArgs> JobStarted;
public event EventHandler<FileSyncJobEventArgs> JobFinished;
public event EventHandler<FileSyncJobEventArgs> JobError;
private readonly IFileJobOptions options;
private readonly Timer timer;
private readonly ISyncProvider syncProvider;
private volatile bool v_jobRunning = false;
public static bool InitialFullSync { get; set; } = false;
private FileSyncJob(IFileJobOptions fileSyncJobOptions)
{
options = fileSyncJobOptions;
timer = new Timer(TimerElapsed);
switch (options.FileSyncProvider)
{
default:
case SyncProvider.FileIO:
syncProvider = new FileIOProvider(fileSyncJobOptions);
break;
case SyncProvider.SMBLib:
syncProvider = new SmbLibProvider(fileSyncJobOptions);
break;
case SyncProvider.Robocopy:
syncProvider = new RoboCopyProvider(fileSyncJobOptions);
break;
case SyncProvider.SCP:
syncProvider = new ScpProvider(fileSyncJobOptions);
break;
case SyncProvider.Abstract:
syncProvider = new AbstractProvider(fileSyncJobOptions);
break;
}
}
public static IFileJob CreateJob(IFileJobOptions fileSyncJobOptions)
{
return new FileSyncJob(fileSyncJobOptions);
}
public static IFileSyncJobBuilder CreateJobBuilder()
{
return new FileSyncJobBuilder();
}
public void ExecuteNow()
{
RunJobInterlocked();
}
public Task ExecuteNowASync()
{
return Task.Run(() => { RunJobInterlocked(); });
}
public void StartJob()
{
if (options.Interval != TimeSpan.Zero)
timer.Change(TimeSpan.Zero, options.Interval);
else
ExecuteNow();
}
public void StopJob()
{
timer.Change(TimeSpan.Zero, TimeSpan.Zero);
}
private void TimerElapsed(object state)
{
try
{
RunJobInterlocked();
}
catch (Exception exc)
{
JobError?.Invoke(this, new FileSyncJobEventArgs(JobName, FileSyncJobStatus.Error, exc));
}
}
private void RunJobInterlocked()
{
if (v_jobRunning)
{
JobError?.Invoke(this, new FileSyncJobEventArgs(JobName, FileSyncJobStatus.Error, new FileSyncJobRunningException("A job is still running")));
return;
}
v_jobRunning = true;
JobStarted?.Invoke(this, new FileSyncJobEventArgs(JobName, FileSyncJobStatus.Running));
try
{
//True Job Code
options.Logger.LogInformation("start job {0}", JobName);
if (options is IFileSyncJobOptions)
syncProvider.SyncSourceToDest();
else if (options is IFileCleanJobOptions)
syncProvider.DeleteFiles();
else
throw new NotImplementedException($"job with options type {options.GetType()}");
options.Logger.LogInformation("end job {0}", JobName);
}
catch (Exception exc)
{
options.Logger.LogError(exc, "JobError {0}", JobName);
JobError?.Invoke(this, new FileSyncJobEventArgs(JobName, FileSyncJobStatus.Error, exc));
}
finally
{
v_jobRunning = false;
}
JobFinished?.Invoke(this, new FileSyncJobEventArgs(JobName, FileSyncJobStatus.Idle));
}
}
}