forked from chad/resque-sharp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
238 additions
and
9 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,150 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace resque | ||
{ | ||
public class Worker | ||
{ | ||
string[] queues; | ||
public Worker(params string[] queues) | ||
{ | ||
this.queues = queues; | ||
} | ||
|
||
public void work(int interval) | ||
{ | ||
work(interval, null); | ||
} | ||
|
||
public void work(int interval, Func<Job,bool> block) | ||
{ | ||
try | ||
{ | ||
startup(); | ||
while (true) | ||
{ | ||
Job job = reserve(); | ||
if (job != null) | ||
{ | ||
process(job, block); | ||
} | ||
else | ||
{ | ||
if (interval == 0) | ||
break; | ||
System.Threading.Thread.Sleep(interval * 1000); | ||
} | ||
|
||
|
||
} | ||
} | ||
finally | ||
{ | ||
unregisterWorker(); | ||
} | ||
} | ||
|
||
private void unregisterWorker() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private void process(Job job, Func<Job, bool> block) | ||
{ | ||
try | ||
{ | ||
setWorkingOn(job); | ||
job.perform(); | ||
} | ||
catch (Exception e) | ||
{ | ||
job.fail(e); | ||
setFailed(); | ||
} | ||
finally | ||
{ | ||
if (block != null) | ||
{ | ||
block(job); | ||
} | ||
setDoneWorking(); | ||
} | ||
} | ||
|
||
private void setFailed() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private void setWorkingOn(Job job) | ||
{ | ||
job.worker = this; | ||
string data = Resque.encode(new Dictionary<string, object>() { { "queue", job.queue }, { "run_at", currentTimeFormatted() }, { "payload", job.payload } }); | ||
Resque.redis().Set("resque:worker:" + workerId(), data); | ||
} | ||
|
||
private void setDoneWorking() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private void startup() | ||
{ | ||
//pruneDeadWorkers(); | ||
registerWorker(); | ||
} | ||
|
||
private void registerWorker() | ||
{ | ||
Resque.redis().AddToSet("resque:workers", workerId()); | ||
setStarted(); | ||
} | ||
|
||
private Job reserve() | ||
{ | ||
foreach(string queue in queues) { | ||
Job job = Job.Reserve(queue); | ||
if (job != null) | ||
{ | ||
return job; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void setStarted() | ||
{ | ||
currentTimeFormatted(); | ||
Resque.redis().Set(new Dictionary<string, byte[]>() { { startedKey(), Encoding.UTF8.GetBytes(currentTimeFormatted()) } }); | ||
} | ||
|
||
private static string currentTimeFormatted() | ||
{ | ||
DateTime currentTime = DateTime.Now; | ||
string currentTimeFormatted = currentTime.ToString("ddd MMM dd hh:mm:ss zzzz yyyy"); | ||
return currentTimeFormatted; | ||
} | ||
|
||
private string startedKey() | ||
{ | ||
return "resque:worker:" + workerId() + ":started"; | ||
} | ||
|
||
public bool IsWorking() | ||
{ | ||
return state() == "working"; | ||
} | ||
|
||
public string state() | ||
{ | ||
return Resque.redis().ContainsKey("resque:worker:" + workerId()) ? "working" : "idle"; | ||
} | ||
|
||
internal string workerId() | ||
{ | ||
return "FIXME"; | ||
} | ||
} | ||
} |
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,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework; | ||
|
||
namespace resque | ||
{ | ||
[TestFixture] | ||
public class WorkerTest | ||
{ | ||
private Worker worker; | ||
[SetUp] | ||
public void Init() | ||
{ | ||
Resque.setRedis(new Redis("192.168.1.119", 6379)); | ||
Resque.redis().FlushAll(); | ||
worker = new Worker("jobs"); | ||
Job.create("jobs", "resque.DummyJob", 20, "/tmp"); | ||
} | ||
|
||
//[Test] | ||
//public void CanFailJobs() | ||
//{ | ||
// Job.create("jobs", "resque.BadJob"); | ||
// worker.work(0); | ||
//} | ||
|
||
[Test] | ||
public void KnowsWhenItsWorking() | ||
{ | ||
worker.work(0, (Job Job) => {Assert.That(worker.IsWorking(), Is.True); return true;}); | ||
} | ||
|
||
[Test] | ||
public void CanDoLambdaDambdaDingDong() | ||
{ | ||
Assert.That( | ||
foo(x => x.ToUpper()), | ||
Is.EqualTo("LOL") | ||
); | ||
foo((string x) => { Assert.That(x, Is.EqualTo("lol")); return "blah"; }); | ||
} | ||
|
||
public string foo(Func<string, string> rocker) { | ||
return rocker("lol"); | ||
} | ||
} | ||
} |
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