Projektdateien hinzufügen.
This commit is contained in:
120
FileinterfaceCronScheduler/Scheduler/CronJobRepository.cs
Normal file
120
FileinterfaceCronScheduler/Scheduler/CronJobRepository.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace FileInterface.Scheduler
|
||||
{
|
||||
public sealed class CronJobRepository
|
||||
{
|
||||
private readonly object _sync = new object();
|
||||
private readonly string _fileName;
|
||||
private readonly JavaScriptSerializer _serializer = new JavaScriptSerializer();
|
||||
private List<CronJobConfig> _jobs;
|
||||
|
||||
public CronJobRepository(string fileName)
|
||||
{
|
||||
_fileName = Path.GetFullPath(fileName);
|
||||
_serializer.MaxJsonLength = Int32.MaxValue;
|
||||
_jobs = LoadInternal();
|
||||
}
|
||||
|
||||
public IList<CronJobConfig> GetAll()
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
// Clone through JSON so callers cannot mutate repository state behind our lock.
|
||||
return Clone(_jobs);
|
||||
}
|
||||
}
|
||||
|
||||
public CronJobConfig GetById(string id)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
CronJobConfig job = _jobs.FirstOrDefault(j => String.Equals(j.Id, id, StringComparison.OrdinalIgnoreCase));
|
||||
return job == null ? null : Clone(job);
|
||||
}
|
||||
}
|
||||
|
||||
public CronJobConfig Save(CronJobConfig job)
|
||||
{
|
||||
if (job == null) throw new ArgumentNullException("job");
|
||||
Validate(job);
|
||||
|
||||
lock (_sync)
|
||||
{
|
||||
if (String.IsNullOrWhiteSpace(job.Id))
|
||||
job.Id = Guid.NewGuid().ToString("N");
|
||||
|
||||
int index = _jobs.FindIndex(j => String.Equals(j.Id, job.Id, StringComparison.OrdinalIgnoreCase));
|
||||
if (index >= 0) _jobs[index] = Clone(job);
|
||||
else _jobs.Add(Clone(job));
|
||||
|
||||
SaveInternal();
|
||||
return Clone(job);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Delete(string id)
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
int count = _jobs.RemoveAll(j => String.Equals(j.Id, id, StringComparison.OrdinalIgnoreCase));
|
||||
if (count > 0) SaveInternal();
|
||||
return count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
private List<CronJobConfig> LoadInternal()
|
||||
{
|
||||
if (!File.Exists(_fileName)) return new List<CronJobConfig>();
|
||||
string json = File.ReadAllText(_fileName, Encoding.UTF8);
|
||||
if (String.IsNullOrWhiteSpace(json)) return new List<CronJobConfig>();
|
||||
return _serializer.Deserialize<List<CronJobConfig>>(json) ?? new List<CronJobConfig>();
|
||||
}
|
||||
|
||||
private void SaveInternal()
|
||||
{
|
||||
string dir = Path.GetDirectoryName(_fileName);
|
||||
if (!String.IsNullOrWhiteSpace(dir)) Directory.CreateDirectory(dir);
|
||||
|
||||
string json = _serializer.Serialize(_jobs);
|
||||
string tmp = _fileName + ".tmp";
|
||||
File.WriteAllText(tmp, json, new UTF8Encoding(false));
|
||||
|
||||
if (File.Exists(_fileName))
|
||||
{
|
||||
string backup = _fileName + ".bak";
|
||||
try { File.Replace(tmp, _fileName, backup, true); }
|
||||
catch
|
||||
{
|
||||
File.Delete(_fileName);
|
||||
File.Move(tmp, _fileName);
|
||||
}
|
||||
}
|
||||
else File.Move(tmp, _fileName);
|
||||
}
|
||||
|
||||
private static void Validate(CronJobConfig job)
|
||||
{
|
||||
if (String.IsNullOrWhiteSpace(job.Name)) throw new InvalidOperationException("Name is required.");
|
||||
CronExpression.Parse(job.Cron);
|
||||
if (String.IsNullOrWhiteSpace(job.CustomFunction)) throw new InvalidOperationException("CustomFunction is required.");
|
||||
if (!job.CallOnceWithoutFile && String.IsNullOrWhiteSpace(job.SourceDirectory))
|
||||
throw new InvalidOperationException("SourceDirectory is required for file jobs.");
|
||||
if (job.MoveFileAfterSuccess && String.IsNullOrWhiteSpace(job.MoveTargetDirectory))
|
||||
throw new InvalidOperationException("MoveTargetDirectory is required when MoveFileAfterSuccess=true.");
|
||||
if (job.InArgs == null) job.InArgs = new List<string>();
|
||||
if (String.IsNullOrWhiteSpace(job.SearchPattern)) job.SearchPattern = "*.*";
|
||||
if (job.JoinSeparator == null) job.JoinSeparator = ";";
|
||||
}
|
||||
|
||||
private T Clone<T>(T value)
|
||||
{
|
||||
return _serializer.Deserialize<T>(_serializer.Serialize(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user