private static void AssignWork(Work work, Guid workerId) { var workAssignment = new WorkAssignment { Work = work, WorkerId = workerId, AssignedAt = DateTime.Now }; AssignedWork.TryAdd(work.MessageId, workAssignment); using (var client = new LocalClient()) { client.PublishMessage(workerId.ToString(), work.ToString()); } }
static void Main(string[] args) { Func<string> prompt = () => { Console.Write("Enter your message: "); return Console.ReadLine(); }; string message; while (!string.IsNullOrEmpty(message = prompt())) { using (var client = new LocalClient()) { var work = new Work { MessageId = Guid.NewGuid(), Message = message }; client.PublishMessage(Channels.Distribution, work.ToString()); } } }
private static bool TryPurgeWork(out Work work) { var messageId = (from w in AssignedWork where WorkTimeoutExpired(w.Value.AssignedAt) select w.Key).FirstOrDefault(); if (messageId == null) { work = null; return false; } WorkAssignment workAssignment; if (AssignedWork.TryRemove(messageId, out workAssignment)) { work = workAssignment.Work; Console.WriteLine("[{0}] Purging and reassigning work: {1}", DateTime.Now.ToShortTimeString(), work); return true; } work = null; return false; }