-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from TinkoffCreditSystems/feature/5
Feature/5
- Loading branch information
Showing
27 changed files
with
690 additions
and
8 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
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
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Horarium\Horarium.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,118 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory | ||
{ | ||
public class InMemoryRepository : IJobRepository | ||
{ | ||
private readonly OperationsProcessor _processor = new OperationsProcessor(); | ||
|
||
private readonly JobsStorage _storage = new JobsStorage(); | ||
|
||
private readonly ConcurrentDictionary<string, RecurrentJobSettings> _settingsStorage = | ||
new ConcurrentDictionary<string, RecurrentJobSettings>(); | ||
|
||
public Task<JobDb> GetReadyJob(string machineName, TimeSpan obsoleteTime) | ||
{ | ||
JobDb Query() | ||
{ | ||
var job = _storage.FindReadyJob(obsoleteTime); | ||
if (job == null) return null; | ||
|
||
_storage.Remove(job.JobId); | ||
|
||
job.Status = JobStatus.Executing; | ||
job.ExecutedMachine = machineName; | ||
job.StartedExecuting = DateTime.UtcNow; | ||
job.CountStarted++; | ||
|
||
_storage.Add(job); | ||
|
||
return job; | ||
} | ||
|
||
return _processor.Execute(Query); | ||
} | ||
|
||
public Task AddJob(JobDb job) | ||
{ | ||
return _processor.Execute(() => _storage.Add(job.Copy())); | ||
} | ||
|
||
public Task FailedJob(string jobId, Exception error) | ||
{ | ||
return _processor.Execute(() => | ||
{ | ||
var job = _storage.GetById(jobId); | ||
if (job == null) return; | ||
|
||
_storage.Remove(job); | ||
|
||
job.Status = JobStatus.Failed; | ||
job.Error = error.Message + error.StackTrace; | ||
|
||
_storage.Add(job); | ||
}); | ||
} | ||
|
||
public Task RemoveJob(string jobId) | ||
{ | ||
return _processor.Execute(() => _storage.Remove(jobId)); | ||
} | ||
|
||
public Task RepeatJob(string jobId, DateTime startAt, Exception error) | ||
{ | ||
return _processor.Execute(() => | ||
{ | ||
var job = _storage.GetById(jobId); | ||
if (job == null) return; | ||
|
||
_storage.Remove(job); | ||
|
||
job.Status = JobStatus.RepeatJob; | ||
job.StartAt = startAt; | ||
job.Error = error.Message + error.StackTrace; | ||
|
||
_storage.Add(job); | ||
}); | ||
} | ||
|
||
public Task AddRecurrentJob(JobDb job) | ||
{ | ||
return _processor.Execute(() => | ||
{ | ||
var foundJob = _storage.FindRecurrentJobToUpdate(job.JobKey) ?? job.Copy(); | ||
|
||
_storage.Remove(foundJob); | ||
|
||
foundJob.Cron = job.Cron; | ||
foundJob.StartAt = job.StartAt; | ||
|
||
_storage.Add(foundJob); | ||
}); | ||
} | ||
|
||
public Task AddRecurrentJobSettings(RecurrentJobSettings settings) | ||
{ | ||
_settingsStorage.AddOrUpdate(settings.JobKey, settings, (_, __) => settings); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<string> GetCronForRecurrentJob(string jobKey) | ||
{ | ||
if (!_settingsStorage.TryGetValue(jobKey, out var settings)) | ||
throw new Exception($"Settings for recurrent job (jobKey = {jobKey}) aren't found"); | ||
|
||
return Task.FromResult(settings.Cron); | ||
} | ||
|
||
public Task<Dictionary<JobStatus, int>> GetJobStatistic() | ||
{ | ||
return Task.FromResult(_storage.GetStatistics()); | ||
} | ||
} | ||
} |
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,13 @@ | ||
using System.Collections.Generic; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes.Comparers | ||
{ | ||
internal class JobIdComparer : IComparer<JobDb> | ||
{ | ||
public int Compare(JobDb x, JobDb y) | ||
{ | ||
return StaticJobIdComparer.Compare(x, y); | ||
} | ||
} | ||
} |
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 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes.Comparers | ||
{ | ||
internal class JobKeyComparer : IComparer<JobDb> | ||
{ | ||
public int Compare(JobDb x, JobDb y) | ||
{ | ||
var result = string.Compare(x.JobKey, y.JobKey, StringComparison.Ordinal); | ||
|
||
return result == 0 ? StaticJobIdComparer.Compare(x, y) : result; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Horarium.InMemory/Indexes/Comparers/StartAtComparer.cs
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 @@ | ||
using System.Collections.Generic; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes.Comparers | ||
{ | ||
internal class StartAtComparer : IComparer<JobDb> | ||
{ | ||
public int Compare(JobDb x, JobDb y) | ||
{ | ||
if (x.StartAt < y.StartAt) return -1; | ||
if (x.StartAt > y.StartAt) return 1; | ||
|
||
return StaticJobIdComparer.Compare(x, y); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Horarium.InMemory/Indexes/Comparers/StartedExecutingComparer.cs
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 @@ | ||
using System.Collections.Generic; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes.Comparers | ||
{ | ||
internal class StartedExecutingComparer : IComparer<JobDb> | ||
{ | ||
public int Compare(JobDb x, JobDb y) | ||
{ | ||
if (x.StartedExecuting < y.StartedExecuting) return -1; | ||
if (x.StartedExecuting > y.StartedExecuting) return 1; | ||
|
||
return StaticJobIdComparer.Compare(x, y); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Horarium.InMemory/Indexes/Comparers/StaticJobIdComparer.cs
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,13 @@ | ||
using System; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes.Comparers | ||
{ | ||
internal static class StaticJobIdComparer | ||
{ | ||
public static int Compare(JobDb x, JobDb y) | ||
{ | ||
return string.Compare(x.JobId, y.JobId, StringComparison.Ordinal); | ||
} | ||
} | ||
} |
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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Horarium.InMemory.Indexes.Comparers; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes | ||
{ | ||
internal class ExecutingJobIndex : IAddRemoveIndex | ||
{ | ||
private readonly SortedSet<JobDb> _startedExecutingIndex = new SortedSet<JobDb>(new StartedExecutingComparer()); | ||
|
||
private readonly JobKeyIndex _jobKeyIndex = new JobKeyIndex(); | ||
|
||
public void Add(JobDb job) | ||
{ | ||
if (job.Status != JobStatus.Executing) return; | ||
|
||
_startedExecutingIndex.Add(job); | ||
_jobKeyIndex.Add(job); | ||
} | ||
|
||
public void Remove(JobDb job) | ||
{ | ||
_startedExecutingIndex.Remove(job); | ||
_jobKeyIndex.Remove(job); | ||
} | ||
|
||
public int Count() | ||
{ | ||
return _startedExecutingIndex.Count; | ||
} | ||
|
||
public JobDb GetJobKeyEqual(string jobKey) | ||
{ | ||
return _jobKeyIndex.Get(jobKey); | ||
} | ||
|
||
public JobDb GetStartedExecutingLessThan(DateTime startedExecuting) | ||
{ | ||
if (_startedExecutingIndex.Count != 0 && _startedExecutingIndex.Min.StartAt < startedExecuting) | ||
return _startedExecutingIndex.Min; | ||
|
||
return null; | ||
} | ||
} | ||
} |
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,27 @@ | ||
using System.Collections.Generic; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes | ||
{ | ||
internal class FailedJobIndex : IAddRemoveIndex | ||
{ | ||
private readonly Dictionary<string, JobDb> _index = new Dictionary<string, JobDb>(); | ||
|
||
public void Add(JobDb job) | ||
{ | ||
if (job.Status != JobStatus.Failed) return; | ||
|
||
_index.Add(job.JobId, job); | ||
} | ||
|
||
public void Remove(JobDb job) | ||
{ | ||
_index.Remove(job.JobId); | ||
} | ||
|
||
public int Count() | ||
{ | ||
return _index.Count; | ||
} | ||
} | ||
} |
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,13 @@ | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes | ||
{ | ||
internal interface IAddRemoveIndex | ||
{ | ||
void Add(JobDb job); | ||
|
||
void Remove(JobDb job); | ||
|
||
int Count(); | ||
} | ||
} |
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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Horarium.InMemory.Indexes.Comparers; | ||
using Horarium.Repository; | ||
|
||
namespace Horarium.InMemory.Indexes | ||
{ | ||
internal class JobKeyIndex : IAddRemoveIndex | ||
{ | ||
private readonly Dictionary<string, SortedSet<JobDb>> _index = new Dictionary<string, SortedSet<JobDb>>(); | ||
|
||
public void Add(JobDb job) | ||
{ | ||
if (string.IsNullOrEmpty(job.JobKey)) return; | ||
|
||
if (!_index.TryGetValue(job.JobKey, out var set)) | ||
_index[job.JobKey] = new SortedSet<JobDb>(new JobIdComparer()) {job}; | ||
else | ||
set.Add(job); | ||
} | ||
|
||
public void Remove(JobDb job) | ||
{ | ||
if (string.IsNullOrEmpty(job.JobKey)) return; | ||
|
||
if (!_index.TryGetValue(job.JobKey, out var set)) return; | ||
|
||
set.Remove(job); | ||
} | ||
|
||
public int Count() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public JobDb Get(string jobKey) | ||
{ | ||
if (!_index.TryGetValue(jobKey, out var set)) return null; | ||
|
||
if (set.Count != 0) return set.Min; | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.