protected void PropageteRequestToWorkers(RestRequest request, Queue queue, Worker worker, Worker coworker) { var requestForMaster = request.AddParameter("Cooperator", coworker.Address); IRestResponse response; if (WorkerQueries.IsWorkerAlive(DBConnection, worker.Id)) { response = PropagateRequest(requestForMaster, worker); if (response.ResponseStatus == ResponseStatus.TimedOut || response.ResponseStatus == ResponseStatus.Error) { response = PropagateRequest(request, coworker); QueuesQueries.swapWorkers(DBConnection, queue); } } else { response = PropagateRequest(request, coworker); QueuesQueries.swapWorkers(DBConnection, queue); } if (response.StatusCode != HttpStatusCode.OK) { throw new BadRequestException(); } }
protected T PropageteRequestToWorkers <T>(RestRequest request, ICollection collection, Worker worker, Worker coworker) where T : new() { IRestResponse <T> response; if (WorkerQueries.IsWorkerAlive(DBConnection, worker.Id)) { response = PropagateRequest <T>(request, worker); if (response.ResponseStatus == ResponseStatus.TimedOut || response.ResponseStatus == ResponseStatus.Error) { response = PropagateRequest <T>(request, coworker); swapWorkers(collection); } } else { response = PropagateRequest <T>(request, coworker); swapWorkers(collection); } if (response.StatusCode != HttpStatusCode.OK) { throw new BadRequestException(); } return(response.Data); }
void CalculateQueueWorkers(string queueName, out Worker worker, out Worker coworker) { var workerCount = WorkerQueries.GetWorkersCount(DBConnection); var nameHash = Math.Abs(queueName.GetHashCode()); var workerPosition = nameHash % workerCount; var coworkerPosition = (nameHash + 1) % workerCount; worker = WorkerQueries.GetWorker(DBConnection, workerPosition); coworker = WorkerQueries.GetWorker(DBConnection, coworkerPosition); }
void AddNewWorker(WorkerHeartbeat request) { var worker = new Worker() { Id = request.Id, Address = request.Address, Alive = true, LastHeartbeat = DateTime.UtcNow }; WorkerQueries.AddNewWorker(DBConnection, worker); }
protected void processRequest(string queueName, RestRequest request) { if (!QueuesQueries.QueueExists(DBConnection, queueName)) { throw new QueueNotExistsException(); } else { var queue = QueuesQueries.getQueueByName(DBConnection, queueName); var worker = WorkerQueries.GetWorkerById(DBConnection, queue.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, queue.Cooperator); PropageteRequestToWorkers(request, queue, worker, coworker); } }
protected void processRequest(string topicName, RestRequest request) { if (!TopicsQueries.TopicExists(DBConnection, topicName)) { throw new TopicNotExistsException(); } else { var topic = TopicsQueries.getTopicByName(DBConnection, topicName); var worker = WorkerQueries.GetWorkerById(DBConnection, topic.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, topic.Cooperator); PropageteRequestToWorkers(request, topic, worker, coworker); } }
public Message Get(GetMessage request) { var requestToSend = new RestRequest($"queues/{request.QueueName}/messages", Method.GET); if (!QueuesQueries.QueueExists(DBConnection, request.QueueName)) { throw new QueueNotExistsException(); } else { var queue = QueuesQueries.getQueueByName(DBConnection, request.QueueName); var worker = WorkerQueries.GetWorkerById(DBConnection, queue.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, queue.Cooperator); return(PropageteRequestToWorkers <Message>(requestToSend, queue, worker, coworker)); } }
public void Delete(DeleteQueue request) { var requestToSend = new RestRequest($"queues/{request.QueueName}", Method.DELETE); if (!QueuesQueries.QueueExists(DBConnection, request.QueueName)) { throw new QueueNotExistsException(); } else { var queue = QueuesQueries.getQueueByName(DBConnection, request.QueueName); var worker = WorkerQueries.GetWorkerById(DBConnection, queue.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, queue.Cooperator); QueuesQueries.DeleteQueue(DBConnection, request.QueueName); PropageteRequestToWorkers(requestToSend, queue, worker, coworker); } }
public void Delete(DeleteTopic request) { var requestToSend = new RestRequest($"/topics/{request.TopicName}", Method.DELETE); if (!TopicsQueries.TopicExists(DBConnection, request.TopicName)) { throw new TopicNotExistsException(); } else { var topic = TopicsQueries.getTopicByName(DBConnection, request.TopicName); var worker = WorkerQueries.GetWorkerById(DBConnection, topic.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, topic.Cooperator); TopicsQueries.DeleteTopic(DBConnection, request.TopicName); PropageteRequestToWorkers(requestToSend, topic, worker, coworker); } }
public Announcement Get(GetAnnouncement request, int subscriberId) { var requestToSend = new RestRequest($"/topics/{request.TopicName}/announcements", Method.GET); requestToSend.AddParameter("SubscriberId", subscriberId); if (!TopicsQueries.TopicExists(DBConnection, request.TopicName)) { throw new TopicNotExistsException(); } else { var topic = TopicsQueries.getTopicByName(DBConnection, request.TopicName); var worker = WorkerQueries.GetWorkerById(DBConnection, topic.Worker); var coworker = WorkerQueries.GetWorkerById(DBConnection, topic.Cooperator); return(PropageteRequestToWorkers <Announcement>(requestToSend, topic, worker, coworker)); } }
public void Register(WorkerHeartbeat request) { if (Encrypt.EncryptToken(request.Time, request.Id, request.Address, request.Token)) { using (IDbTransaction transaction = DBConnection.OpenTransaction()) { if (!WorkerQueries.WorkerExists(DBConnection, request.Id)) { AddNewWorker(request); } else { Revive(request); } transaction.Commit(); } } else { throw new BadRequestException(); } }
void Revive(WorkerHeartbeat request) { WorkerQueries.ReviveWorker(DBConnection, request); }