Пример #1
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        void AddNewWorker(WorkerHeartbeat request)
        {
            var worker = new Worker()
            {
                Id            = request.Id,
                Address       = request.Address,
                Alive         = true,
                LastHeartbeat = DateTime.UtcNow
            };

            WorkerQueries.AddNewWorker(DBConnection, worker);
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
        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);
            }
        }
Пример #7
0
        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));
            }
        }
Пример #8
0
        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);
            }
        }
Пример #9
0
        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);
            }
        }
Пример #10
0
        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));
            }
        }
Пример #11
0
 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();
     }
 }
Пример #12
0
 void Revive(WorkerHeartbeat request)
 {
     WorkerQueries.ReviveWorker(DBConnection, request);
 }