Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored APIs a bit. Moved some responsibilities around. Need to st…
…art writing more tests.
- Loading branch information
unknown
committed
Oct 12, 2011
1 parent
4e813cd
commit cf6dcd8
Showing
21 changed files
with
255 additions
and
241 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 |
---|---|---|
|
@@ -4,4 +4,5 @@ obj | |
*.suo | ||
App_Data | ||
*.results.xml | ||
/src/packages | ||
/src/packages | ||
_ReSharper.* |
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...kgrounder.EntityFramework/Entities/Job.cs → ...der.EntityFramework/Entities/JobWorker.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
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 @@ | ||
using System.Data.Entity; | ||
|
||
namespace WebBackgrounder.EntityFramework.Entities { | ||
public class JobsContext : DbContext { | ||
public DbSet<JobWorker> JobWorkers { get; set; } | ||
} | ||
} |
82 changes: 0 additions & 82 deletions
82
src/WebBackgrounder.EntityFramework/EntityJobRepository.cs
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
src/WebBackgrounder.EntityFramework/EntityJobWorkerRepository.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,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using WebBackgrounder.EntityFramework.Entities; | ||
|
||
namespace WebBackgrounder.EntityFramework { | ||
public class EntityJobWorkerRepository { | ||
readonly JobsContext _context; | ||
readonly IEnumerable<JobWorker> _workers; | ||
readonly string _jobName; | ||
|
||
public EntityJobWorkerRepository(JobsContext context, string jobName) { | ||
_context = context; | ||
_jobName = jobName; | ||
_workers = from worker in _context.JobWorkers | ||
where worker.Name == jobName | ||
&& worker.Status < (int)JobStatus.Complete | ||
select worker; | ||
} | ||
|
||
public bool AnyActiveWorkers() { | ||
return _workers.Any(); | ||
} | ||
|
||
public JobUnitOfWork ReserveWorker(Guid workerId) { | ||
var worker = new JobWorker { Name = _jobName, WorkerId = workerId, Status = (int)JobStatus.Ready }; | ||
_context.JobWorkers.Add(worker); | ||
_context.SaveChanges(); | ||
|
||
var currentWorker = GetCurrentWorker(); | ||
return currentWorker.WorkerId == workerId ? new JobUnitOfWork(this, currentWorker) : null; | ||
} | ||
|
||
// Retrieves the oldest (aka first inserted) job with the same job name. | ||
JobWorker GetCurrentWorker() { | ||
// REVIEW: Make absolutely sure this queries the database and doesn't look in some context cache. | ||
var currentWorker = (from worker in _context.JobWorkers | ||
where worker.Name == _jobName | ||
&& worker.Status == (int)JobStatus.Ready | ||
orderby worker.Id ascending | ||
select worker).FirstOrDefault(); | ||
|
||
if (currentWorker == null) { | ||
throw new InvalidOperationException("Could not find a job to handle this worker, even though we should have just created one."); | ||
} | ||
|
||
return currentWorker; | ||
} | ||
|
||
public void SetWorkerStarted(JobWorker worker) { | ||
worker.Status = (int)JobStatus.Started; | ||
_context.SaveChanges(); | ||
} | ||
|
||
public void SetWorkerComplete(JobWorker worker) { | ||
worker.Status = (int)JobStatus.Started; | ||
_context.SaveChanges(); | ||
} | ||
|
||
public void SetWorkerFailed(JobWorker worker) { | ||
worker.Status = (int)JobStatus.Started; | ||
_context.SaveChanges(); | ||
} | ||
|
||
public void UpdateIgnoredWorkers(JobWorker currentWorker) { | ||
var ignoredWorkers = from worker in _workers | ||
where worker.Status == (int)JobStatus.Ready | ||
&& worker.Name == currentWorker.Name | ||
&& worker.WorkerId != currentWorker.WorkerId | ||
select worker; | ||
|
||
foreach (var loser in ignoredWorkers) { | ||
loser.Status = (int)JobStatus.Ignored; | ||
} | ||
_context.SaveChanges(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
| ||
namespace WebBackgrounder.EntityFramework { | ||
public enum JobStatus { | ||
Ignored = 0, | ||
Ready = 1, | ||
Started = 2, | ||
Done = 3 | ||
Complete = 3, | ||
Failed = 4, | ||
Ignored = 5 | ||
} | ||
} |
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,28 @@ | ||
using System; | ||
using WebBackgrounder.EntityFramework.Entities; | ||
|
||
namespace WebBackgrounder.EntityFramework { | ||
public class JobUnitOfWork : IDisposable { | ||
readonly EntityJobWorkerRepository _repository; | ||
readonly JobWorker _currentJob; | ||
bool _finished; | ||
|
||
public JobUnitOfWork(EntityJobWorkerRepository repository, JobWorker job) { | ||
_currentJob = job; | ||
_repository = repository; | ||
_repository.SetWorkerStarted(_currentJob); | ||
} | ||
|
||
public void Complete() { | ||
_repository.UpdateIgnoredWorkers(_currentJob); | ||
_repository.SetWorkerComplete(_currentJob); | ||
_finished = true; | ||
} | ||
|
||
public void Dispose() { | ||
if (!_finished) { | ||
_repository.SetWorkerFailed(_currentJob); | ||
} | ||
} | ||
} | ||
} |
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
54 changes: 18 additions & 36 deletions
54
src/WebBackgrounder.EntityFramework/WebFarmJobCoordinator.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
Oops, something went wrong.
This is async, should be: